Apr 05, 2022
How to Install Git on Ubuntu 18.04: The Ultimate Guide
If you are a developer or learning to be one, you might already know how important a version control system is. It helps you keep track of every change that happens to your code. Here, we will be talking about one of the best version control systems available right now — Git.
Why Use Git?
Git can be considered the most popular version control system to date. With it, you can manage your software code by monitoring changes, reverting back to previous versions of your code, or creating new branches for alternative code.
We will list several advantages of using Git, which makes it such an amazing tool, such as:
- Branching and merging — Git allows you to create a branch of your code when you need to split your work into smaller parts. This will also help you divide your tasks based on the level of importance. What’s great, merging the code back only takes seconds.
- Small and fast — the tool is lightweight and easy to use because most of the operations can happen locally on your computer.
- Distributed and secure — as a distributed version control system, you don’t have to worry about data loss. That’s because every user owns a copy of the repository. Moreover, every file is checksummed to prevent corrupt or incorrect data.
- Staging area — this feature makes Git stand out from the rest. It acts as an intermediate area to review and format every commit of your repository.
- Open-source and free — Git is a free tool and is maintained by a strong and dedicated community.
What’s great, developers are not the only ones that can benefit from Git. Other fields of work, such as marketing, customer support, and so on are encouraged to implement Git in their projects.
How to Install Git on Ubuntu 18.04
There are two essential steps that you need to follow — installing Git and configuring it on your Linux computer. However, before we begin, pay attention to these prerequisites first.
Make sure that your computer is running on Ubuntu 18.04, codenamed Bionic Beaver. Also, you have to log in to your Linux as root or a user with sudo privileges. If you want to install Git on any other operating system, feel free to read our Git tutorial.
1. Installing Git on Ubuntu
There are two methods to install Git on Ubuntu. We’ll break them one by one and you can choose which one works best for you. Remember, both options require you to use the Linux terminal.
Installing Git With APT
Ubuntu 18.04 already contains Git in default repositories. You can easily install it using the APT package manager.
- Firstly, update the repository by running the following command:
sudo apt-get update
- Note that the version in the repositories might not be the newest one. You can check the available versions with:
apt-cache policy git
Here’s an example of the output:
git: Installed: (none) Candidate: 1:2.17.1-1ubuntu0.4 Version table: 1:2.17.1-1ubuntu0.4 500 500 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages 500 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages 1:2.17.0-1ubuntu1 500 500 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages
- There are two available versions – 1:2.17.1-1ubuntu0.4 500 and 1:2.17.0-1ubuntu1 500. The candidate version shows which version would be installed. To install Git, simply run:
sudo apt-get install git
When the script asks for permission to install Git, press Y.
Installing Git from Github
You can download Git from GitHub to get the latest version of the tool.
- With this method, you need an additional Github package before you install Git.
sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
If a confirmation message appears, choose Y.
- Once it’s done, visit the Git releases page. Right-click on the tar.gz file of the latest version and copy the link. In this case, we are going to use the latest (at the time writing) v2.23.0 version, so the command should be:
- After you have downloaded the file, extract it using:
tar -zxf v2.23.0.tar.gz
- When it’s done, head to the extracted directory:
Now install git locally by running this command one after the other:
make prefix=/usr/local all make prefix=/usr/local install
Note, that the installation can take a few minutes to complete.
2. Configuring Git
Once you have successfully installed Git on Ubuntu, you will need to configure it in order to make it work properly.
- On the terminal, replace “user_name” with your real username in the following command:
git config --global user.name "user_name"
Remember to include the quotation mark.
- Now enter your email address instead of “firstname.lastname@example.org”:
git config --global user.email "email@example.com"
Step 3 – List of Basic Git Commands
Here is a list of useful Git commands to help you get started:
|git clone ssh://firstname.lastname@example.org/repo.git||Clone an existing repository|
|git init||Create a new local repository|
|Working with Local Changes|
|git status||Change files in the working directory|
|git diff||Change to tracked files|
|git add .||Add all changes to your next commit|
|git add -p||Add some changes into your next commit|
|git commit -a||Commit all local changes in tracked files|
|git commit||Commit previously staged changes|
|git commit -amend||Change the last commit|
|Checking Commit History|
|git log||Show all commits|
|git log -p||Show changes over time for a specific commit|
|git blame||See who changed the commit and when the change happened|
|Creating Branches and Tags|
|git branch -av||See all existing branches|
|git checkout||Switch to a branch|
|git branch||Create a new branch based on your current branch|
|git checkout — track <remote/branch>||Create a new branch based on a remote branch|
|git branch -d||Delete a local branch|
|git tag||Mark your current commit with a tag|
|Updating and Publishing|
|git remote -v||List all currently configured remotes|
|git remote show||Show information about a remote|
|git remote add||Add a new remote repository|
|git fetch||Download all changes|
|git pull branch||Download all changes from branches and merge into HEAD|
|git push||Push changes from local to remote|
|git branch -dr <remote/branch>||Delete a branch on the remote|
|git push — tags||Publish your tags|
|Merging and Rebasing|
|git merge||Merge into current HEAD|
|git rebase||Rebase current HEAD|
|git rebase — abort||Abort a rebase|
|git rebase — continue||Continue a rebase after resolving conflicts|
|git reset — hard HEAD||Discard all local changes in your working directory|
|git checkout HEAD||Discard all local changes in a specific file|
|git revert||Revert a specific commit|
|git reset — hard||Reset your HEAD to a previous commit by discarding all changes|
|git reset||Reset your HEAD to a previous commit but preserve all unstaged changes|
|git reset — keep||Reset your HEAD to a previous commit and preserve uncommitted local changes|
To see more git commands, use:
Git is one of the best distributed version control systems in the world. This free tool packs a lot of useful features that help developers manage their code and repositories.
In this tutorial, you have learned how to install Git on Ubuntu 18.04. To summarize, let’s take a look at the steps once again.
- Install Git on Ubuntu through APT or GitHub.
- Configure your Git by entering your username and email.
Furthermore, we have also added a list of important Git commands that you can use to operate the software.
Don’t forget to leave a comment below if you have any questions!