Skip to content
$ lynuxpro
← Git

CHEAT SHEET · 21 LESSONS IN ONE PAGE

Git cheat sheet

Every Git command from the course on one page: setup, daily work, history, undo, branches, remotes, rebase, debugging and security.

Saves a clean, printable copy (Ctrl+P or ⌘P also works).

The daily loop

CommandWhat it does
git switch main && git pullstart from the latest main
git switch -c feature/NAMEcreate a branch for your change
git status / git status -swhat changed
git diff / git diff --stagedunstaged / staged changes
git add FILE / git add .stage changes
git commit -m "Message"commit what is staged
git push -u origin feature/NAMEfirst push of the branch, then open a merge request
git pushlater pushes
git log --oneline --graph --allsee history and branches

Setup

CommandWhat 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 mainnew repos start on main
git config --global pull.rebase truegit pull rebases instead of merging
git config --global core.editor nanoeditor for messages
git config --global alias.lg "log --oneline --graph --all"create git lg
git config --global rerere.enabled trueremember conflict resolutions
git config --list --show-originall settings and where they come from
git config user.email work@company.comthis repository only
ssh-keygen -t ed25519 -C "you@example.com"create an SSH key for GitLab/GitHub
ssh -T git@gitlab.comtest SSH access

Start a repository

CommandWhat it does
git initturn this folder into a repository
git clone URLcopy a repository with full history
git clone URL DIRinto a folder name of your choice
git clone --depth 1 URLlatest commit only (fast, for CI)
git clone -b BRANCH URLcheck out a specific branch
git clone --recurse-submodules URLinclude submodules

Stage and commit

CommandWhat it does
git add -Astage everything, including deletions
git add -pstage parts of files interactively
git commit -am "Message"stage tracked changes and commit
git commit --amendchange the last commit (before pushing)
git commit --amend --no-editadd staged files to the last commit
git commit --fixup COMMITmark a fix for an earlier commit
git rm FILE / git rm --cached FILEdelete / untrack but keep the file
git mv OLD NEWrename a tracked file

History and changes

CommandWhat it does
git log / git log --onelinehistory, full or one line each
git log -n 5last 5 commits
git log --author=NAME / --grep=WORDfilter by author / message
git log --since="2 weeks ago"by date
git log -- FILEcommits that changed a file
git log -p / --statwith diffs / file summary
git log --format="%h %an %ar %s"custom one-line format
git log A..Bcommits in B that are not in A
git show COMMITone commit with its diff
git show COMMIT:path/filea file as it was
git diffworking tree vs staging area
git diff --stagedstaging area vs last commit
git diff HEADeverything since the last commit
git diff A B -- FILEbetween two commits
git diff --stat / --name-onlysummary / file names
git shortlog -sncommits per author
git blame FILE / -L 10,20who last changed each line
git describename 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).

Undo

SituationCommand
Discard edits to a filegit restore FILE
Unstage a file, keep editsgit restore --staged FILE
Take a file from an older commitgit restore --source=HEAD~2 FILE
Fix the last commit messagegit commit --amend -m "New message"
Undo last commit, keep changes stagedgit reset --soft HEAD~1
Undo last commit, keep changes as editsgit reset HEAD~1
Undo last commit and delete the changesgit reset --hard HEAD~1
Unstage a file (older form)git reset FILE
Undo a pushed commit safelygit revert COMMIT
Cancel a merge in progressgit merge --abort
Recover "lost" commitsgit reflog then git reset --hard HEAD@{n}
Save a lost commit on a branchgit branch rescue HEAD@{n}
Delete untracked files (dry run first)git clean -n -d then git clean -f -d

Ignore files

ItemWhat it does
.gitignorepatterns of files to ignore; commit it
*.log / build/ / /root-only / !keep.logwildcard / folder / root only / exception
git check-ignore -v FILEwhich rule ignores a file
git add -f FILEadd an ignored file anyway
git rm --cached FILEstop tracking a committed file
git config --global core.excludesFile ~/.gitignore_globalpersonal ignores

Branches

CommandWhat it does
git branch / -v / -vv / -alist / 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 COMMITlook at an old commit
git branch -m OLD NEWrename
git branch -d NAME / -D NAMEdelete merged / force delete
git branch --merged / --no-mergedsafe to delete / still has work
git checkout -b NAMEcreate and switch (older form)

Merge and conflicts

CommandWhat it does
git merge BRANCHbring BRANCH into the current branch
git merge --no-ff BRANCHalways create a merge commit
git merge --squash BRANCHstage all its changes as one commit
git merge --abortcancel
git statuslists conflicted files ("both modified")
<<<<<<< HEAD … ======= … >>>>>>> BRANCHconflict markers: yours, then theirs
git add FILE then git commitmark resolved and finish
git diff main...BRANCHits changes since the branches split
grep -rn '<<<<<<<' .check no markers are left

