Introduction

Git is a version control system used in modern software development. It allows multiple developers to work on the same project while tracking changes, revisions, and contributors.

This step by step guide walks you through installing and configuring Git on an Ubuntu 18.04. It will present two (2) installation options, each with its benefits, depending on your needs. The same steps will work for Ubuntu 16.04 and any Ubuntu-based distribution.

installation of Git on Ubuntu header

Prerequisites

  • Access to a user account with sudo or root privileges
  • A server with any Ubuntu release up and running.
  • Access to a command line/terminal window (Ctrl-Alt-T)
  • The apt-get tool, pre-loaded in Ubuntu and other Debian-based distros

Installing Git on Ubuntu

Option 1: Install Git with Default Packages on Ubuntu

Using the apt package management tool is the easiest way to install Git. However, the version in the default repositories may not be the latest release from the developer. If you want to install the latest release, skip down to install from source.

1. To update the packages, launch a terminal window, and enter:

sudo apt-get update

This helps to ensure that you’re working with the latest software versions.

2. To install from the default repositories, enter the following:

sudo apt-get install git

Allow the process to complete.

3. Verify the installation and version by entering:

git ––version

The output should appear as:
Git version 2.17.1

Option 2: Install Git on Ubuntu From Source Code

To use the latest version of Git from the developers, download and install from the original source code.

1. Start by installing the following packages:

sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip

Allow the process to complete.

2. Open a browser window and navigate to the following address:

https://github.com/git/git

3. You can select the version of Git you download by changing the branch to a specific version. Select the Master Branch.

Also at the top, you see a link labeled releases where you can browse the different release versions. It is recommended that you avoid versions labeled with rc. This stands for release candidate, and may not be completely stable.

4. Click the green Clone or download button on the right-hand side. Copy the URL by clicking on the file icon.

5. Switch back to your command prompt and enter the following:

cd /tmp

6. You are now working in the temp directory. Type in the following command:

sudo wget https://github.com/git/git/archive/master.zip -O git.zip

This downloads the Git file, and renames it git.zip.

Note: If you found a package under the releases page, you can substitute the link to that version. For example:

sudo wget https://github.com/git/git/archive/v2.20.1.zip -O git.zip

7. Next, extract the files, enter the command:

unzip git.zip

8. Allow the process to finish. Then, move to the new directory:

cd git-*

9. Then, compile the package with the following command:

make prefix=/usr/local/

10. And once the process is finished, install the software with:

sudo make prefix=/usr/local install

11. Verify the Git installation by entering:

git ––version

The output should appear as:

Git version 2.18.0 (depending on the version you selected)

Configuring Git

Git contains a basic configuration file that holds your information. Setting your username and email address is essential.

1. In a terminal window, enter the following and replace your_name with your name, and email@address.com with your email address.:

git config ––global user.name “your_name”

git config ––global user.email “email@address.com”

2. Verify configuration changes with the command:

git config ––list

The system should display the name and email address you just entered.

Note: If you do not make these edits, you will receive a warning when you commit to Git which makes you go back and revise your commits.

Basic Git Commands

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

  • Find changed files in the working directory: git status
  • Change to tracked files: git diff
  • Add all changes to your next commit: git add
  • Add selected changes into your next commit: git add -p
  • Commit all local changes in tracked files: git commit -a
  • Commit previously staged changes: git commit
  • Change the last commit: git commit -amend
  • List all currently configured remotes: git remote -v
  • View information about a remote: git remote show
  • Add a new remote repository: git remote add
  • Download all changes from a remote repository: git fetch
  • Download all changes from and merge into HEAD: git pull branch

To see more git commands use: git --help

Conclusion

Now you know how to install Git on your Ubuntu system. You also learned configuration and basic Git commands. which will help you manage your codes more effectively.