Git Ubuntu
How To

Install Git on Ubuntu and Configure it

Sagar Sharma
Sagar Sharma

Table of Contents

Being the most popular open-source version control system, Git is one of the most crucial skills that you can adopt for software development.

And in this guide, I will walk you through how you can install git on Ubuntu with basic configuration.

How to install Git on Ubuntu

Git is available in the default repository of almost every Linux distribution including Ubuntu (obviously!).

Ubuntu 18.04 Review of New Features
Ubuntu 18.04 Review of New Features

So it is supposed to be a straightforward installation by the following command:

sudo apt install git

Once done, you can use the --version option with the git command to check the installed version:

git --version
check git version in ubuntu

Now, let's jump to the basic configuration.

How to create a Git repository on Ubuntu

To create a git repository, you'd need to append the desired name of the directory with the git init command:

git init repository_name

Here, I created a repository named myrepo:

git init myrepo
create git repository in ubuntu

As you can see, it created an empty directory inside my home directory.

But on the surface, it looks like any other normal directory that you create using the mkdir command:

list files and directories in current directory using the ls command in ubuntu

But there is a hidden directory named .git which contains all the necessary information which separates it from any other local repository.

And you can use the ls command to show hidden files:

ls -la
find hidden files in ubuntu using the ls command

To find what's inside, change your current directory to the .git:

cd .git

And use the ls command to list the contents of a directory:

ls 
list contents of current directory in ubuntu using the ls command

How to configure Git on Ubuntu

Now, let's create an identity (a username) associated with all the commits that you are going to make on the system.

There are two types of identity used for git: global and specific to a single repository.

So if you want to use a single identity for every repository, follow the given command syntax:

git config --global user.name "Enter_username"
git config --global user.email "Enter_email"

But if you want to set identities for a specific directory, you'd have to change to that directory.

So if I want to set an identity for a repository named myrepo, First, I will use the cd command to change to myrepo:

cd myrepo

Now, you can use the following command to set a username and email to that specific git repository:

git config user.name "Enter_username"
git config user.email "Enter_email"

How to commit a file in Git

Here, I will create a simple text file to demonstrate how to commit a file in git.

I have used the nano text editor to create and edit text files here:

nano simple.txt
use nano text editor to create and edit text files in ubuntu

Once done, save the changes and exit from the nano text editor.

Now, you can use the following command to track files on git:

git status
check git commit status in ubuntu

And as you can see, it is not tracking the file.

So you can conclude that just saving a file in the git repository won't make it a part of the git repository and the file will be handled as a standard Linux file that you usually have locally.

This means, you manually have to tell git that you want this specific file to be a part of a version control system.

First, you will have to add this file to the part of the commit that you are currently working on.

This can be done using the git add command:

git add filename
add file to current commit in git

And as you can see, it showed my file with green color (a good sign) indicating that the change can be committed.

Now, you can commit with a simple explanation of changes. But it is my first commit so, so I'm going with something simple:

git commit -m "My first commit on simple.txt"
commit a file in git using ubuntu

And if things go as planned, it should repeat the message by notifying you that file was changed.

Now, if you check the status, it will show you something like this:

nothing to commit, working tree clean in git

That's it!

Wrapping Up

This was a simple guide by my side on how you can install and perform basic configuration on git over the Ubuntu platform.

Did I miss anything? Or is there anything else that you want me to cover? I'd love to hear your opinions in the comments!