Remotes

CommandWhat it does
git remote -vlist remotes
git remote add NAME URL / set-url / removemanage remotes
git fetch / git fetch --prunedownload, change nothing / drop deleted branches
git pull / git pull --rebasefetch and merge / rebase
git push -u origin BRANCHpush and set upstream
git push --force-with-leasesafe force push (own branches only)
git push origin --delete BRANCHdelete a remote branch
git log --oneline main..origin/mainwhat is new on the server
git branch -vvahead/behind per branch

Rejected push ("fetch first"): git pull --rebase, then git push. Never git push --force on shared branches.

Stash

CommandWhat it does
git stash push -m "msg"save tracked changes
git stash push -u -m "msg"include untracked files
git stash listlist stashes
git stash show -p stash@{0}see one
git stash pop / applyrestore and remove / restore and keep
git stash drop stash@{1} / cleardelete one / all
git stash branch NAMEnew branch from a stash

Rebase and rewriting history

CommandWhat it does
git rebase mainreplay your branch on top of main
git rebase --continue / --skip / --abortafter a conflict / drop commit / cancel
git rebase -i mainedit commits: pick, reword, edit, squash, fixup, drop
git rebase -i --autosquash mainapply --fixup commits automatically
git config --global rebase.autoSquash truealways autosquash
git filter-repo --path FILE --invert-pathsremove a file from all history

Golden rule: never rebase commits that others already have.

Tags and releases

CommandWhat 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 COMMITlightweight tag on a commit
git show v1.2.0tag and its commit
git push origin v1.2.0 / --tagsshare one / all tags
git tag -d v1.2.0 / git push origin --delete v1.2.0delete locally / on server
git log --oneline v1.1.0..v1.2.0release notes
git tag -s v1.2.0 -m "msg" / git tag -v v1.2.0signed tag / verify

Semantic versioning: MAJOR (breaking) . MINOR (feature) . PATCH (fix).

Debugging

CommandWhat it does
git blame -L 1,10 FILEwho changed these lines
git bisect start BAD GOODstart a binary search
git bisect good / bad / skipmark the checked-out commit
git bisect run ./test.shautomatic: exit 0 good, 1–127 bad, 125 skip
git bisect resetfinish and go back
git cherry-pick COMMIT / -xcopy a commit / note where it came from
git cherry-pick A..Bcopy a range
git reflogevery position HEAD has been
git grep -n "text"search tracked files

Hooks

ItemWhat it does
.git/hooks/pre-commitruns before a commit; non-zero exit stops it
.git/hooks/commit-msgchecks the message (file path in $1)
.git/hooks/pre-pushruns before a push (tests)
chmod +x .git/hooks/NAMEhooks must be executable
git commit --no-verifyskip local hooks
git config core.hooksPath .githooksshare hooks in the repository
pre-commit install / pre-commit run --all-filesthe pre-commit framework

Internals and plumbing

CommandWhat it does
cat .git/HEADcurrent branch reference
git rev-parse HEAD / --short HEADcommit hash (short)
git rev-parse --abbrev-ref HEADcurrent branch name
git rev-parse --show-toplevelrepository root
git cat-file -t / -p OBJECTobject type / content
git cat-file -p HEAD^{tree}the root tree
git ls-tree HEAD / git ls-filestree entries / tracked files
git hash-object FILEblob hash of a file
git count-objects -vH / git fsck / git gcsize / integrity / pack
git status --porcelainstable output for scripts
git diff --name-only origin/main...HEADchanged files in an MR (CI)

Large repositories

CommandWhat it does
git submodule add URL PATHembed a repo pinned to a commit
git submodule update --init --recursivefetch submodules
git lfs install / git lfs track "*.onnx"large files via LFS (commit .gitattributes)
git lfs ls-files / git lfs pulllist / download LFS files
git clone --filter=blob:none --sparse URLpartial clone + sparse checkout
git sparse-checkout set DIR1 DIR2 / list / disableonly some folders
git config core.fsmonitor truefaster git status
git config core.untrackedCache true / feature.manyFiles truemore speed settings
git maintenance startbackground optimisation

Security

CommandWhat it does
git config --global gpg.format sshsign with SSH keys
git config --global user.signingkey ~/.ssh/key.pubwhich key signs
git config --global commit.gpgsign truesign every commit
git commit -S -m "msg"sign one commit
git verify-commit HEAD / git log --show-signaturecheck signatures
Protected branches, required approvals, pipelines must pass, CODEOWNERSenforce on GitLab/GitHub
gitleaks in pre-commit and CIblock secrets

Branching strategies at a glance

StrategyBranchesBest for
Trunk-based / GitHub Flowmain + short branches + feature flagsservices deployed continuously
GitLab Flowmain → staging → production, or release/*staged or versioned delivery
GitFlowdevelop, feature/, release/, hotfix/*, mainscheduled 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.