⚙️ Git Installation and Initial Setup
Concepts in hand, it's time to put the tool on your machine. This lesson installs Git on any operating system, verifies it, and — just as importantly — configures it so your identity, editor, and defaults are right before you make your very first commit.
🎯 Learning Objectives
By the end of this lesson, you will be able to:
- Install Git on Windows, macOS, or Linux and verify the version
- Set your user identity and understand Git's three configuration levels
- Apply the recommended settings: default branch, editor, line endings, and helpful aliases
- Create a global
.gitignoreso junk files never sneak into a repo - Explain the three states (modified, staged, committed) that structure all Git work
Estimated Time: 25–35 minutes • Difficulty: Beginner
Hands-on: Configure Git from scratch and create your first repository and commit.
In This Lesson
Why Git, Specifically?
Git is the distributed version control system Linus Torvalds built in 2005 for Linux kernel development. Its original design goals still explain why it won:
- Speed: most operations are local, so they're near-instant even on huge projects.
- Simple internal model: a content-addressed store of snapshots, which makes it fast and tamper-evident.
- Cheap branching & merging: non-linear development is the normal case, not a special one.
- Fully distributed: no mandatory central server; every clone is complete.
- Scale: it handles thousands of contributors on a single codebase.
Add the ecosystem — GitHub, GitLab, Bitbucket, and deep editor integration — and Git is the near-universal default. Installing it is your entry ticket to nearly every modern codebase.
📖 Key Term
Git: a free, open-source distributed version control system that stores project history as a chain of content-addressed snapshots, enabling fast local operations and cheap branching.
Installing Git by OS
Git runs everywhere. Find your operating system below, run the install, and then verify.
Windows
The recommended route is Git for Windows, which bundles Git, Git Bash, and the Credential Manager. Download the installer from git-scm.com/download/win and accept the defaults, with two choices worth noting:
- PATH option: choose "Git from the command line and also from 3rd-party software."
- Line endings: choose "Checkout Windows-style, commit Unix-style line endings."
Prefer a Linux environment on Windows? Install WSL (Windows Subsystem for Linux) and use its package manager instead:
# In an elevated PowerShell, install WSL + Ubuntu:
wsl --install
# Then, inside your Ubuntu shell:
sudo apt update
sudo apt install git
macOS
The cleanest option is Homebrew:
# Install Homebrew first if you don't have it:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Then install Git:
brew install git
Alternatively, running any git command triggers macOS to offer the Xcode Command Line Tools, which include Git. The official installer at git-scm.com/download/mac also works.
Linux
Use your distribution's package manager:
# Debian / Ubuntu
sudo apt update && sudo apt install git
# Fedora
sudo dnf install git
# Arch
sudo pacman -S git
# RHEL / CentOS
sudo yum install git
Verify the install
On any OS, open a terminal and run:
git --version
Expected output (your version will differ):
git version 2.44.0
If you see a version number, Git is installed and on your PATH. If the command isn't found, jump to Troubleshooting.
Initial Configuration
Git reads settings from three levels, each overriding the one above it, so a repo setting beats your user setting, which beats the system default:
(all users)"] A --> C["Global — ~/.gitconfig
(your account)"] A --> D["Local — .git/config
(one repository)"] B -. overridden by .-> C C -. overridden by .-> D
Set your identity (do this first)
Git stamps every commit with a name and email. Use the email tied to your GitHub/GitLab account:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Recommended settings
These four make Git noticeably more pleasant and are worth setting once, globally:
# Name the initial branch "main" (modern default)
git config --global init.defaultBranch main
# Use VS Code as the editor for commit messages (--wait is important)
git config --global core.editor "code --wait"
# Line-ending handling: "true" on Windows, "input" on macOS/Linux
git config --global core.autocrlf true # Windows
git config --global core.autocrlf input # macOS / Linux
# Cache credentials so you don't retype them constantly
git config --global credential.helper manager # Windows (Git Credential Manager)
git config --global credential.helper osxkeychain # macOS
Handy aliases
Aliases turn frequent commands into short ones:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --decorate --all"
Now git st runs git status, and git lg draws a compact, graphical history.
Inspect your configuration
git config --list # every active setting
git config user.name # one specific value
git config --show-origin user.email # which file defined it
⚠️ The most common first-time mistake
Skipping user.email, or setting it to an address your Git host doesn't recognize. Your commits then show up as an "unknown" author on GitHub. Set it now, and match it to your account.
A Global .gitignore
Some files should never be committed to any repository — OS cruft like .DS_Store and editor folders like .idea/. Rather than repeat those patterns in every project, set them once globally.
Create ~/.gitignore_global with contents like:
# OS files
.DS_Store
Thumbs.db
# Editor / IDE
.idea/
.vscode/
*.swp
# Logs and local databases
*.log
*.sqlite
Then tell Git to always honor it:
git config --global core.excludesfile ~/.gitignore_global
💡 Global vs. per-project ignores
Use the global ignore for machine- and editor-specific noise that's about you. Use a project's own .gitignore for things about the codebase — node_modules/, build output, .env secrets. We'll dig into project-level ignores in the next lesson.
The Three States of Git
Everything you do in Git makes more sense once you internalize that a file lives in one of three areas. A change flows left to right:
git add stages changes; git commit records them into history; git restore/checkout pulls a saved version back out.- Modified: you changed a file in the working directory but haven't staged it.
- Staged: you marked the change to go into the next commit.
- Committed: the snapshot is safely stored in your local repository.
That staging step — unique among common VCSs — is what lets you commit exactly the changes that belong together, even if your working directory holds more.
Your First Repository
Let's turn a folder into a repository and make two commits. Type each command and read the output — the messages teach you as much as the commands.
# 1. Create and enter a project folder
mkdir my-project
cd my-project
# 2. Turn it into a Git repository
git init
# 3. Create a file
echo "# My Project" > README.md
# 4. See what Git notices (README.md is "untracked")
git status
# 5. Stage the file, then commit it
git add README.md
git commit -m "Add project README"
# 6. Make a change and commit it in one step (works for tracked files)
echo "A sample project for learning Git." >> README.md
git commit -am "Describe the project in the README"
# 7. Review your history
git log --oneline
Your history now looks like:
a1b2c3d Describe the project in the README
e4f5g6h Add project README
Working on an existing project instead? You'd clone it rather than init:
git clone https://github.com/octocat/Hello-World.git
cd Hello-World
Cloning downloads the full repository — every file and its entire history — and sets up the remote automatically.
Troubleshooting
A few first-run snags and their fixes:
| Symptom | Likely cause & fix |
|---|---|
git: command not found |
Terminal opened before install finished, or Git isn't on PATH. Restart the terminal; on Windows re-run the installer and pick the PATH option. |
| Every file shows as "modified" for no reason | Line-ending conversion. Set core.autocrlf correctly and add a .gitattributes with * text=auto, then run git add --renormalize . |
| Repeated password prompts | No credential helper. Configure one (see above); for GitHub, use a personal access token or SSH key, not your account password. |
| SSL certificate errors | Usually an outdated Git or a wrong system clock. Update Git and check the date/time before anything drastic. |
Git also has excellent built-in help — reach for it before searching the web:
git help config # full manual for a command
git config -h # quick option summary
Hands-on Exercise
🏋️ Configure Git and make your first commit
Objective: go from a fresh install to a real repository with two commits, fully configured.
Instructions
- Verify your install with
git --version. - Set your
user.nameanduser.emailglobally. - Set
init.defaultBranchtomainand add at least two aliases. - Create a global
.gitignoreand register it withcore.excludesfile. - Create a new folder,
git initit, add aREADME.md, and commit it. - Edit the README, commit again with
-am, and viewgit log --oneline.
💡 Hint
After step 2, run git config --list --show-origin to confirm your identity landed in ~/.gitconfig. If a value looks wrong, just re-run the same git config --global command — it overwrites the old value.
✅ Reference solution
git --version
git config --global user.name "Ada Lovelace"
git config --global user.email "ada@example.com"
git config --global init.defaultBranch main
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global core.excludesfile ~/.gitignore_global
mkdir git-practice && cd git-practice
git init
echo "# Git Practice" > README.md
git add README.md
git commit -m "Add project README"
echo "Learning Git setup." >> README.md
git commit -am "Expand README with a description"
git lg
If git lg shows two commits authored by your name, your environment is correctly set up for the rest of the module.
Quick Quiz
🎯 Check your understanding
Question 1: Which command sets your commit identity for all your repositories on this machine?
Question 2: A file you just edited but have not run git add on is in which state?
Question 3: When should you use git clone instead of git init?
Summary & What's Next
🎉 Key Takeaways
- Install Git via the platform-native route and confirm it with
git --version. - Git config has three levels — system, global, local — each overriding the one above.
- Always set
user.nameanduser.email, plusinit.defaultBranch main, your editor, and aliases. - A global
.gitignorekeeps OS and editor junk out of every repo. - Files move through modified → staged → committed, and
git init/git clonestart a repo.
📚 Further Reading
- Pro Git — First-Time Git Setup
- GitHub Docs — Getting started with Git
- GitHub's collection of .gitignore templates
🚀 What's Next?
Git is installed and configured, and you've made your first commits. Next we'll drill the core day-to-day commands — status, add, commit, diff, log, and the ways to undo mistakes — until they're second nature.
🎉 You're set up!
Your machine speaks Git. Now let's make it fluent.