Git Cheatsheet Online | Essential Git Commands for Developers

Git Cheatsheet

In-depth Git cheatsheet for branching, history, rebasing, recovery, and collaboration workflows.

48/48 commands
Version ControlAll Developers

This Git cheatsheet covers both daily development commands and advanced recovery and collaboration patterns used in real projects.

Setup and Identity

Set username
git config --global user.name "Your Name"
Set email
git config --global user.email "you@example.com"
View all config
git config --list
Enable rebase on pull
git config --global pull.rebase true
Set default branch name
git config --global init.defaultBranch main
Store credentials (macOS keychain example)
git config --global credential.helper osxkeychain

Daily Workflow

Check status
git status -sb
Stage all
git add .
Stage specific hunks interactively
git add -p
Commit
git commit -m "<message>"
Amend last commit
git commit --amend --no-edit
Push current branch and set upstream
git push -u origin <branch>

Branching and Sync

Create and switch branch
git switch -c <branch>
Switch branch
git switch <branch>
List local and remote branches
git branch -a
Delete local branch
git branch -d <branch>
Delete remote branch
git push origin --delete <branch>
Fetch all remotes and prune
git fetch --all --prune
Rebase current branch on main
git rebase origin/main

History and Inspection

One-line graph history
git log --oneline --graph --decorate --all
Show file history
git log -- <path/to/file>
Show changes in last commit
git show --stat HEAD
Compare working tree vs staged
git diff --staged
Compare current branch with main
git diff origin/main...HEAD
Who changed each line
git blame <path/to/file>

Stash and WIP Management

Stash tracked changes
git stash push -m "wip: <note>"
Stash including untracked files
git stash push -u -m "wip: <note>"
List stashes
git stash list
Apply latest stash
git stash apply
Apply and drop latest stash
git stash pop
Drop specific stash
git stash drop stash@{0}

Rebase, Cherry-pick, and Cleanup

Interactive rebase last 5 commits
git rebase -i HEAD~5
Continue rebase after conflict fix
git rebase --continue
Abort current rebase
git rebase --abort
Cherry-pick a commit
git cherry-pick <commit-hash>
Squash-merge branch into current
git merge --squash <branch>
Find and remove merged local branches
git branch --merged | grep -v "\*\|main\|master\|develop" | xargs -n 1 git branch -d

Rollback and Recovery

Revert commit safely (shared branches)
git revert <commit-hash>
Soft reset last commit
git reset --soft HEAD~1
Hard reset to remote branch
git reset --hard origin/<branch>
Recover lost commit references
git reflog
Restore to reflog checkpoint
git reset --hard <reflog-ref>
Restore deleted file from HEAD
git checkout HEAD -- <path/to/file>

Collaboration and Review

Fetch PR branch by ref (GitHub)
git fetch origin pull/<id>/head:pr-<id>
List files changed against main
git diff --name-only origin/main...HEAD
Show commit range not in main
git log --oneline origin/main..HEAD
Fixup commit for autosquash
git commit --fixup <commit-hash>
Autosquash during interactive rebase
git rebase -i --autosquash origin/main

What Is Git Cheatsheet?

This Git cheatsheet covers both daily development commands and advanced recovery and collaboration patterns used in real projects.

Daily branch and commit workflow commands

History, diff, and blame inspection patterns

Advanced rebase, stash, cherry-pick, and reflog recovery commands

Key Features of Git Cheatsheet

Daily branch and commit workflow commands

History, diff, and blame inspection patterns

Advanced rebase, stash, cherry-pick, and reflog recovery commands

How to Use Git Cheatsheet

Step 1

Start with status and history commands before changing commits

Step 2

Use branch and sync commands to keep a clean PR workflow

Step 3

Use stash, rebase, and reflog sections for advanced fixes and recovery

Frequently Asked Questions

When should I use git revert instead of reset?

Use revert on shared branches because it creates a new commit and keeps history intact.

What is the safest way to recover lost work after reset or rebase mistakes?

Use git reflog to locate the previous commit reference, then restore it with git reset --hard <reflog-ref> on a local branch.

Still need help?

Need a hand with Git Cheatsheet? Reach out through support or send a feature request with your workflow.

Contact support