Git is one of the most popular version control systems used by software developers in the world. It is used in many type of projects from open-source to commercial and from small to big codebase.
The author of Git is Linus Torvalds - the creator of the Linux kernel. It helps developers to collaborate on their projects, keep track of the code changes, create new branches, revert to previous versions and so on.
This tutorial shows the ways to install and configure Git on Ubuntu 20.04. Also, learn basic git commands to start with.
1) Install Git using APT
Git by default is available on Ubuntu 20.04. You can use the apt command to install git from the repository.
The following command installs the latest version available in the APT repository.
$ sudo apt update $ sudo apt install git
Verify installed Git version by:
$ git version
Output:
git version 2.25.1
APT usually doesn't provide the latest version of packages but a widely-used stable version. To install the latest version we follow the next section.
2) Install Git from Source
If you want to install Git in a more flexible way, you can compile it from the source code. It will take longer time to complete but it allows you to install the latest Git release and customize the installation.
First, install all of the dependencies packages to build Git on your Ubuntu 20.04:
$ sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc
Next, go to the Git project's mirror on Github and download the latest version of Git tarball file in .tar.gz
. At the time of writing this article, the latest version is v2.30.0. You can download it by the following command:
$ wget https://github.com/git/git/archive/v2.30.0.tar.gz
When the download is complete, extract the source files to /opt
directory:
$ sudo tar xf v2.30.0.tar.gz -C /opt $ ls /opt git-2.30.0
Then, move into the Git source code directory:
$ cd /opt/git-2.30.0
Now, run the following commands to compile and install Git:
$ sudo make prefix=/usr/local all $ sudo make prefix=/usr/local install
Once the installation is done, verify the Git version:
$ git version
Output:
git version 2.30.0
Configure Git
After you successfully install your desired Git version, you should configure your git username and email. This information will be embedded in commit messages whenever you commit source code to git repositories.
In order to set the global username and email, run the following commands:
$ git config --global user.name "Your Name" $ git config --global user.email "youremail@example.com"
Sometimes, we also configure the core editor using to write the commit messages, for example: using vim
as editor.
$ git config --global core.editor "vim"
The configuration settings will be created and located in ~/.gitconfig
file:
[user] name = Your Name email = youremail@example.com [core] editor = vim
Some basic Git commands
Let's learn basic git commands to start using git.
Create a new local repository
In order to create a new local git repository, run the following command:
$ mkdir foo $ cd foo $ git init Initialized empty Git repository in /home/ubuntu/foo/.git/
Create a working copy of a local repository
If you want to copy of a local repository to another location, run:
$ git clone /path/to/repository
For a remote server, use:
$ git clone username@host:/path/to/repository
Add one or more files
In order to let Git track a file, you have to run the following commands:
$ git add ${filename}
List the files you've changed and those you still need to add or commit
You can get the working tree status by running:
$ git status
Output:
On branch master No commits yet Changes to be committed: (use "git rm --cached ..." to unstage) new file: README
How to commit
Once the files were added, you can commit them to staging area:
$ git commit -m "your commit message"
Output:
[master (root-commit) 9a07b1d] Commit message 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README
Connect to a remote repository
Sometimes, you need to connect to a remote repository located somewhere (github, gitlab and so on). You can run the following command:
$ git remote add origin ${server}
List all currently configured remote repositories
In order to list all the configured remote repositories, run the following command:
$ git remote -v
Conclusion
Git is a really powerful collaboration tool for any developer. This tutorial has gone through all steps of installing and configuring Git on Ubuntu 20.04. Thank you for reading and please leave your suggestion in the below comment section.