/cheatsheets/git/git-cheat-sheet
Git cheat sheet: everyday commands
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · git version 2.55.0.windows.5updated 2026-10-20
- tool
- Git
- versions
- Git 2.23+
- os
- linux, macos, windows
The Git commands for daily work. git switch and git restore (Git 2.23+) replace the overloaded git checkout for branches and files.
A typical session
git init -q -b main demo && cd demo
echo "hello" > README.md
git add README.md && git commit -qm "Add README"
git switch -q -c feature/greeting
echo "world" >> README.md && git commit -qam "Extend greeting"
git switch -q main && git merge -q --no-edit feature/greeting
git log --oneline --graph --allOutput
* 37042af Extend greeting * 74c3f75 Add README
Setup
| Command | What it does |
|---|---|
| git config --global user.name "Name" | identity for commits |
| git config --global init.defaultBranch main | branch name for new repositories |
| git init | create a repository in the current folder |
| git clone <url> [dir] | copy a repository with its history |
Stage and commit
| Command | What it does |
|---|---|
| git status -sb | short status with branch |
| git add <path> / git add -p | stage a file / stage hunk by hunk |
| git commit -m "msg" | commit the staged changes |
| git commit --amend | rewrite the last commit (message or content) |
| git diff / git diff --staged | unstaged / staged changes |
Branches
| Command | What it does |
|---|---|
| git switch -c <branch> | create and switch |
| git switch <branch> | switch to an existing branch |
| git branch -d <branch> | delete a merged branch |
| git merge <branch> | merge into the current branch |
| git rebase <base> | replay the current branch on top of base |
History
| Command | What it does |
|---|---|
| git log --oneline --graph --all | compact graph of every branch |
| git log -p <path> | history of a file with diffs |
| git show <commit> | one commit in detail |
| git blame <path> | last change for each line |
Undo
| Command | What it does |
|---|---|
| git restore <path> | discard unstaged changes in a file |
| git restore --staged <path> | unstage, keep the changes |
| git reset --soft HEAD~1 | undo the last commit, keep changes staged |
| git revert <commit> | new commit that undoes an old one (safe after push) |
| git reflog | every position HEAD has had; recover "lost" commits |
Stash, remotes and tags
| Command | What it does |
|---|---|
| git stash / git stash pop | shelve and re-apply uncommitted work |
| git remote -v | list remotes |
| git fetch / git pull --rebase | download / download and rebase onto it |
| git push -u origin <branch> | push and set upstream |
| git tag -a v1.0 -m "msg" | annotated tag |
| git push origin --tags | push tags |
Sources
Related pages
commands · gitgit reset --soft HEAD~1: undo the last commit, keep the changesUndo the last commit and keep its changes staged with git reset --soft HEAD~1. --mixed unstages them, --hard deletes them. Undo a reset with the reflog.compare · gitgit merge vs git rebase: differences and when to use eachgit merge joins histories with a merge commit and keeps them as they happened; git rebase replays commits on a new base for a linear history with new hashes.config · gitgit config core.autocrlf: true, input or falsecore.autocrlf sets line-ending conversion: true gives CRLF on checkout, input turns CRLF into LF on commit, false never converts. Share it via .gitattributes.