Introduction Git
This article provides an in-depth, step-by-step guide on how to install and config Git on Ubuntu 22.04, you to take full advantage of its capabilities and streamline your development workflows “How To Install and Configure Git on Ubuntu 22.04”.
In the realm of modern software development, version control is a foundational practice that ensures efficient collaboration, smooth code management, and a clear history of changes.
Table of Contents
Git, a distributed version control system, has revolutionized the way developers work by enabling seamless tracking of changes and collaborative coding. Version control is at the heart of effective software development. It allows developers to track changes to their codebase, work simultaneously on the same project, and revert to previous states if necessary.
Git’s decentralized architecture takes version control a step further by enabling developers to work offline, merge changes effortlessly, and collaborate seamlessly. These practices not only enhance development efficiency but also contribute to better code quality and project management.
Prerequisites
Before we dive into the installation process, there is prerequisite you need to ensure are in place:
- Ubuntu Server: Make sure you have a clean installation of Ubuntu Server (Initial Setup Ubuntu Server 22.04) ready to go. You can deploy this on a physical machine or a virtual environment like VMware or VirtualBox.
Installing Git on Ubuntu 22.04
With Git, you can go back to a particular time and undo changes. With this, you’d agree that Git is a valuable tool under your belt; let’s get on to see how to install git on Ubuntu 22.04!
In this tutorial you will learn:
- How to install Git
- How to set global username
- How to set global email
- How to check git version
There are two basic methods to get Git up and running Ubuntu 22.04: using the Ubuntu package manager or downloading the official Git distribution. Let’s look at them briefly:
- Package Manager: A package manager lets you install software directly from the Ubuntu software repositories. This method guarantees easy maintenance and updates through the package management system.
- Official Git Distribution: Alternatively, you can choose to download and install the official Git distribution directly from the Git project’s website. This method provides the latest version of Git with all its features and enhancements.
Based on your needs, you can use any of the methods.
Option 1: Install Git with the Default Package Manager
Installing Git with a package manager like apt offers you ease of installation; it also allows you easily manage and update Git. The version of Git you get with this method may not be the latest version.
Ubuntu 22.04 should have git installed by default. Run the following command to check if you have git installed:
samm@git:~$ git --version
git version 2.34.1
You can install Git using the following steps with apt :
Step 1: System Update
Before installing any packages, run the apt command below to update and refresh your Ubuntu repository.
samm@git:~$ sudo apt update
Step 2: Install Git Using Apt
Next, install git using apt by running the following command:
samm@git:~$ sudo apt install git
Once the installation is complete, you can start running git commands from the terminal.
Option 2: Install Git from the Source
This method How to Install Git in Ubuntu, though not as simple as the first method, will provide you with the latest version of Git.
Step 1: Install Required Dependencies
Install the dependencies using the following code:
samm@git:~$ sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc
Output:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'zlib1g-dev' instead of 'libz-dev'
gcc is already the newest version (4:11.2.0-1ubuntu1).
gcc set to manually installed.
libexpat1-dev is already the newest version (2.4.7-1ubuntu0.2).
libexpat1-dev set to manually installed.
zlib1g-dev is already the newest version (1:1.2.11.dfsg-2ubuntu9.2).
zlib1g-dev set to manually installed.
The following additional packages will be installed:
cmake-data dh-elpa-helper libjsoncpp25 librhash0
Suggested packages:
cmake-doc ninja-build cmake-format gettext-doc autopoint libasprintf-dev libgettextpo-dev libcurl4-doc libgnutls28-dev libidn11-dev libkrb5-dev libldap2-dev librtmp-dev libssh2-1-dev libssl-doc
The following NEW packages will be installed:
cmake cmake-data dh-elpa-helper gettext libcurl4-gnutls-dev libjsoncpp25 librhash0 libssl-dev
0 upgraded, 8 newly installed, 0 to remove and 66 not upgraded.
Need to get 10.8 MB of archives.
After this operation, 48.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
At some point, you’ll be asked if you would like to continue, type “Y” to indicate yes and press enter.
Step 2: Download the Git Source Code
Once the packages have been installed, you can go on to download Git. You can get the download link for the latest version from the download page.
To download the latest version of Git as at the time of this writing, run the following code:
samm@git:~$ wget https://www.kernel.org/pub/software/scm/git/git-2.41.0.tar.gz -O git.tar.gz
Output :
--2023-08-06 05:25:36-- https://www.kernel.org/pub/software/scm/git/git-2.41.0.tar.gz
Resolving www.kernel.org (www.kernel.org)... 145.40.73.55, 2604:1380:40e1:4800::1
Connecting to www.kernel.org (www.kernel.org)|145.40.73.55|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.41.0.tar.gz [following]
--2023-08-06 05:25:36-- https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.41.0.tar.gz
Resolving mirrors.edge.kernel.org (mirrors.edge.kernel.org)... 147.75.48.161, 2604:1380:40f1:3f00::1
Connecting to mirrors.edge.kernel.org (mirrors.edge.kernel.org)|147.75.48.161|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10791798 (10M) [application/x-gzip]
Saving to: ‘git.tar.gz’
git.tar.gz 100%[=====================================================================================================================>] 10.29M 293KB/s in 35s
2023-08-06 05:26:12 (299 KB/s) - ‘git.tar.gz’ saved [10791798/10791798]
This will download the git source code archive for version 2.41.0 and save it as git.tar.gz in the current directory.
After Git has been downloaded, extract the contents of the file using:
samm@git:~$ tar -zxf git.tar.gz
Step 3: Compile and Install Git
Next, change from the current directory to the extracted git source code directory by running the following in the terminal:
samm@git:~$ cd git-2.41.0/
Once in the extracted git source code directory, you can proceed with the installation of Git. Run the following commands to compile the git source code and install git and also restart the bash shell to save the changes and ensure that the changes are reflected in the current shell session.
Setting up Git on Ubuntu 22.04
Once Git is installed, it is recommended that you make some configurations. In particular, the identification associated with your commits. Rather than having to make this configuration for each repository, it’s possible to make a global configuration using a .gitconfig file located in the root folder of the home user.
Step 1: Add a user and email
You can add a global username and email address for Git on your system by running the following:
This file can be made manually or by using the config option with the git command:
git config --global user.name "Firstname Lastname" git config --global user.email "[email protected]"
samm@git:~$ git config --global user.name "tech_sammlinux"
samm@git:~$ git config --global user.email "[email protected]"
Adding these settings will establish consistent authorship across your Git repositories. It helps identify who made specific changes and enables proper collaboration and communication within a team or open-source community.
Step 2: Verify Configuration
Display your .gitconfig file in your home directory:
samm@git:~$ cat .gitconfig
Output :
[user]
email = [email protected]
name = tech_sammlinux
Step 3: Git Initialization
You can initialize an existing project folder or make a new directory to initialize Git inside it. create a new directory with mkdir command.
samm@git:~$ mkdir TUTORIAL
Get inside that folder by issuing the following command:
samm@git:~$ cd TUTORIAL/
samm@git:~/TUTORIAL$
The following command will initialize the Git repository in the “TUTORIAL” directory:
samm@git:~/TUTORIAL$ git init
Output :
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /home/samm/TUTORIAL/.git/
The output shows that the Git repository has been initialized in the “TUTORIAL” directory.
Let’s check the status of Git inside the “TUTORIAL” directory.
samm@git:~/TUTORIAL$ git status
Output :
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
The command shows that the Git is working inside the “TUTORIAL” and there are no commits encountered yet.
After doing so, you can observe the changes in the Git configuration list as follows:
samm@git:~$ git config --list
Output :
/home/samm/TUTORIAL
[email protected]
user.name=samuelsiahaan
Conclusion
You’ve successfully installed and configured Git on your Ubuntu 22.04 system. Git is now ready for use, allowing you to manage and track changes in your projects effectively. Whether you’re a developer, a student, or someone interested in version control, Git is an essential tool that can simplify and enhance your workflow. Start using Git today to keep track of your project’s history and collaborate seamlessly with others. Happy coding!
Also Read Our Other Guides :
- How To Install Jenkins on Ubuntu 22.04
- How To Use Git Version Control on Linux: For Beginner
- How To Install VirtualBox 7.0 on Ubuntu 22.04
- How To Install Vagrant on Ubuntu 22.04
- How To Install and Configure Ansible on Rocky Linux 9
- How To Create AWS CloudFront: A Step-by-Step Guide
- How To Install Python 3.11 from Source on Ubuntu 22.04
- How To Install Python 3.11 on Rocky Linux 9
Finally, now you have learned How To Install and Config Git on Ubuntu 22.04.