Install and Configure Git Version Control on Ubuntu

In this tutorial I will show you how to install Git on Ubuntu and how to set up a Git repository on Ubuntu. Git is the most popular open-source version control management system. Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. The Git Version Control system allows you to undo changes that you made to your files or restore from a previous known stage.

Git was initially created by Linus Torvalds, the very same person who created the Linux Kernel and first released in 2005 to host all development files for the Linux kernel. Since then Git has become the de facto source-code control system for software developers around the world.

This tutorial we will start by installing git on Ubuntu 18.04 and then we will see how to configure and initialize a new repository.

How to Install Git on Ubuntu 18.04/16.04

The first step is to install Git package. For Ubuntu users, Git is available from the default apt repository. To install Git on Ubuntu 18.04, open the terminal and run the following command:

sudo apt-get update
sudo apt-get install git

Once installation completes, you can check the Ubuntu Git version with the following command.

git --version

Configure Git

After installing Git we need to setup some basic configurations, including our name and email address. We do this by using the git config command.

To configure Git with your name and email, type the following Git commands on the command line.

git config --global user.name "Your Name"
git config --global user.email "email@example.com"

You may also want to set default text editor for Git on Ubuntu, because Some git commands such as commit and tag, allows you to edit messages by launching a text editor.

git config --global core.editor "vim"

To list All configurations, Type:

git config --list --global

All these configurations are stored in .gitconfig file in your Ubuntu home directory.

Creating a new repository on Ubuntu

The git init command is used for creating a new, empty repository in the local directory. A Git repository is a specialized storage area of your project or folder in which you can keep track of your work.

To create a new repository, perform the following steps:

  1. Open your terminal and cd into the folder where you want to create a new repository:

    cd project1
  2. Next, initialize the new repository with git init command:

    git init

    When the init command is run, a new subdirectory named ".git" is created in the directory where the command was run. After the initialization, if you run ls -la command you will see there is new folder named ".git" inside your working directory. This directory contains all of your necessary repository files and repository skeleton.

    Creating a new git repository on Ubuntu
  3. After you create a new file or modify existing files, First you need to add the file to the index using the git add command:

    git add file_name
  4. Then, Commit the changes using the commit command:

    git commit -m "Your Comment"

To view the current status of the repository run the status command:

git status

The status command will tell you which files need to be added to the index and which files need to be committed to the repository.

Git Basic Commands

All the time when you work with Git you use the git command, So there are a few basic commands you should be familiar with.

Command Description
git config Set repository or global options (e.g. Name, email, default text editor, etc.).
git init Initialize new git repository.
git status View the status of a Git repository.
git add Add changes to the index and prepares the content staged for the next commit.
git commit Commit changes to the repository.
git rm Remove files from the index.
git clone Download an existing Git repository from a remote URL.

Summary

In this tutorial we learned how to install git on Ubuntu 18.04. Git Version control system is mainly used by the software developers and programmers, but as a system administrator you can use git to keep track of your server configuration files.