Git Cheat Sheet
DeveloperCommonly used Git commands reference
Fill placeholders once and every command below uses them. 63 commands.
Setup
git config --global user.name "<name>"Set the name recorded on your commits
git config --global user.email "<email>"Set the email recorded on your commits
git config --global init.defaultBranch mainUse main for new repositories
git config --global pull.rebase trueRebase instead of merge when pulling
git config --list --show-originShow all settings and where they come from
Start a repository
git initCreate a repository in the current folder
git clone <url>Download a repository and its history
git clone --depth 1 <url>Shallow clone — latest snapshot only, much faster
git remote add origin <url>Connect a local repo to a remote
git remote -vList remotes and their URLs
Everyday work
git status -sbShort status with branch info
git add <file>Stage a file
git add -pStage changes interactively, hunk by hunk
git commit -m "<message>"Commit staged changes
git commit -am "<message>"Stage all tracked files and commit
git diffUnstaged changes
git diff --stagedStaged changes (what will be committed)
git pullFetch and integrate remote changes
git push -u origin <branch>Push a new branch and set its upstream
Branches
git switch -c <branch>Create and switch to a new branch
git switch <branch>Switch to an existing branch
git switch -Go back to the previous branch
git branch -aList local and remote branches
git branch -d <branch>Delete a merged branch
git branch -D <branch>Force-delete a branch (even if unmerged)destructive
git push origin --delete <branch>Delete a remote branchdestructive
git branch -m <new-name>Rename the current branch
Merge & rebase
git merge <branch>Merge a branch into the current one
git merge --no-ff <branch>Always create a merge commit
git rebase mainReplay your commits on top of main
git rebase -i HEAD~<n>Edit, squash, reorder or drop the last n commits
git rebase --continueContinue after resolving conflicts (or --abort)
git cherry-pick <commit>Apply one commit from another branch
git merge --abortGive up a merge that has conflicts
Undo & fix mistakes
git restore <file>Discard unstaged changes to a filedestructive
git restore --staged <file>Unstage a file (keep the changes)
git commit --amendEdit the last commit (message or content)
git commit --amend --no-editAdd staged changes to the last commit
git reset --soft HEAD~1Undo the last commit, keep changes staged
git reset --hard HEAD~1Delete the last commit and its changesdestructive
git revert <commit>Create a new commit that undoes a commit — safe on shared branches
git reflogEvery position HEAD has been — recover “lost” commits
git clean -fdDelete untracked files and foldersdestructive
git push --force-with-leaseForce-push only if nobody else pushed meanwhiledestructive
Stash
git stash push -m "<message>"Shelve uncommitted changes
git stash -uStash including untracked files
git stash listList stashes
git stash popRe-apply the latest stash and remove it
git stash apply stash@{<n>}Re-apply a stash but keep it
History & search
git log --oneline --graph --decorate --allCompact visual history of all branches
git log -p <file>Every change to a file
git log -S "<text>"Find commits that added or removed text
git blame <file>Who last changed each line
git show <commit>Show a commit's changes
git bisect startBinary-search for the commit that introduced a bug
git grep "<text>"Search tracked files
Tags & releases
git tag -a v<version> -m "<message>"Create an annotated tag
git push origin --tagsPush all tags
git describe --tagsNearest tag + commits since
Advanced
git worktree add ../<folder> <branch>Check out another branch in a second folder
git submodule update --init --recursiveFetch submodules after cloning
git sparse-checkout set <folder>Only check out part of a large repo
git gc --prune=nowClean up and compress the repository