Git Cheat Sheet

A practical Git reference for everyday use, plus the stuff that saves your ass when you accidentally stage giant files, need to rewrite local history, or want to see what is really about to be pushed.

Modern single-file page Copy buttons Cursor-follow glow Commands you actually use

Setup

Basic config you usually set once.

Set your name and email
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Used for commit authorship.
See all config values
git config --list
Handy when Git is behaving weirdly and you want to see current settings.
Initialize a new repo
git init
Creates a new repository in the current folder.

Daily workflow

The bread-and-butter commands.

Check status
git status -sb
Short, useful status view with branch info.
Stage everything in current directory tree
git add .
Be careful with this. Great until you accidentally include giant files.
Stage a specific file
git add path/to/file
Safer than `git add .` when you want to be precise.
Commit staged changes
git commit -m "your message"
Creates a commit from currently staged changes.
Push current branch
git push -u origin "$(git branch --show-current)"
A good default. Better than blindly pushing `--all` most of the time.

Branches

Create, switch, and inspect branches.

List branches
git branch -a
Shows local and remote-tracking branches.
Create and switch to new branch
git switch -c my-branch
Modern replacement for `git checkout -b`.
Switch branches
git switch main
Moves you to another existing branch.
Delete merged branch
git branch -d my-branch
Deletes a local branch that has already been merged.

History and logs

Useful ways to see what happened and when.

Compact commit log
git log --oneline --decorate --graph --all -20
Great all-purpose quick history view.
Show changes for a specific commit
git show COMMIT_HASH
Displays the patch and metadata for a commit.
See commits affecting a file
git log -- path/to/file
Super useful when you need file-specific history.
See who changed each line
git blame path/to/file
Line-by-line authorship and commit info.

Undo / reset / amend

The stuff you use when you realize you messed something up.

Unstage a file
git restore --staged path/to/file
Removes a file from the staging area without deleting your edits.
Amend the most recent commit
git commit --amend --no-edit
Rewrites the last commit using the currently staged content but keeps the same message.
Undo last commit, keep changes staged
git reset --soft HEAD~1
Good when you want to redo the commit cleanly.
Undo last commit, keep changes unstaged
git reset HEAD~1
Removes the commit and unstages its contents.
Rebuild all unpushed local commits as one clean commit
git reset --soft origin/main
Very useful when local history got messy and nothing has pushed successfully yet.

Stash

Temporary parking spot for work in progress.

Stash current work
git stash push -m "WIP"
Saves your current changes and cleans the working tree.
List stashes
git stash list
Shows saved stash entries.
Apply most recent stash
git stash pop
Restores and removes the most recent stash.

Remotes and sync

Working with GitHub or any other remote.

See remote URLs
git remote -v
Shows fetch/push URLs for configured remotes.
Fetch from remote
git fetch origin
Downloads remote changes without merging them into your branch.
Pull current branch
git pull
Fetches and merges remote changes into the current branch.
Set or change origin URL
git remote set-url origin REPO_URL
Useful if the repo moved or you switched from HTTPS to SSH.

Cleanup and ignore

Keep junk files out of the repo.

Ignore file patterns
echo "*.mp4" >> .gitignore
echo "*.flac" >> .gitignore
Prevents future accidental staging of those file types.
Remove tracked file but keep it on disk
git rm --cached path/to/file
Stops tracking a file without deleting your local copy.
See which ignore rule matched a file
git check-ignore -v path/to/file
Excellent for debugging `.gitignore` behavior.

Large file sanity checks

The exact sort of thing that saves you from GitHub rejecting a push.

List tracked MP4/FLAC files
git ls-files | grep -E '\.(mp4|flac)$'
Shows whether those file types are currently tracked in the repo snapshot.
See large files in commits you are about to push
branch=$(git branch --show-current)

git rev-list --objects "origin/$branch..HEAD" |
git cat-file --batch-check="%(objecttype) %(objectname) %(objectsize) %(rest)" |
sort -k3 -n |
tail -20
Shows the largest objects in your outgoing history.
Quick check: are MP4/FLAC files still in outgoing history?
branch=$(git branch --show-current)
git rev-list --objects "origin/$branch..HEAD" | grep -E "\.(mp4|flac)$"
If this prints nothing, those files are not in what you are about to push.

Inspection and troubleshooting

Great when something feels off and you want facts instead of guesses.

See what is staged
git diff --cached --name-status
Shows staged changes, which is exactly what will go into the next commit.
See unstaged changes
git diff
Shows working-tree changes that are not yet staged.
Show commits you have locally but GitHub does not
git log --oneline origin/$(git branch --show-current)..HEAD
Great quick view of unpushed commits.
Find commits that touched a specific file type
git log --all -- '*.mp4' '*.flac'
Helps identify the commit that introduced a bad file.

High-value reminders