Configure Git

This guide explains how to set up and configure Git on your local machine. Git is a distributed version control system that allows you to track changes in your code, collaborate with others, and maintain different versions of your projects.

Install Git

  • Install Git based on your operating system:

    1
    2
    3
    4
    5
    6
    7
    8
    
    # For Fedora/RHEL
    sudo dnf install git
      
    # For macOS (using Homebrew)
    brew install git
      
    # For Windows
    # Download from https://git-scm.com/download/win
    
  • Verify the installation:

    1
    
    git --version
    

Installing Git is the first step to using version control. The commands above will install the latest stable version for your operating system.

Configure User Identity

  • Set your name and email globally:

    1
    2
    
    git config --global user.name "Your Name"
    git config --global user.email "[email protected]"
    
  • Verify your configuration:

    1
    
    git config --list
    

Your identity is used in commit messages to identify who made changes. Use the email associated with your GitHub, GitLab, or other Git hosting service.

Set Up Default Editor

  • Configure your preferred text editor:

    1
    2
    3
    4
    5
    6
    7
    8
    
    # For VSCode
    git config --global core.editor "code --wait"
      
    # For Vim
    git config --global core.editor "vim"
      
    # For Nano
    git config --global core.editor "nano"
    

The default editor is used when Git needs you to enter commit messages or resolve conflicts.

Configure Line Endings

  • Set appropriate line ending behavior:

    1
    2
    3
    4
    5
    
    # For macOS/Linux
    git config --global core.autocrlf input
      
    # For Windows
    git config --global core.autocrlf true
    

Proper line ending configuration prevents issues when collaborating across different operating systems.

Set Up SSH for GitHub/GitLab

  • Generate an SSH key pair:

    1
    
    ssh-keygen -t rsa -C "[email protected]"
    
  • Start the SSH agent:

    1
    2
    
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa.pub
    
  • Copy your public key:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    # macOS
    pbcopy < ~/.ssh/id_ed25519.pub
      
    # Linux
    cat ~/.ssh/id_ed25519.pub
    # Then manually copy the output
      
    # Windows (Git Bash)
    cat ~/.ssh/id_ed25519.pub | clip
    

SSH keys provide secure authentication without requiring password entry each time you push or pull. Add the copied public key to your GitHub/GitLab account settings.

Configure Default Branch Name

  • Set the default branch name for new repositories:

    1
    
    git config --global init.defaultBranch main
    

Many organizations have moved away from using “master” as the default branch name.

Set Up Useful Aliases

  • Create shortcuts for common commands:

    1
    2
    3
    4
    5
    
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.ci commit
    git config --global alias.st status
    git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
    

Aliases save time by reducing the need to type long commands. The last alias creates a visually appealing log output.

Verify Configuration

  • View your complete configuration:

    1
    
    git config --list
    

This command shows all your Git settings, helping you verify that everything is configured correctly.