🌿
Git cheat sheet
Commands for the daily Git workflow. Commands marked with a warning discard work and cannot always be undone, so check git status and consider git stash first.
Click any command to copy it.
Set up and get code
| Command | What it does |
|---|---|
git config --global user.name "Your Name" | Set the name on your commits. |
git config --global user.email "you@example.com" | Set the email on your commits. |
git init | Create a repository in the current folder. |
git clone URL | Copy a remote repository. |
git clone --depth 1 URL | Clone only the latest commit (fast, small). |
Stage and commit
| Command | What it does |
|---|---|
git status | Show changed, staged and untracked files. |
git add -p | Stage changes interactively, hunk by hunk. |
git add -A | Stage all changes, including deletions. |
git commit -m "message" | Commit the staged changes. |
git commit --amend | Change the last commit (only if you have not pushed it). |
git diff | Show unstaged changes. |
git diff --staged | Show staged changes. |
Branches and merging
| Command | What it does |
|---|---|
git branch | List local branches. |
git switch -c new-branch | Create and switch to a new branch. |
git switch main | Switch to an existing branch. |
git merge branch | Merge a branch into the current one. |
git rebase main | Replay your branch's commits on top of main. |
git branch -d branch | Delete a merged branch. |
git branch -D branch | Destructive: Delete a branch even if it has unmerged commits. |
git cherry-pick SHA | Apply one commit onto the current branch. |
Remotes
| Command | What it does |
|---|---|
git remote -v | List remotes and their URLs. |
git fetch | Download remote changes without merging. |
git pull --rebase | Fetch and replay your local commits on top. |
git push -u origin branch | Push a branch and set its upstream. |
git push --force-with-lease | Destructive: Force push, but refuse if someone else pushed in the meantime. |
git push origin --delete branch | Destructive: Delete a remote branch. |
History and inspection
| Command | What it does |
|---|---|
git log --oneline --graph --decorate --all | Compact graph of all branches. |
git show SHA | Show one commit's changes. |
git blame file | Show who last changed each line. |
git log -p file | History of changes to one file. |
git reflog | Every position HEAD has been at; recovers 'lost' commits. |
git bisect start | Begin a binary search for the commit that introduced a bug. |
Undo
| Command | What it does |
|---|---|
git restore file | Destructive: Discard unstaged changes to a file. |
git restore --staged file | Unstage a file but keep the changes. |
git reset --soft HEAD~1 | Undo the last commit and keep its changes staged. |
git reset --hard HEAD~1 | Destructive: Undo the last commit and discard its changes. |
git revert SHA | Create a new commit that undoes an earlier one (safe for shared branches). |
git clean -fd | Destructive: Delete untracked files and folders. Preview with -n first. |
git stash | Shelve uncommitted changes. |
git stash pop | Restore the most recent stash. |
git stash list | List stashes. |
Tags
| Command | What it does |
|---|---|
git tag -a v1.0 -m "Release 1.0" | Create an annotated tag. |
git push origin v1.0 | Push one tag. |
git push --tags | Push all tags. |
git tag -d v1.0 | Delete a local tag. |
Frequently asked questions
How do I undo my last commit?
Use git reset --soft HEAD~1 to keep the changes staged, or git revert HEAD to add a new commit that reverses it if it is already pushed.
How do I recover a commit I lost?
Run git reflog to find its SHA, then git switch -c recovered SHA to bring it back on a branch.