Git is the most popular version control system of choice for many software developers. Linus Torvalds developed Git during the development of the Linux kernel back in 2005. And since then, developers widely use this version control system to collaborate with other members on their projects.

If you're learning software development and its various facets, you might have already heard about Git at some point. This guide will explain Git in detail, along with a brief guide on how to install and configure it on Linux.

What Is Git, and Why Do You Need It?

Software development is challenging. It involves working with several files and often requires tinkering with the source code to achieve the intended output before it's ready to use.

Not just that, though, even after the code is running in production, there's still the need for periodic refactoring to keep the code efficient, maintainable, and readable to other developers on the team.

With so many variables, and multiple developers working on a project simultaneously, it can soon become challenging to keep a tab on all the different project files and their revisions.

This is where a version control system (VCS) like Git comes into play. It makes it easier to track and manage the changes to the code submitted by various team members, and in turn, accelerates the software development and testing process.

Some of the notable benefits of using a version control system include:

  • Access to long-term change history so you can view every change that's ever been made to a file by the team.
  • Branching and merging, which facilitates simultaneous contribution and allows you to merge multiple versions of a file into a single file to apply the changes and prevent file duplication.

Of course, what version control system you use also determines the benefits you can seek from using it. In the case of Git, since it's a distributed version control system (DVCS), all your code files are present on every contributor's computer.

So, in addition to the above benefits (and a few others), Git also allows you to work offline—except for the push and pull functionalities, which still require internet connectivity to work.

Related: The Top 10 Version Control Systems for Linux

How to Install Git on Linux

Installing Git on Linux is fairly straightforward. Use the following commands, depending on your Linux distro, to install it on your computer.

Install Git on Debian/Ubuntu

Git is available on the official Ubuntu and Debian repositories. Therefore, you can easily install it using APT:

        sudo apt install git
    

Install Git on Fedora

You can install Git on Fedora using either DNF or YUM. If you're running an older version of Fedora (up to Fedora 21), use YUM:

        sudo yum install git
    

Conversely, if you have Fedora 22 or above running on your system, you can use DNF to install Git.

        sudo dnf install git
    

Install Git on Arch Linux

If you're on Arch Linux, you can install Git using Pacman:

        sudo pacman -S git
    

Install Git on FreeBSD

To install Git on FreeBSD, issue the following command:

        sudo pkg install git
    

Once done, verify if the installation was successful by running the following command:

        git --version
    

If it returns a version number, it means that the installation was successful. If not, you need to go over the installation process again.

How to Configure Git on Linux

Once you've installed Git on your system, you need to configure some of its components before you can use it, such as the username, email address, and the default text editor. This configuration will be a one-time process, and your configured settings should last as long as you don't remove Git from your system.

Create an Identity for Git

To begin with, you first need to set up a default identity (username and email address) for every commit you make on your system. There are two ways to do this. You can either set a global identity so that all the commits you push go through the same identity or set a per-repository identity to use separate identities for different projects.

To set a global identity, open the terminal and run the below commands:

        git config --global user.name "your_name"
git config --global user.email "your_email_address"

If you want to set up your default identity for a particular repository, first head over to the directory which contains the repository. Use the ls command to list directories (and sub-directories) and the cd command to go into them.

Once you're in the repository, run the following commands in the terminal:

        git config user.name "your_name"
git config user.email "your_email_address"

Configure SSH for Git on Linux

Furthermore, although not necessary, you can also set up SSH for Git on your computer to allow password-less logins. That way, you don't have to enter your password every time you want to commit changes to a repository.

To do this, open a terminal window and run the following command to create a new SSH key with your email:

        ssh-keygen -t rsa -b 4096 -C "your_email_address"
    

When prompted for a file name, specify the location where you want to save the key and hit Enter; to proceed with the default option, press Enter.

The system will now ask you to set a passphrase to add an additional layer of security to SSH on your machine. Type a strong passphrase that you can remember and hit Enter.

Finally, you need to add the SSH key to the ssh-agent, which holds your system's private keys. For this, run the following code in the terminal:

        ssh-add ~/.ssh/id
    

Once you've configured your identity, you can configure Git further to suit your workflow.

Change the Default Text Editor for Git

One of the additional configurations you can do is change Git's default text editor for your interactions.

By default, Git is configured to use the Vim text editor. However, if you've never used Vim before, you might not feel at home using it. To demonstrate the process, we'll be setting nano as the default Git text editor. But if you have a preferred text editor, feel free to replace nano with that in the following command:

        git config --global core.editor nano
    

Review the Configurations

When you've configured Git to your preferences, check the configuration settings once to ensure they're correct. Run the following command to get a list of all the Git configuration settings for your system:

        git config --list
    

At some point of time in the future, if you'd like to edit the configuration, open the gitconfig file by running:

        nano ~/.gitconfig
    

Then, edit the values of the identities that you want to change.

Successfully Running Git on Linux

Using the guide above, you should be able to install and configure Git on your Linux system in no time. And hereafter, you must incorporate Git into your workflow to better manage your projects.

For this purpose, there are various Git services out there that can help you manage your repositories. One such is GitHub, which facilitates version control while offering secure cloud storage and integration support for a myriad of tools.

If you're new to Git, though, and wondering where to begin, learning how to create your first repository might help you in getting comfortable with the tool.