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!).
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:
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
git init myrepo
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:
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:
To find what's inside, change your current directory to the
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
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:
Now, you can use the following command to track files on git:
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
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"
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:
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!