Skip to content

Git - Environment Setup

Before you can start using Git, you need to install it on your system and perform some initial configuration. This setup is typically a one-time process.

On Debian-based distributions (like Ubuntu):

$ sudo apt update
$ sudo apt install git

On RPM-based distributions (like Fedora, CentOS/RHEL):

# For Fedora
$ sudo dnf install git
# For CentOS/RHEL
$ sudo yum install git

The easiest way is to install Xcode Command Line Tools. If you try to run git from the terminal for the first time, it will prompt you to install them. Alternatively, you can install Git using Homebrew:

$ brew install git

Download and install Git for Windows from https://git-scm.com/download/win. It includes Git BASH (a command-line tool) and a GUI client.

After installation, verify it by opening a terminal (or Git BASH on Windows) and typing:

$ git --version
git version 2.xx.x

Git uses a tool called git config to set configuration variables. These settings can be stored at three levels:

  1. --system: System-wide (/etc/gitconfig). Affects all users and repositories. Requires root/admin rights.
  2. --global: User-specific (~/.gitconfig or ~/.config/git/config). Affects all repositories for the current user. This is the most common level for personal settings.
  3. --local: Repository-specific (.git/config in the current repository). Affects only the current repository. This is the default if no option is specified.

This information is embedded in every commit you make. Set it globally:

$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"

Modern practice is to use main as the default branch name for new repositories. Configure Git to use main:

$ git config --global init.defaultBranch main

Git uses your system’s default editor for commit messages. You can specify a different one (e.g., Vim, Nano, VS Code):

# Example for Vim
$ git config --global core.editor vim
# Example for VS Code (ensure 'code' is in your PATH)
# $ git config --global core.editor "code --wait"

As discussed in the ‘Different Platforms’ chapter, configure line ending handling (example for cross-platform projects, assuming .gitattributes will be used per project for fine-grained control):

# For Windows users
$ git config --global core.autocrlf true
# For macOS/Linux users
$ git config --global core.autocrlf input

When you git pull, Git fetches changes and then merges them. You can configure it to rebase instead of merge:

# To make `git pull` use rebase by default for new branches
$ git config --global pull.rebase true
# Alternatively, to keep the default merge behavior (common for beginners):
# $ git config --global pull.rebase false

Using pull.rebase = true can help maintain a cleaner, more linear project history. However, ensure you understand rebasing before enabling this globally.

Enable color highlighting in Git’s output for better readability:

$ git config --global color.ui auto

Modern Git defaults to simple push behavior, which is safer. It pushes the current branch to its upstream counterpart if they share the same name. You can ensure this is set:

$ git config --global push.default simple

To view all your global Git settings:

$ git config --global --list

This will output your configured settings, for example:

user.name=Your Name
user.email=your.email@example.com
init.defaultbranch=main
core.editor=vim
core.autocrlf=true # (or input)
pull.rebase=true # (or false)
color.ui=auto
push.default=simple

You can also view settings for a specific repository by navigating into it and running git config --local --list.