Mastering Git from the Command Line
A straight-talking walkthrough of Git’s most essential CLI commands, what they do, and when to use them.




Note:
This guide is written for solo developers working independently with Git.
If you’re working in a team or contributing to shared repositories, some practices may differ.
A follow-up guide on team workflows, conflict handling, and collaboration best practices is coming soon.
You’ve clicked the buttons, dragged the files, maybe even used GitHub Desktop — but if you want real power and speed, it’s time to get comfortable in the command line. This guide breaks down Git’s most essential commands, how to use them, and what they actually mean.
Why the Command Line Still Matters
- It’s faster once you learn it
- It works in any dev environment (even headless servers)
- It’s what pros, automation, and DevOps tools rely on
Let’s break down the commands you’ll use every day — with clear definitions and real examples.
Git Configuration
git config
What it does:
Sets configuration options like your name, email, editor, and aliases.
Examples:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Use --global
to apply it everywhere, or leave it off for per-repo settings.
Creating & Cloning Repositories
git init
Initializes a new Git repository in the current folder.
git init
Creates a hidden .git
folder that starts tracking changes.
git clone
Copies an existing Git repo to your local system.
git clone https://github.com/user/repo.git
Checking Status & Adding Files
git status
Shows the state of your working directory and staging area.
git status
See what’s changed, what’s staged, and what’s untracked.
git add
Stages files for the next commit.
git add file.txt # Add one file
git add . # Add everything in the folder
Committing Changes
git commit
Saves a snapshot of the staged changes.
git commit -m "Your message here"
This is what creates real history. Don’t forget the -m
for your message.
Branching & Switching
git branch
Lists, creates, or deletes branches.
git branch # List branches
git branch feature1 # Create a branch
git switch
/ git checkout
Switches to another branch.
git switch main
Prefer switch
over the older checkout
for clarity.
git merge
Combines another branch into your current one.
git merge feature1
Fast-forwards if possible; otherwise creates a merge commit.
Pushing & Pulling
git push
Uploads your commits to the remote repo.
git push origin main
git pull
Fetches and integrates changes from the remote.
git pull
git fetch
Downloads commits but doesn’t apply them.
git fetch
Useful for previewing changes before merging.
Fixing Mistakes
git reset
Unstages or rewinds commits. Be careful!
git reset HEAD~1 # Undo last commit (keep changes)
git restore
Restores file content from a commit or discards local changes.
git restore file.txt
git revert
Creates a new commit that undoes a previous one.
git revert <commit-id>
git stash
Temporarily saves changes without committing.
git stash
git stash pop
Viewing History
git log
Shows commit history.
git log --oneline
git diff
Compares changes.
git diff
git show
Shows details of a commit.
git show <commit-id>
TL;DR Cheat Sheet
Command | Purpose |
---|---|
git init |
Start a new repo |
git clone |
Copy a remote repo |
git status |
Check changes |
git add |
Stage files |
git commit |
Save a snapshot |
git push |
Upload commits to remote |
git pull |
Fetch and merge remote changes |
git branch |
View/create branches |
git switch |
Move between branches |
git merge |
Combine branches |
git log |
View history |
git diff |
Compare file changes |
git stash |
Hide local changes temporarily |
Your Turn
Still using Git through buttons and GUIs? No shame — but if you’ve got a favorite CLI trick, alias, or “I broke everything and fixed it with one command” story, drop it in the thread.
Let’s build a proper command-line Git cheatbook — together.