Skip
Arish's avatar

8. Installing Git on Linux


Installing Git on Linux

Git is available in the package repositories of most Linux distributions.

Ubuntu / Debian

bash
1# Update package list
2sudo apt update
3
4# Install Git
5sudo apt install git
6
7# Verify installation
8git --version

Fedora

bash
1# Install Git
2sudo dnf install git
3
4# Verify installation
5git --version

CentOS / RHEL

bash
1# Install Git
2sudo yum install git
3
4# Verify installation
5git --version

Arch Linux

bash
1# Install Git
2sudo pacman -S git
3
4# Verify installation
5git --version

openSUSE

bash
1# Install Git
2sudo zypper install git
3
4# Verify installation
5git --version

Install from Source (Latest Version)

If you need the latest version:

bash
1# Install dependencies
2sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext
3
4# Download source
5wget https://github.com/git/git/archive/refs/tags/v2.43.0.tar.gz
6
7# Extract
8tar -xzf v2.43.0.tar.gz
9cd git-2.43.0
10
11# Compile and install
12make prefix=/usr/local all
13sudo make prefix=/usr/local install

Configure Git

After installation, configure your identity:

bash
1# Set your name
2git config --global user.name "Your Name"
3
4# Set your email
5git config --global user.email "your.email@example.com"

Verify Configuration

bash
1git config --list

Set Default Editor

bash
1# For VS Code
2git config --global core.editor "code --wait"
3
4# For Vim (default on most Linux systems)
5git config --global core.editor "vim"
6
7# For Nano
8git config --global core.editor "nano"

Set Default Branch Name

bash
1git config --global init.defaultBranch main

Configure SSH for GitHub

Generate an SSH key:

bash
1# Generate SSH key
2ssh-keygen -t ed25519 -C "your.email@example.com"
3
4# Start SSH agent
5eval "$(ssh-agent -s)"
6
7# Add key to agent
8ssh-add ~/.ssh/id_ed25519
9
10# Copy public key
11cat ~/.ssh/id_ed25519.pub

Add the public key to your GitHub account (Settings → SSH Keys).

Test Your Installation

bash
1# Create a test folder
2mkdir git-test
3cd git-test
4
5# Initialize a repository
6git init
7
8# Create a test file
9echo "Hello Git" > test.txt
10
11# Stage and commit
12git add test.txt
13git commit -m "Initial commit"
14
15# Success!
16[main (root-commit) abc1234] Initial commit
17 1 file changed, 1 insertion(+)
18
19# Clean up
20cd ..
21rm -rf git-test

Useful Git Configuration

Enable Colors

bash
1git config --global color.ui auto

Set Pull Behavior

bash
1# Rebase by default when pulling
2git config --global pull.rebase true

Create Useful Aliases

bash
1git config --global alias.st status
2git config --global alias.co checkout
3git config --global alias.br branch
4git config --global alias.ci commit
5git config --global alias.lg "log --oneline --graph --all"

Troubleshooting

Permission Denied

If you get permission errors:

bash
1# Check file permissions
2ls -la ~/.gitconfig
3
4# Fix permissions if needed
5chmod 644 ~/.gitconfig

Old Version in Repository

If your distro has an old version:

bash
1# Add Git PPA (Ubuntu)
2sudo add-apt-repository ppa:git-core/ppa
3sudo apt update
4sudo apt install git

Summary

Git is now installed on your Linux system! Key points:

  1. Use your package manager for easy installation
  2. Configure your identity
  3. Set up SSH for GitHub authentication
  4. Create aliases for common commands

You're ready to start using Git!