| Command | What it does |
|---|
git switch main && git pull | start from the latest main |
git switch -c feature/NAME | create a branch for your change |
git status / git status -s | what changed |
git diff / git diff --staged | unstaged / staged changes |
git add FILE / git add . | stage changes |
git commit -m "Message" | commit what is staged |
git push -u origin feature/NAME | first push of the branch, then open a merge request |
git push | later pushes |
git log --oneline --graph --all | see history and branches |
| Command | What it does |
|---|
git config --global user.name "Name" | your name in commits |
git config --global user.email "you@example.com" | your email in commits |
git config --global init.defaultBranch main | new repos start on main |
git config --global pull.rebase true | git pull rebases instead of merging |
git config --global core.editor nano | editor for messages |
git config --global alias.lg "log --oneline --graph --all" | create git lg |
git config --global rerere.enabled true | remember conflict resolutions |
git config --list --show-origin | all settings and where they come from |
git config user.email work@company.com | this repository only |
ssh-keygen -t ed25519 -C "you@example.com" | create an SSH key for GitLab/GitHub |
ssh -T git@gitlab.com | test SSH access |
| Command | What it does |
|---|
git init | turn this folder into a repository |
git clone URL | copy a repository with full history |
git clone URL DIR | into a folder name of your choice |
git clone --depth 1 URL | latest commit only (fast, for CI) |
git clone -b BRANCH URL | check out a specific branch |
git clone --recurse-submodules URL | include submodules |
| Command | What it does |
|---|
git add -A | stage everything, including deletions |
git add -p | stage parts of files interactively |
git commit -am "Message" | stage tracked changes and commit |
git commit --amend | change the last commit (before pushing) |
git commit --amend --no-edit | add staged files to the last commit |
git commit --fixup COMMIT | mark a fix for an earlier commit |
git rm FILE / git rm --cached FILE | delete / untrack but keep the file |
git mv OLD NEW | rename a tracked file |
History and changes
| Command | What it does |
|---|
git log / git log --oneline | history, full or one line each |
git log -n 5 | last 5 commits |
git log --author=NAME / --grep=WORD | filter by author / message |
git log --since="2 weeks ago" | by date |
git log -- FILE | commits that changed a file |
git log -p / --stat | with diffs / file summary |
git log --format="%h %an %ar %s" | custom one-line format |
git log A..B | commits in B that are not in A |
git show COMMIT | one commit with its diff |
git show COMMIT:path/file | a file as it was |
git diff | working tree vs staging area |
git diff --staged | staging area vs last commit |
git diff HEAD | everything since the last commit |
git diff A B -- FILE | between two commits |
git diff --stat / --name-only | summary / file names |
git shortlog -sn | commits per author |
git blame FILE / -L 10,20 | who last changed each line |
git describe | name a commit from the nearest tag |
Names: HEAD (current), HEAD~1 or HEAD^ (parent), HEAD~3, HEAD^2 (second parent of a merge), branch names, tags, HEAD@{2} (reflog).
| Situation | Command |
|---|
| Discard edits to a file | git restore FILE |
| Unstage a file, keep edits | git restore --staged FILE |
| Take a file from an older commit | git restore --source=HEAD~2 FILE |
| Fix the last commit message | git commit --amend -m "New message" |
| Undo last commit, keep changes staged | git reset --soft HEAD~1 |
| Undo last commit, keep changes as edits | git reset HEAD~1 |
| Undo last commit and delete the changes | git reset --hard HEAD~1 |
| Unstage a file (older form) | git reset FILE |
| Undo a pushed commit safely | git revert COMMIT |
| Cancel a merge in progress | git merge --abort |
| Recover "lost" commits | git reflog then git reset --hard HEAD@{n} |
| Save a lost commit on a branch | git branch rescue HEAD@{n} |
| Delete untracked files (dry run first) | git clean -n -d then git clean -f -d |
| Item | What it does |
|---|
.gitignore | patterns of files to ignore; commit it |
*.log / build/ / /root-only / !keep.log | wildcard / folder / root only / exception |
git check-ignore -v FILE | which rule ignores a file |
git add -f FILE | add an ignored file anyway |
git rm --cached FILE | stop tracking a committed file |
git config --global core.excludesFile ~/.gitignore_global | personal ignores |
| Command | What it does |
|---|
git branch / -v / -vv / -a | list / with last commit / with upstream / with remote branches |
git switch NAME / git switch - | switch / back to the previous branch |
git switch -c NAME [START] | create and switch |
git switch --detach COMMIT | look at an old commit |
git branch -m OLD NEW | rename |
git branch -d NAME / -D NAME | delete merged / force delete |
git branch --merged / --no-merged | safe to delete / still has work |
git checkout -b NAME | create and switch (older form) |
| Command | What it does |
|---|
git merge BRANCH | bring BRANCH into the current branch |
git merge --no-ff BRANCH | always create a merge commit |
git merge --squash BRANCH | stage all its changes as one commit |
git merge --abort | cancel |
git status | lists conflicted files ("both modified") |
<<<<<<< HEAD … ======= … >>>>>>> BRANCH | conflict markers: yours, then theirs |
git add FILE then git commit | mark resolved and finish |
git diff main...BRANCH | its changes since the branches split |
grep -rn '<<<<<<<' . | check no markers are left |
| Command | What it does |
|---|
git remote -v | list remotes |
git remote add NAME URL / set-url / remove | manage remotes |
git fetch / git fetch --prune | download, change nothing / drop deleted branches |
git pull / git pull --rebase | fetch and merge / rebase |
git push -u origin BRANCH | push and set upstream |
git push --force-with-lease | safe force push (own branches only) |
git push origin --delete BRANCH | delete a remote branch |
git log --oneline main..origin/main | what is new on the server |
git branch -vv | ahead/behind per branch |
Rejected push ("fetch first"): git pull --rebase, then git push. Never git push --force on shared branches.
| Command | What it does |
|---|
git stash push -m "msg" | save tracked changes |
git stash push -u -m "msg" | include untracked files |
git stash list | list stashes |
git stash show -p stash@{0} | see one |
git stash pop / apply | restore and remove / restore and keep |
git stash drop stash@{1} / clear | delete one / all |
git stash branch NAME | new branch from a stash |
Rebase and rewriting history
| Command | What it does |
|---|
git rebase main | replay your branch on top of main |
git rebase --continue / --skip / --abort | after a conflict / drop commit / cancel |
git rebase -i main | edit commits: pick, reword, edit, squash, fixup, drop |
git rebase -i --autosquash main | apply --fixup commits automatically |
git config --global rebase.autoSquash true | always autosquash |
git filter-repo --path FILE --invert-paths | remove a file from all history |
Golden rule: never rebase commits that others already have.
| Command | What it does |
|---|
git tag / git tag -l "v1.*" | list tags |
git tag -a v1.2.0 -m "Release 1.2.0" | annotated tag (for releases) |
git tag v1.2.0 COMMIT | lightweight tag on a commit |
git show v1.2.0 | tag and its commit |
git push origin v1.2.0 / --tags | share one / all tags |
git tag -d v1.2.0 / git push origin --delete v1.2.0 | delete locally / on server |
git log --oneline v1.1.0..v1.2.0 | release notes |
git tag -s v1.2.0 -m "msg" / git tag -v v1.2.0 | signed tag / verify |
Semantic versioning: MAJOR (breaking) . MINOR (feature) . PATCH (fix).
| Command | What it does |
|---|
git blame -L 1,10 FILE | who changed these lines |
git bisect start BAD GOOD | start a binary search |
git bisect good / bad / skip | mark the checked-out commit |
git bisect run ./test.sh | automatic: exit 0 good, 1–127 bad, 125 skip |
git bisect reset | finish and go back |
git cherry-pick COMMIT / -x | copy a commit / note where it came from |
git cherry-pick A..B | copy a range |
git reflog | every position HEAD has been |
git grep -n "text" | search tracked files |
| Item | What it does |
|---|
.git/hooks/pre-commit | runs before a commit; non-zero exit stops it |
.git/hooks/commit-msg | checks the message (file path in $1) |
.git/hooks/pre-push | runs before a push (tests) |
chmod +x .git/hooks/NAME | hooks must be executable |
git commit --no-verify | skip local hooks |
git config core.hooksPath .githooks | share hooks in the repository |
pre-commit install / pre-commit run --all-files | the pre-commit framework |
| Command | What it does |
|---|
cat .git/HEAD | current branch reference |
git rev-parse HEAD / --short HEAD | commit hash (short) |
git rev-parse --abbrev-ref HEAD | current branch name |
git rev-parse --show-toplevel | repository root |
git cat-file -t / -p OBJECT | object type / content |
git cat-file -p HEAD^{tree} | the root tree |
git ls-tree HEAD / git ls-files | tree entries / tracked files |
git hash-object FILE | blob hash of a file |
git count-objects -vH / git fsck / git gc | size / integrity / pack |
git status --porcelain | stable output for scripts |
git diff --name-only origin/main...HEAD | changed files in an MR (CI) |
| Command | What it does |
|---|
git submodule add URL PATH | embed a repo pinned to a commit |
git submodule update --init --recursive | fetch submodules |
git lfs install / git lfs track "*.onnx" | large files via LFS (commit .gitattributes) |
git lfs ls-files / git lfs pull | list / download LFS files |
git clone --filter=blob:none --sparse URL | partial clone + sparse checkout |
git sparse-checkout set DIR1 DIR2 / list / disable | only some folders |
git config core.fsmonitor true | faster git status |
git config core.untrackedCache true / feature.manyFiles true | more speed settings |
git maintenance start | background optimisation |
| Command | What it does |
|---|
git config --global gpg.format ssh | sign with SSH keys |
git config --global user.signingkey ~/.ssh/key.pub | which key signs |
git config --global commit.gpgsign true | sign every commit |
git commit -S -m "msg" | sign one commit |
git verify-commit HEAD / git log --show-signature | check signatures |
| Protected branches, required approvals, pipelines must pass, CODEOWNERS | enforce on GitLab/GitHub |
gitleaks in pre-commit and CI | block secrets |
| Strategy | Branches | Best for |
|---|
| Trunk-based / GitHub Flow | main + short branches + feature flags | services deployed continuously |
| GitLab Flow | main → staging → production, or release/* | staged or versioned delivery |
| GitFlow | develop, feature/, release/, hotfix/*, main | scheduled versioned products |
Get every new cheat sheet by email
We're adding cheat sheets for Terraform, AWS, GitLab CI/CD and more.
One email a week at most. Unsubscribe with one click.