Skip to main content

Hello, Git!

The previous section explains what Git is and its key features. Now, let's try to do some hands-on with Git. This guide will walk through the basics of getting started with Git, including installation, configuration, and some fundamental commands.

Installation

To get started with Git, the first step is to install it on the machine. Git is available for various operating systems, including Linux, macOS, and Windows.

Linux

On many Linux distributions, Git comes pre-installed. If not, it can be installed using the package manager specific to the distribution.

Here is how to install Git on some common Linux distributions by using terminal:

Terminal
sudo apt-get update
sudo apt-get install git
info

For other Linux distributions and methods, refer to Download for Linux and Unix page on the official Git website for detailed instructions.

macOS

On macOS, Git can be installed using Homebrew, a popular package manager for macOS.

info

If Homebrew is not already installed on the system then it can be installed by following the instructions on the official Homebrew website: https://brew.sh/.

If Homebrew is already installed then Git can be installed by running the following command in the terminal:

Terminal
brew install git
info

Alternatively, Git can also be installed on macOS by a variety of other methods. Refer to Download for macOS page on the official Git website for more information.

Windows

On Windows, the easiest way to install Git is by using the official installer. Download the installer from the official Git website: https://git-scm.com/download/win and follow the installation instructions.

info

Similarly, Git can also be installed on Windows by a variety of other methods. All these methods are explained on the same page linked above.

Verification for Installation

After installation, it can be verified that Git is installed correctly by running following command in the terminal in case of Linux and macOS:

Terminal
git --version

While in case of Windows, Git can be verified by opening "Git Bash" (which is installed along with Git) and running the same command.

This should display the installed version of Git.

info

There are various GUI tools available for Git, such as GitKraken, SourceTree, GitHub Desktop, and others as listed on GUI Clients page on the official Git website. However, Git is primarily a command-line tool, and for learning it is recommended to get familiar with the command line interface first.

Git CLI can be used with terminal applications like terminal emulators on Linux and macOS or using Git Bash on Windows.

Configuration

Before using Git, it is recommended to configure some basic settings, such as the user name and email address. This information will be associated with the commits made using Git.

The user name and email address can be configured by running the following command:

Terminal
git config --global user.name "Adam"
git config --global user.email "adam@example.com"

The --global flag ensures that these settings apply to all repositories on the system. If it is required to set different user name and email address for a specific repository, this can be done by running the same commands without the --global flag while being inside that repository.

The configuration settings can be verified by running the following command:

Terminal
git config --list

This will display a list of all the configuration settings, including the user name and email address.

Editing Configuration

Configuration settings can be changed at any time by running the git config command with the desired values. For example, to change the user name and email address, the following commands can be run:

Terminal
git config --global user.name "Bob"
git config --global user.email "bob@example.com"

Editing configuration can also be done by running following config editing command:

Terminal
git config --global --edit

Again, the --global flag ensures that these settings apply to all repositories on the system. If it is required to set different user name, email address, or any other settings for a specific repository, this can be done by running the same commands without the --global flag while being inside that repository.

Another possible way to edit the configuration is to edit the Git configuration file directly. The global configuration file is located at ~/.gitconfig on Linux and macOS, and at %USERPROFILE%\.gitconfig on Windows for global settings. Each repository also has its own configuration file located at .git/config within the repository directory for repository-specific settings.

It is important to note that changes made to the configuration file directly will not be reflected in the output of git config --list until the file is saved.

tip

Check Commands Cheat Sheet for more information about Git commands and configuration options.

Hello (Initializing a Repository)

To start using Git for version control, a new repository needs to be initialized. This can be done by navigating to the project directory in the terminal and running the following command:

Terminal
git init

This will create a new Git repository in the current directory, allowing to start tracking changes to the files in the project.

Example

Here is an example of initializing a new Git repository:

Terminal
mkdir hello-world
cd hello-world
git init

Status Check

After initializing a repository, the status of the repository can be checked by running the following command inside the repository directory:

Terminal
git status

Summary

This section provided an introduction to getting started with Git, including installation, configuration, and initializing a repository. With these basics in place, it is now possible to start using Git for version control. In the next sections, some fundamental scenarios will be covered.