Git - Environment Setup
Git - Environment Setup
Section titled “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.
Installation
Section titled “Installation”On Debian-based distributions (like Ubuntu):
$ sudo apt update$ sudo apt install gitOn RPM-based distributions (like Fedora, CentOS/RHEL):
# For Fedora$ sudo dnf install git
# For CentOS/RHEL$ sudo yum install gitThe 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 gitWindows
Section titled “Windows”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 --versiongit version 2.xx.xInitial Configuration
Section titled “Initial Configuration”Git uses a tool called git config to set configuration variables. These settings can be stored at three levels:
--system: System-wide (/etc/gitconfig). Affects all users and repositories. Requires root/admin rights.--global: User-specific (~/.gitconfigor~/.config/git/config). Affects all repositories for the current user. This is the most common level for personal settings.--local: Repository-specific (.git/configin the current repository). Affects only the current repository. This is the default if no option is specified.
User Identity
Section titled “User Identity”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"Default Branch Name
Section titled “Default Branch Name”Modern practice is to use main as the default branch name for new repositories. Configure Git to use main:
$ git config --global init.defaultBranch mainDefault Editor
Section titled “Default Editor”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"Line Endings
Section titled “Line Endings”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 inputPull Behavior (Rebase vs. Merge)
Section titled “Pull Behavior (Rebase vs. Merge)”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 falseUsing pull.rebase = true can help maintain a cleaner, more linear project history. However, ensure you understand rebasing before enabling this globally.
Color Highlighting
Section titled “Color Highlighting”Enable color highlighting in Git’s output for better readability:
$ git config --global color.ui autoDefault Push Behavior
Section titled “Default Push Behavior”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 simpleListing Git Settings
Section titled “Listing Git Settings”To view all your global Git settings:
$ git config --global --listThis will output your configured settings, for example:
user.name=Your Nameuser.email=your.email@example.cominit.defaultbranch=maincore.editor=vimcore.autocrlf=true # (or input)pull.rebase=true # (or false)color.ui=autopush.default=simpleYou can also view settings for a specific repository by navigating into it and running git config --local --list.