access_time
hourglass_empty
person_outline

How to Install Git on Ubuntu 18.04: The Ultimate Guide

If you are a developer or learning to be one, you might already know how important a version control system is. It helps you keep track of every change that happens to your code. Here, we will be talking about one of the best version control systems available right now — Git.

In this tutorial, you will learn how to install Git on Ubuntu 18.04 to help you write code more effectively.

Why Use Git?

Git can be considered the most popular version control system to date. With it, you can manage your software code by monitoring changes, reverting back to previous versions of your code, or creating new branches for alternative code.

We will list several advantages of using Git, which makes it such an amazing tool, such as:

  • Branching and merging — Git allows you to create a branch of your code when you need to split your work into smaller parts. This will also help you divide your tasks based on the level of importance. What’s great, merging the code back only takes seconds.
  • Small and fast — the tool is lightweight and easy to use because most of the operations can happen locally on your computer.
  • Distributed and secure — as a distributed version control system, you don’t have to worry about data loss. That’s because every user owns a copy of the repository. Moreover, every file is checksummed to prevent corrupt or incorrect data.
  • Staging area — this feature makes Git stand out from the rest. It acts as an intermediate area to review and format every commit of your repository.
  • Open-source and free — Git is a free tool and is maintained by a strong and dedicated community.

What’s great, developers are not the only ones that can benefit from Git. Other fields of work, such as marketing, customer support, and so on are encouraged to implement Git in their projects.

How to Install Git on Ubuntu 18.04

There are two essential steps that you need to follow — installing Git and configuring it on your Linux computer. However, before we begin, pay attention to these prerequisites first.

Prerequisites

Make sure that your computer is running on Ubuntu 18.04, codenamed Bionic Beaver. Also, you have to log in to your Linux as root or a user with sudo privileges. If you want to install Git on any other operating system, feel free to read our Git tutorial.

1. Installing Git on Ubuntu

There are two methods to install Git on Ubuntu. We’ll break them one by one and you can choose which one works best for you. Remember, both options require you to use the Linux terminal.

Installing Git With APT

Ubuntu 18.04 already contains Git in default repositories. You can easily install it using the APT package manager.

  1. Firstly,  update the repository by running the following command:
    sudo apt-get update
  2. Note that the version in the repositories might not be the newest one. You can check the available versions with:
    apt-cache policy git

    Here’s an example of the output:

       git:
    Installed: (none)
    Candidate: 1:2.17.1-1ubuntu0.4
    Version table:
       1:2.17.1-1ubuntu0.4 500
       500 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages
       500 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages
       1:2.17.0-1ubuntu1 500
       500 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages
  3. There are two available versions – 1:2.17.1-1ubuntu0.4 500 and 1:2.17.0-1ubuntu1 500. The candidate version shows which version would be installed. To install Git, simply run:
    sudo apt-get install git

    When the script asks for permission to install Git, press Y.

Installing Git from Github

You can download Git from GitHub to get the latest version of the tool.

  1. With this method, you need an additional Github package before you install Git.
    sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev

    If a confirmation message appears, choose Y.

  2. Once it’s done, visit the Git releases page. Right-click on the tar.gz file of the latest version and copy the link. In this case, we are going to use the latest (at the time writing) v2.23.0 version, so the command should be:
    wget https://github.com/git/git/archive/v2.23.0.tar.gz
  3. After you have downloaded the file, extract it using:
    tar -zxf v2.23.0.tar.gz
  4. When it’s done, head to the extracted directory:
    cd git-2.23.0/

    Now install git locally by running this command one after the other:

    make prefix=/usr/local all
    
    make prefix=/usr/local install

    Note, that the installation can take a few minutes to complete.

2. Configuring Git

Once you have successfully installed Git on Ubuntu, you will need to configure it in order to make it work properly.

  1. On the terminal, replace “user_name” with your real username in the following command:
    git config --global user.name "user_name"

    Remember to include the quotation mark.

  2. Now enter your email address instead of “email@domain.com”:
    git config --global user.email "email@domain.com"

Step 3 – List of Basic Git Commands

Here is a list of useful Git commands to help you get started:

CommandExplanation
Creating Repository
git clone ssh://username@somedomain.com/repo.gitClone an existing repository
git initCreate a new local repository
Working with Local Changes
git statusChange files in the working directory
git diffChange to tracked files
git add .Add all changes to your next commit
git add -pAdd some changes into your next commit
git commit -aCommit all local changes in tracked files
git commitCommit previously staged changes
git commit -amendChange the last commit
Checking Commit History
git logShow all commits
git log -pShow changes over time for a specific commit
git blameSee who changed the commit and when the change happened
Creating Branches and Tags
git branch -avSee all existing branches
git checkoutSwitch to a branch
git branchCreate a new branch based on your current branch
git checkout — track <remote/branch>Create a new branch based on a remote branch
git branch -dDelete a local branch
git tagMark your current commit with a tag
Updating and Publishing
git remote -vList all currently configured remotes
git remote showShow information about a remote
git remote addAdd a new remote repository
git fetchDownload all changes
git pull branchDownload all changes from branches and merge into HEAD
git pushPush changes from local to remote
git branch -dr <remote/branch>Delete a branch on the remote
git push — tagsPublish your tags
Merging and Rebasing
git mergeMerge into current HEAD
git rebaseRebase current HEAD
git rebase — abortAbort a rebase
git rebase — continueContinue a rebase after resolving conflicts
Discarding Changes
git reset — hard HEADDiscard all local changes in your working directory
git checkout HEADDiscard all local changes in a specific file
git revertRevert a specific commit
git reset — hardReset your HEAD to a previous commit by discarding all changes
git resetReset your HEAD to a previous commit but preserve all unstaged changes
git reset — keepReset your HEAD to a previous commit and preserve uncommitted local changes

To see more git commands, use:

git --help

Conclusion

Git is one of the best distributed version control systems in the world. This free tool packs a lot of useful features that help developers manage their code and repositories.

In this tutorial, you have learned how to install Git on Ubuntu 18.04. To summarize, let’s take a look at the steps once again.

  1. Install Git on Ubuntu through APT or GitHub.
  2. Configure your Git by entering your username and email.

Furthermore, we have also added a list of important Git commands that you can use to operate the software.

Don’t forget to leave a comment below if you have any questions!

The Author

Author

Elvinas S. / @elvinas

Elvinas is a senior server administrator at Hostinger. He monitors the infrastructure’s well-being and keeps the uptime at a maximum. Besides server management, web development has always been one of his biggest passions.

Related tutorials

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in VPS
How to Install and Configure ownCloud on CentOS 7 VPS

Close