Git Cheat Sheet

This page contains a condensed overview of everyday Git. It covers setup, staging and committing, ignoring files, reading diffs and history, branches and merge conflicts, remotes, stashing, and undoing mistakes. You can also download the information as a printable cheat sheet:

Free Bonus: Git Cheat Sheet

Get a Git Cheat Sheet (PDF) with everyday commands for committing, branching, syncing with remotes, and undoing mistakes on one printable page:

Git Cheat Sheet

Practice with hands-on coding exercises, quizzes, and guided learning paths. Not sure where to begin? Start here.

New to Git?

Getting Started

  • Git runs locally; GitHub is just one place to host a repo
  • --global settings apply to every repo on your machine
  • Run git status early and often
  • git help <command> opens any command’s manual
Language: Shell Filename: Install Check and Identity
$ git --version
git version 2.43.0
$ git config --global user.name "Your Name"
$ git config --global user.email \
    "you@example.com"
$ git config --global init.defaultBranch main
Language: Shell Filename: Start or Copy a Repository
$ mkdir hello-git && cd hello-git
$ git init         # Creates hidden .git/
$ git clone <url>  # Copy an existing repo

Want a guided first project?

Stage and Commit

  • Working directory → git add → staging area → git commit → history
  • Commit one logical change at a time
  • Messages: imperative mood, first line ≤ 50 characters
Language: Shell Filename: Check What Changed
$ git status -s
 M main.py     # Modified, not staged
?? README.md   # Untracked
Language: Shell Filename: Stage Changes
$ git add main.py   # One file
$ git add .         # Everything below here
$ git status -s
A  README.md        # Staged new file
M  main.py          # Staged modification
Language: Shell Filename: Save a Snapshot
$ git commit -m "Add README"
$ git commit -am "Fix typo"  # Tracked only
$ git commit  # Opens editor for long message

Think you’ve got staging down?

Ignore Files

  • Never commit secrets, virtual environments, or caches
  • Commit .gitignore itself so the whole team shares it
  • Ignoring doesn’t untrack: use git rm --cached first
Language: Shell Filename: A Python .gitignore
$ cat .gitignore
__pycache__/
*.pyc
.env
.venv/
Language: Shell Filename: Debug and Untrack
$ git check-ignore -v .env
.gitignore:3:.env   .env
$ git rm --cached secrets.txt

Committed a secret by accident?

Inspect Changes and History

  • git diff compares against the stage; new files don’t show
  • Press q to leave the pager
  • HEAD is your current commit; HEAD~1 is its parent
Language: Shell Filename: Review a Diff
$ git diff           # Unstaged changes
$ git diff --staged  # What you'll commit
@@ -1,4 +1,4 @@
-print(greet("World"))
+print(greet("Git learner"))
Language: Shell Filename: Browse the Log
$ git log --oneline
1c692b1 Update greeting and add README
dd5a68a Add .gitignore
$ git log --oneline --graph --all
$ git log -p main.py    # One file's history
$ git show HEAD         # Last commit + diff
$ git blame main.py     # Who wrote each line

Want to dig further into history?

Branches and Merging

  • A branch is a movable pointer to a commit; they’re cheap
  • Commit or stash before you switch branches
  • Merge into the branch you’re on
Language: Shell Filename: Create and Switch
$ git switch -c add-farewell  # New branch
$ git branch
* add-farewell
  main
$ git switch main
$ git switch -    # Back to previous branch
Language: Shell Filename: Merge and Clean Up
$ git merge add-farewell
Fast-forward
$ git branch -d add-farewell
Language: Shell Filename: Resolve a Conflict
$ git merge shout
CONFLICT (content): Merge conflict in main.py
<<<<<<< HEAD
    return f"Hi, {name}!"
=======
    return f"HELLO, {name}!"
>>>>>>> shout
$ git add main.py && git commit  # Resolved
$ git merge --abort              # Or give up

Ready to test yourself on branching?

Remote Repositories

  • origin is the conventional name for your main remote
  • fetch downloads; pull = fetch + merge
  • Pull before you push to avoid rejected pushes
Language: Shell Filename: Connect and Publish
$ git remote add origin <url>
$ git remote -v
$ git push -u origin main  # First push
$ git push                 # After that
Language: Shell Filename: Stay in Sync
$ git fetch         # Download, don't merge
$ git pull          # Download and merge
$ git pull --rebase # Replay yours on top

Pull, fetch, or rebase?

Stash Work in Progress

  • Stash parks uncommitted changes so you can switch tasks
  • pop applies and deletes; apply keeps the stash
Language: Shell Filename: Park and Restore
$ git stash push -m "wip"
$ git stash list
stash@{0}: On main: wip
$ git stash pop

Wondering what stash leaves behind?

Undo Mistakes

  • restore on a file discards edits for good
  • Shared commits: revert, never reset or --amend
  • reset --hard also wipes uncommitted work
Situation Command
Unstage a file git restore --staged f
Discard file edits git restore f
Fix last message git commit --amend
Undo commit, keep work git reset --soft HEAD~1
Undo a pushed commit git revert <hash>
Rename a tracked file git mv old new
Language: Shell Filename: Recover a Lost Commit
$ git reset --hard HEAD~1  # Oops!
$ git reflog
5349a96 HEAD@{0}: reset: moving to HEAD~1
5708734 HEAD@{1}: commit: Say hi instead
$ git reset --hard 5708734  # Back again
Language: Shell Filename: Delete Untracked Files
$ git clean -n  # Dry run first
Would remove tmp.log
$ git clean -f

Still fuzzy on restore, reset, and revert?

Ready to go beyond the cheat sheet?

You can download this information as a printable cheat sheet:

Free Bonus: Git Cheat Sheet

Get a Git Cheat Sheet (PDF) with everyday commands for committing, branching, syncing with remotes, and undoing mistakes on one printable page:

Git Cheat Sheet