Git Tutorial
- 1. Introduction to Git & VCS
- 2. Installation & Configuration
- 3. Git Architecture & Concepts
- 4. Basic Workflow (add/commit)
- 5. Git Log & History
- 6. Branching Basics
- 7. Merging & Conflict Resolution
- 8. Git Rebasing
- 9. Undoing Changes
- 10. Working with Remotes
- 11. Git Stashing
- 12. Git Tagging
- 13. Git Ignore & Attributes
- 14. Advanced Git Tools
- 15. Best Practices & Workflows
2. Installation & Configuration
Before you can use Git, you need to install it on your computer and set up your personal configuration options. In this chapter, we will walk through the installation process for Windows, macOS, and Linux, followed by essential setup commands.
Installing Git
1. Installing on Windows
The easiest way to install Git on Windows is to use the official executable installer:
- Go to the official Git download page: git-scm.com/download/win.
- Download the 64-bit Git for Windows Setup installer.
- Run the downloaded `.exe` file and follow the setup wizard. We recommend keeping the default options.
- Ensure you check the option to use "Git Bash" (a powerful bash emulator for Windows command lines).
2. Installing on macOS
There are multiple ways to install Git on macOS:
- Via Xcode Command Line Tools: Open your terminal and run
git --version. If not installed, a prompt will ask you to install the command line developer tools. - Via Homebrew: If you use Homebrew, open the terminal and run:
brew install git
3. Installing on Linux
Use your package manager to install Git on Linux distributions:
- Ubuntu/Debian:
sudo apt-get update sudo apt-get install git - Fedora/Red Hat:
sudo dnf install git
Verifying Installation
To verify that Git has installed successfully, open your Terminal (Mac/Linux) or Command Prompt/Git Bash (Windows) and type:
git --versionThis should print the installed Git version, e.g., git version 2.40.1.
First-Time Git Configuration
Now that Git is installed, you must customize your Git environment. Git provides a tool called git configthat lets you read and set configuration variables that control how Git looks and operates.
Setting Your Identity
The very first thing you should do is set your username and email address. This is critical because every Git commit uses this information, and it is permanently baked into the history:
# Set global username
git config --global user.name "Your Name"
# Set global email address
git config --global user.email "your.email@example.com"Configuring Default Editor
By default, Git uses your system's default editor (often Vi or Vim) for typing commit messages. If you prefer to use VS Code, you can configure it as follows:
git config --global core.editor "code --wait"Configuring Default Branch Name
Historically, Git used master as the default branch name when initializing a new repository. In modern versions, you can easily set your default branch to main globally:
git config --global init.defaultBranch mainChecking Your Configurations
To check all configuration settings Git can find in your environment, use:
git config --listTo check a specific value, run git config <key>, for example:
git config user.nameConfig Levels Explained
Git configuration variables can be stored in three different levels:
| Config Level | Flag | Description |
|---|---|---|
| System | --system | Applies to all users on this system. Stored in /etc/gitconfig. |
| Global | --global | Applies to your user account specifically. Stored in ~/.gitconfig. |
| Local | --local | Specific to a single repository. Stored in .git/config inside the project. |