Git Cheatsheet
Git HelpersInteractive, searchable reference of essential git commands organized by category with syntax, descriptions, and examples.
Setup
git config --global user.name "Name"
Set your global username
git config --global user.name "Jane Doe"git config --global user.email "email"
Set your global email
git config --global user.email "jane@example.com"git config --list
Show all git configuration settings
git config --listgit init
Initialize a new git repository
git init my-projectgit clone <url>
Clone a remote repository
git clone https://github.com/user/repo.gitStaging
git status
Show working tree status
git statusgit add <file>
Stage a specific file
git add index.htmlgit add .
Stage all changes in current directory
git add .git add -p
Interactively stage chunks of changes
git add -pgit reset HEAD <file>
Unstage a file (keep changes)
git reset HEAD style.cssgit rm --cached <file>
Remove file from staging (keep on disk)
git rm --cached secrets.envBranching
git branch
List all local branches
git branchgit branch <name>
Create a new branch
git branch feature/logingit branch -d <name>
Delete a branch (safe)
git branch -d old-featuregit branch -D <name>
Force delete a branch
git branch -D broken-featuregit branch -a
List all branches (local + remote)
git branch -agit checkout <branch>
Switch to an existing branch
git checkout maingit checkout -b <branch>
Create and switch to new branch
git checkout -b feature/authgit switch <branch>
Switch branches (modern syntax)
git switch developgit switch -c <branch>
Create and switch (modern syntax)
git switch -c feature/apiMerging
git merge <branch>
Merge a branch into current branch
git merge feature/logingit merge --no-ff <branch>
Merge with a merge commit (no fast-forward)
git merge --no-ff feature/authgit merge --squash <branch>
Squash all commits into one before merging
git merge --squash feature/cleanupgit merge --abort
Abort a merge in progress
git merge --abortStashing
git stash
Stash working directory changes
git stashgit stash push -m "msg"
Stash with a descriptive message
git stash push -m "WIP: login form"git stash list
List all stashes
git stash listgit stash pop
Apply most recent stash and remove it
git stash popgit stash apply stash@{n}
Apply a specific stash (keep it)
git stash apply stash@{2}git stash drop stash@{n}
Delete a specific stash
git stash drop stash@{0}git stash clear
Delete all stashes
git stash clearRemote
git remote -v
List remote repositories
git remote -vgit remote add <name> <url>
Add a remote repository
git remote add origin https://github.com/user/repo.gitgit fetch
Download objects/refs from remote
git fetch origingit pull
Fetch and merge remote changes
git pull origin maingit push
Push commits to remote
git push origin maingit push -u origin <branch>
Push and set upstream branch
git push -u origin feature/authgit push --force-with-lease
Force push safely (checks remote state)
git push --force-with-leaseUndoing
git checkout -- <file>
Discard changes in a file
git checkout -- index.htmlgit restore <file>
Discard changes (modern syntax)
git restore style.cssgit reset --soft HEAD~1
Undo last commit, keep changes staged
git reset --soft HEAD~1git reset --mixed HEAD~1
Undo last commit, unstage changes
git reset --mixed HEAD~1git reset --hard HEAD~1
Undo last commit, discard changes
git reset --hard HEAD~1git revert <commit>
Create a new commit that undoes a previous one
git revert a1b2c3dgit clean -fd
Remove untracked files and directories
git clean -fdViewing History
git log
Show commit history
git loggit log --oneline
Compact commit history (one line each)
git log --onelinegit log --oneline --graph --all
Visual branch graph
git log --oneline --graph --allgit log -n <num>
Show last N commits
git log -n 5git log --author="name"
Filter commits by author
git log --author="Jane"git log -- <file>
Show commits that modified a file
git log -- src/app.jsgit show <commit>
Show details of a specific commit
git show a1b2c3dgit diff
Show unstaged changes
git diffgit diff --staged
Show staged changes
git diff --stagedgit blame <file>
Show who last modified each line
git blame src/utils.jsTags
git tag
List all tags
git taggit tag <name>
Create a lightweight tag
git tag v1.0.0git tag -a <name> -m "msg"
Create an annotated tag
git tag -a v1.0.0 -m "Release 1.0.0"git push origin <tag>
Push a tag to remote
git push origin v1.0.0git push origin --tags
Push all tags to remote
git push origin --tagsgit tag -d <name>
Delete a local tag
git tag -d v0.9.0Advanced
git rebase <branch>
Reapply commits on top of another branch
git rebase maingit rebase -i HEAD~n
Interactive rebase for last N commits
git rebase -i HEAD~3git cherry-pick <commit>
Apply a specific commit to current branch
git cherry-pick a1b2c3dgit bisect start
Start binary search for a buggy commit
git bisect startgit reflog
Show history of HEAD movements
git refloggit shortlog -sn
Commit count by author (leaderboard)
git shortlog -sn --allgit archive --format=zip HEAD -o out.zip
Create a zip archive of the repo
git archive --format=zip HEAD -o project.zipgit submodule add <url>
Add a submodule
git submodule add https://github.com/lib/lib.git libs/libgit worktree add <path> <branch>
Create a linked working tree
git worktree add ../hotfix-tree hotfix/urgent