Chapter 2: Getting Started with Git
Chapter Objectives
By the end of this chapter, you will be able to:
- Install Git on Windows, macOS, and Linux operating systems.
- Perform the essential first-time Git setup, including configuring your user identity (name and email).
- Set your preferred text editor for Git operations like writing commit messages.
- Understand the different levels of Git configuration (system, global, local).
- Check your current Git configuration settings.
- Know how to access Git’s built-in help documentation.
Introduction
In Chapter 1, we explored what Version Control Systems are, their importance, the different types, and the fundamental Git concepts of the repository, working directory, and staging area. Now that you understand why Git is a valuable tool, it’s time to get it running on your system and configure it for use.
This chapter will guide you through installing Git on the three major operating systems: Windows, macOS, and Linux. Once installed, we’ll cover the crucial first-time setup steps. These include telling Git who you are (so your contributions can be correctly attributed) and configuring your preferred text editor for writing commit messages. We’ll also look at how Git manages configuration settings and how you can view them. Finally, we’ll touch upon how to get help directly from Git itself, a skill that will be invaluable as you learn more complex commands.
Theory
Why Initial Configuration is Important
Before you start making commits and contributing to projects, Git needs to know who you are. Every Git commit uses this information, and it’s immutably baked into the commits you create. This is crucial for traceability in any project, especially collaborative ones, as it allows everyone to see who made which changes. The two most important pieces of information are your name and email address.
Additionally, Git often needs to open a text editor for you to type messages, most commonly when you write a commit message. By default, Git uses your system’s default editor, which might not be your preference (e.g., it might default to Vi or Vim, which have a steeper learning curve if you’re unfamiliar with them). Configuring your preferred editor upfront can make your Git experience smoother.
Git Configuration Levels
Git stores its configuration settings in plain text files. There are three main levels at which Git configuration can be applied:
System Level (--system
):
- These settings apply to every user on the system and all their repositories.
- The configuration file is typically found in
/etc/gitconfig
on Linux/macOS, orC:\ProgramData\Git\config
on Windows (for Git for Windows). This location might vary depending on your Git installation, specifically if it was installed via a package manager to a different prefix. - You usually need administrator or superuser privileges to edit this file.
- This level is less commonly used for personal settings.
Global Level (--global
):
- These settings apply to you, the current user, and all of your repositories.
- The configuration file is stored in your user’s home directory. This is typically
~/.gitconfig
or~/.config/git/config
on Linux/macOS, andC:\Users\<YourUserName>\.gitconfig
on Windows. - This is the most common level for configuring your personal user name, email, and default editor.
Local Level (--local
or repository-specific):
- These settings apply only to the specific repository you are currently working in.
- The configuration file is stored in the repository’s
.git
directory, specifically at.git/config
. - You can use this to override global settings for a particular project. For example, you might use your work email for work-related repositories and a personal email for personal projects.
Git reads these configuration files in order: system, then global, then local. If a setting is defined in multiple files, the value in the later file (e.g., local) overrides the value from an earlier file (e.g., global).
%%{ init: { "theme": "base", "themeVariables": { "primaryColor": "#DBEAFE", /* Process Nodes - Light Blue */ "primaryTextColor": "#1E40AF", "primaryBorderColor": "#2563EB", "lineColor": "#A78BFA", /* Arrow/connector color - Light Purple */ "textColor": "#1F2937", /* Default text - Dark Gray */ "fontSize": "14px", "fontFamily": "Open Sans", /* Custom colors for specific node types based on prompt */ "mainBkg": "#F9FAFB", /* A light background for the diagram itself if possible */ "nodeBorder": "#1F2937", /* Specific node styles based on provided palette */ "sysNodeFill": "#FEE2E2", /* System Level - Light Red (Check/Validation) */ "sysNodeStroke": "#DC2626", "sysNodeColor": "#991B1B", "globalNodeFill": "#FEF3C7", /* Global Level - Light Amber (Decision) */ "globalNodeStroke": "#D97706", "globalNodeColor": "#92400E", "localNodeFill": "#EDE9FE", /* Local Level - Light Purple (Primary/Start) */ "localNodeStroke": "#5B21B6", "localNodeColor": "#5B21B6", "overrideArrowColor": "#059669" /* Green for override indication */ }, "flowchart": { "htmlLabels": true, "nodeSpacing": 60, "rankSpacing": 80, "curve": "basis" } } }%% graph TD; subgraph Overall Git Configuration direction TB System["<div style='padding:10px;'><b>System Level</b><br>(--system)<br><br>Applies to ALL users & ALL repositories on the machine.<br><br><i>e.g., /etc/gitconfig</i></div>"]; Global["<div style='padding:10px;'><b>Global Level</b><br>(--global)<br><br>Applies to the CURRENT user & ALL their repositories.<br><br><i>e.g., ~/.gitconfig</i></div>"]; Local["<div style='padding:10px;'><b>Local Level</b><br>(--local / repository-specific)<br><br>Applies ONLY to the specific repository.<br><br><i>e.g., MyProject/.git/config</i></div>"]; EffectiveSettings["<div style='padding:10px;'><b>Effective Settings for a Repository</b><br><br>Git reads settings in order:<br>1. System<br>2. Global<br>3. Local<br><br>Later settings <b>override</b> earlier ones.</div>"]; end System -- "⚙️ Inherits from" --> Global; Global -- "⚙️ Inherits from" --> Local; Local -- "<b style='color:#059669;'>OVERRIDES</b> Global & System settings" --> EffectiveSettings; Global -. "<b style='color:#059669;'>OVERRIDES</b> System settings" .-> EffectiveSettings; System -.-> EffectiveSettings; style System fill:#FEE2E2,stroke:#DC2626,stroke-width:2px,color:#991B1B; style Global fill:#FEF3C7,stroke:#D97706,stroke-width:2px,color:#92400E; style Local fill:#EDE9FE,stroke:#5B21B6,stroke-width:2px,color:#5B21B6; style EffectiveSettings fill:#D1FAE5,stroke:#059669,stroke-width:2px,color:#065F46; classDef default fill:#transparent,stroke:#1F2937,stroke-width:1px,color:#1F2937,font-family:'Open Sans';
Getting Help from Git
Git has comprehensive built-in documentation. If you’re unsure about a command, its options, or how it works, you can ask Git for help. There are a few ways to access this:
git help <command>
: Opens the official manual page for the specified command (e.g.,git help config
).git <command> --help
: Does the same asgit help <command>
.git help --all
: Lists all available commands.
Command | Description |
---|---|
git help <command> | Opens the official, detailed manual page for the specified Git command (e.g., git help config). |
git <command> –help | Identical to git help <command>. Also opens the manual page for the command. |
git help –all | Lists all available Git commands along with a brief description of each. Useful for discovering commands. |
git help (without arguments) | Provides a synopsis of the git help command itself and some common Git commands. |
These help pages are usually very detailed and are an excellent resource.
Practical Examples
Let’s get Git installed and configured.
Installing Git
The installation process varies by operating system.
1. Installing on Linux
You can generally install Git using your distribution’s built-in package manager.
- Debian/Ubuntu-based (like Ubuntu, Mint, Pop!_OS):Open a terminal and type:
sudo apt update sudo apt install git
- Fedora/CentOS/RHEL-based (like Fedora, CentOS Stream, Rocky Linux, AlmaLinux):Open a terminal and type:
sudo dnf install git # For Fedora or newer RHEL/CentOS # OR sudo yum install git # For older RHEL/CentOS
- Arch Linux/Manjaro:Open a terminal and type:
sudo pacman -S git
2. Installing on macOS
There are several ways to install Git on macOS:
- Xcode Command Line Tools: The easiest way is often to install the Xcode Command Line Tools. If you try to run
git
from the terminal without it being installed, macOS will usually prompt you to install the command line tools.git --version
If Git is not installed, you should see a dialog box. Click “Install” to download and install the tools, which include Git. Alternatively, you can initiate the installation by running:xcode-select --install
- Homebrew: If you use the Homebrew package manager:
brew install git
- Official Installer: You can also download the latest Git installer for macOS from the official Git website: https://git-scm.com/download/mac
3. Installing on Windows
The primary way to install Git on Windows is using the official Git for Windows installer.
- Go to the official Git website: https://git-scm.com/download/win
- The download for the latest version should start automatically. If not, click the download link.
- Run the installer executable.
- Follow the prompts in the installation wizard. The default options are generally sensible for most users. Key choices during installation include:
- Choosing the default editor: You can select editors like VS Code, Notepad++, Vim, etc.
- Adjusting your PATH environment: The recommended option is usually “Git from the command line and also from 3rd-party software” as it makes Git available in both Git Bash, Command Prompt (CMD), and PowerShell.
- Configuring line ending conversions: The default (“Checkout Windows-style, commit Unix-style line endings”) is usually best for Windows users.
Git for Windows provides:
git
(the command-line tool itself)- Git Bash: A Bash shell environment that provides a Unix-like command-line experience on Windows, including common Unix commands.
- Git GUI: A simple graphical interface for Git (though we’ll focus on the CLI).
Verifying Installation
After installation, open a new terminal window (Terminal on macOS/Linux, Git Bash/CMD/PowerShell on Windows) and verify that Git is installed correctly:
git --version
Expected Output (version number will vary):
git version 2.44.0
# or
git version 2.44.0.windows.1
If this command outputs a version number, Git is installed and accessible from your command line.
First-Time Git Setup: Configuration
Now that Git is installed, let’s configure your user identity and preferred editor. These commands are typically run once per computer.
1. Setting Your User Name and Email
Git associates your name and email address with every commit you make. This is important for tracking who made changes.
Open your terminal and run the following commands, replacing “Your Name” and “youremail@example.com” with your actual name and email address. The --global
flag means these settings will apply to all your Git repositories on this computer.
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
Example:
git config --global user.name "Alice Wonderland"
git config --global user.email "alice@wonderland.io"
There will be no output if these commands are successful.
Warning: Use an email address that you want to be publicly associated with your commits, especially if you plan to contribute to public projects on platforms like GitHub or GitLab.
2. Setting Your Default Text Editor
When Git needs you to enter a message (e.g., for a commit), it will open a text editor. You can configure your preferred editor. Here are examples for some common editors:
- Visual Studio Code (VS Code):
git config --global core.editor "code --wait"
The--wait
flag is important; it tells Git to wait for you to close the file in VS Code before proceeding. - Nano (a simple terminal-based editor, often available on Linux/macOS):
git config --global core.editor "nano"
- Vim (a powerful terminal-based editor, default on many Unix systems):
git config --global core.editor "vim"
- Sublime Text (ensure
subl
command-line tool is installed and in your PATH):git config --global core.editor "subl -n -w"
The-n
opens a new window, and-w
waits for the file to be closed. - Notepad++ (Windows):
# Ensure Notepad++ is in your system PATH. Path might vary. git config --global core.editor "'C:\Program Files\Notepad++\notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Note: Paths with spaces on Windows often require careful quoting. Using Git Bash can sometimes simplify this. If using Git Bash for the Notepad++ example, you might use:git config --global core.editor "/c/Program\ Files/Notepad++/notepad++.exe -multiInst -notabbar -nosession -noPlugin"
Choose the command that corresponds to your preferred editor. If your editor’s command-line launcher is not in your system’s PATH, you’ll need to provide the full path to the executable.
3. (Optional) Setting the Default Branch Name
Historically, the default branch in new Git repositories was named master
. In recent years, the community has largely shifted towards using main
as a more inclusive term. Newer versions of Git (2.28 and later) allow you to configure the default branch name for new repositories created with git init
.
Many Git installations now default to main
. If yours doesn’t, or if you want to explicitly set it, you can use:
git config --global init.defaultBranch main
This ensures any new repository you initialize will have main
as its primary branch.
Configuration Key | Command Example (Global) | Description | Common Values / Notes |
---|---|---|---|
user.name | git config –global user.name “Your Name” | Sets the name that will be associated with your commits. Essential for attribution. | Your full name (e.g., “Alice Wonderland”). |
user.email | git config –global user.email “youremail@example.com” | Sets the email address that will be associated with your commits. Also essential for attribution. | Your primary email address (e.g., “alice@wonderland.io”). |
core.editor | git config –global core.editor “code –wait” | Specifies the default text editor Git will use for operations requiring message input (e.g., commit messages). |
Examples: “code –wait” (VS Code) “nano” (Nano) “vim” (Vim) “subl -n -w” (Sublime Text) Ensure the command correctly invokes your editor and waits for it to close. |
init.defaultBranch | git config –global init.defaultBranch main | Sets the default branch name (e.g., “main” or “master”) for newly initialized repositories using git init. | Modern practice favors main. Older Git versions defaulted to master. |
core.autocrlf (OS-Specific) | Windows: git config –global core.autocrlf true macOS/Linux: git config –global core.autocrlf input |
Handles line ending conversions between Windows (CRLF) and Unix-like systems (LF). |
true (Windows): Convert LF to CRLF on checkout, CRLF to LF on commit. input (macOS/Linux): Convert CRLF to LF on commit, no conversion on checkout. false: No conversion. (Not generally recommended for cross-platform projects). |
4. Checking Your Configuration Settings
To view all your Git configuration settings (local, global, and system combined), use:
git config --list
Expected Output (will include your settings and Git defaults):
user.name=Alice Wonderland
user.email=alice@wonderland.io
core.editor=code --wait
init.defaultbranch=main
credential.helper=manager-core
core.autocrlf=true
... (many other settings) ...
To see where each configuration value is coming from (system, global, or local file), use:
git config --list --show-origin
Expected Output (example):
file:/usr/local/etc/gitconfig credential.helper=osxkeychain
file:/Users/alice/.gitconfig user.name=Alice Wonderland
file:/Users/alice/.gitconfig user.email=alice@wonderland.io
file:/Users/alice/.gitconfig core.editor=code --wait
file:.git/config core.repositoryformatversion=0
...
You can also check a specific key:
git config user.name
Expected Output:
Alice Wonderland
Getting Help
If you want to learn more about the config
command, for example:
git help config
# or
git config --help
This will open the manual page for git config
, detailing all its options and capabilities.
OS-Specific Notes
- Installation: As detailed above, the installation process is the most OS-dependent part.
- Configuration Files Paths:
- System:
- Linux/macOS: Often
/etc/gitconfig
or/usr/local/etc/gitconfig
. - Windows (Git for Windows):
C:\ProgramData\Git\config
or within the Git installation directory (e.g.,C:\Program Files\Git\etc\gitconfig
).
- Linux/macOS: Often
- Global:
- Linux/macOS:
~/.gitconfig
or~/.config/git/config
. - Windows:
C:\Users\<YourUserName>\.gitconfig
.
- Linux/macOS:
- Local: Always
.git/config
within the repository.
- System:
- Default Editor:
- On Linux and macOS, if
core.editor
is not set, Git often defaults tovi
orvim
. - On Windows, Git for Windows installer prompts you to choose a default editor. If somehow unset, it might fall back to a very basic editor or prompt you.
- On Linux and macOS, if
- PATH Environment Variable:
- On Linux and macOS, package managers usually handle adding Git to your PATH.
- On Windows, the Git for Windows installer provides options for PATH modification. If Git commands aren’t recognized in Command Prompt or PowerShell, it’s likely a PATH issue. Re-installing Git and choosing the recommended PATH option usually fixes this.
Tip: Using Git Bash on Windows often provides a more consistent experience with commands and path handling, especially if you’re used to Linux/macOS terminals.
Common Mistakes & Troubleshooting Tips
Git Issue / Error | Symptom(s) | Troubleshooting / Solution |
---|---|---|
Forgetting user.name/email | Commits have incorrect author info (e.g., “username@hostname”) or Git prompts for identity on commit. | Solution: Run git config –global user.name “Your Name” and git config –global user.email “youremail@example.com”. (Amending past commits is advanced). |
Typos in Config Keys/Values | Setting doesn’t take effect. Editor command fails (e.g., editor ‘myeditor –opton’ not found). | Solution: Verify key spelling with git config –list. Ensure editor command is correct and in PATH. Use git config –unset <key> to remove, then set correctly. |
Editor Not Waiting for Input | Git opens editor for commit message, then immediately proceeds, resulting in an empty/aborted commit. | Solution: Use correct “wait” flags for your GUI editor: code –wait (VS Code), subl -n -w (Sublime), etc. Check editor’s CLI documentation. |
Config Level Confusion | Settings don’t apply as expected across repositories, or a specific repo behaves differently. | Solution: Use –global for user-wide settings. Use git config –list –show-origin to see where settings originate (system, global, local). |
‘git’ command not found (Post-installation) | Typing git in CMD/PowerShell (Windows) or terminal gives an error: ‘git’ is not recognized… | Solution (Windows):
|
Incorrect path to editor (especially on Windows) | Git reports it cannot find the configured editor, e.g., error: cannot run C:\Program Files\My Editor\editor.exe: No such file or directory. | Solution:
|
Exercises
- Verify Git Installation and Configure Identity:
- If you haven’t already, install Git on your system following the instructions for your OS.
- Open a new terminal window.
- Run
git --version
to confirm Git is installed and accessible. Note down the version. - Configure your global
user.name
with your actual name. - Configure your global
user.email
with your actual email address. - Run
git config --list
to verify that your name and email are now listed.
- Configure Default Editor and Check Settings:
- Choose your favorite text editor (e.g., VS Code, Nano, Vim, Sublime Text, Notepad++).
- Find the correct command to set it as Git’s
core.editor
globally. - Execute the
git config --global core.editor "your_editor_command"
command. - Use
git config core.editor
to specifically check if the editor setting was applied correctly. - Use
git config --list --show-origin
to see all your settings and where they originate from. Identify youruser.name
,user.email
, andcore.editor
entries and note which file they are listed under.
- Practice Getting Help:
- Use Git’s help system to find information about the
git init
command. - Use Git’s help system to find information about the
git add
command. - Briefly skim the descriptions. What is the primary purpose of each of these commands according to the help text? (No need to fully understand them yet, just practice accessing and reading the help).
- Use Git’s help system to find information about the
Summary
This chapter equipped you with the essentials to get started with Git:
- Installation: You learned how to install Git on Windows, macOS, and Linux.
- Core Configuration:
- Setting
user.name
anduser.email
globally is crucial for commit attribution:git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
- Setting
core.editor
globally customizes the editor for commit messages:git config --global core.editor "your_editor_command"
- Optionally, setting
init.defaultBranch
(e.g., tomain
) for new repositories:git config --global init.defaultBranch main
- Setting
- Configuration Levels: Git uses system, global, and local configuration files, with local settings overriding global, and global overriding system.
- Checking Configuration:
git config --list
shows all active settings.git config --list --show-origin
shows settings and their origin files.git config <key>
shows the value for a specific key.
- Getting Help:
git help <command>
orgit <command> --help
provides detailed documentation for Git commands.
With Git installed and configured, you are now ready to start using it for version control. The next chapter will dive into creating your first repository and making your initial commits.
Further Reading
- Pro Git Book:
- Chapter 1: Getting Started – Installing Git: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
- Chapter 1: Getting Started – First-Time Git Setup: https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup
- Chapter 1: Getting Started – Getting Help: https://git-scm.com/book/en/v2/Getting-Started-Getting-Help
- Official Git Documentation:
git config
command: https://git-scm.com/docs/git-config- Git Downloads Page: https://git-scm.com/downloads
