Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Install Git on Linux
Git is a popular open-source version control system that tracks changes in files and coordinates work among multiple developers. Unlike traditional version control systems like CVS or SVN, Git uses a distributed model where each developer has a complete copy of the project history. This article covers the basic steps for installing Git on Linux systems, configuring it, and creating your first repository.
Git vs Traditional Version Control Systems
Traditional version control systems store data as a list of files with changes made to each file over time. Git takes a different approach by thinking of data as snapshots of a filesystem. Every time you commit changes, Git captures a snapshot of all your files at that moment. If files haven't changed, Git doesn't store duplicate data − it simply links to the previous identical snapshot, making it highly efficient.
Installing Git from Package Manager
The easiest way to install Git on most Linux distributions is through the package manager. For Debian/Ubuntu-based systems, use the following command −
$ sudo apt-get install git
The installation output will look similar to this −
tp@linux:~$ sudo apt-get install git [sudo] password for tp: Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: git-man liberror-perl Suggested packages: git-daemon-run git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-arch git-bzr git-cvs git-mediawiki git-svn The following NEW packages will be installed: git git-man liberror-perl 0 upgraded, 3 newly installed, 0 to remove and 286 not upgraded. Need to get 3,421 kB of archives. After this operation, 21.9 MB of additional disk space will be used. Do you want to continue? [Y/n] y
For other distributions, use their respective package managers −
| Distribution | Command |
|---|---|
| Red Hat/CentOS/Fedora |
sudo yum install git or sudo dnf install git
|
| Arch Linux | sudo pacman -S git |
| openSUSE | sudo zypper install git |
Installing Git from Source
For the latest features or custom installations, you can compile Git from source. First, install the required dependencies −
$ sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev asciidoc xmlto docbook2x
Then download and compile Git from the official repository following the instructions in the source package.
Initial Configuration
After installation, verify Git is installed correctly −
$ whereis git
git: /usr/bin/git /usr/bin/X11/git /usr/share/man/man1/git.1.gz
Check the installed version −
$ git --version
git version 2.34.1
Setting Up User Information
Configure your identity for Git commits −
$ git config --global user.name "Your Name" $ git config --global user.email "your.email@example.com"
Verify your configuration −
$ git config --list
user.name=Your Name user.email=your.email@example.com
This information is stored in ~/.gitconfig file in your home directory.
Creating Your First Repository
To create a new Git repository, navigate to your project directory and initialize it −
$ cd /path/to/your/project $ git init
Initialized empty Git repository in /path/to/your/project/.git/
This creates a .git directory containing all the repository metadata.
Adding and Committing Files
Add files to your repository −
$ git add filename.txt # Or add all files $ git add .
Commit your changes −
$ git commit -m "Initial commit: Add project files"
[master (root-commit) a1b2c3d] Initial commit: Add project files 2 files changed, 15 insertions(+) create mode 100644 README.md create mode 100644 main.py
Common Git Commands
| Command | Purpose |
|---|---|
git status |
Show working directory status |
git log |
View commit history |
git diff |
Show changes between commits |
git clone <url> |
Clone a remote repository |
Conclusion
Installing Git on Linux is straightforward using package managers, and the initial setup requires only basic user configuration. Once installed, you can create repositories, track file changes, and collaborate with other developers. Git's snapshot-based approach makes it a powerful and efficient version control system for any project size.
