Skip to content

Git Cheat Sheet

The Git commands you use every day, with what they do.

About Git cheat sheet

Git records snapshots of a project as commits. Changes move through three places: the working tree (your files), the staging area (what the next commit will contain, filled with git add) and the repository (the commits). Branches are movable names for commits, and remotes such as origin are other copies of the repository that you fetch from and push to.

The commands below cover everyday work. Newer Git versions split the old all-purpose checkout command into git switch (change branches) and git restore (discard or unstage changes), which are harder to misuse. Commands marked as destructive throw work away; when in doubt, git reflog lists recent positions of HEAD, so commits that seem lost can usually be recovered.

49 entries

Set up

CommandWhat it doesNote
git config --global user.name "Ada Lovelace"Set the name on your commits
git config --global user.email ada@example.comSet the email on your commits
git config --global init.defaultBranch mainName the first branch of new repositories main
git config --listShow your settings

Start a repository

CommandWhat it doesNote
git initTurn the current folder into a repository
git clone <url>Copy a remote repositorygit clone git@github.com:org/repo.git
git clone --depth 1 <url>Clone only the latest commit (shallow)

Stage and commit

CommandWhat it doesNote
git statusShow changed, staged and untracked files
git add <file>Stage a filegit add src/app.ts
git add -pStage changes hunk by hunk
git add -AStage everything, including deletions
git commit -m "message"Commit staged changes
git commit --amendChange the last commit (message or content)Only before you've pushed it
git rm <file>Delete a file and stage the deletion
git mv <old> <new>Rename a file and stage the rename

Branches

CommandWhat it doesNote
git branchList local branches-a includes remote branches
git switch -c <branch>Create a branch and switch to itOlder: git checkout -b <branch>
git switch <branch>Switch to a branch
git merge <branch>Merge a branch into the current one
git rebase <branch>Replay your commits on top of another branchDon't rebase commits others already have
git branch -d <branch>Delete a merged branch-D forces deletion
git cherry-pick <commit>Apply one commit onto the current branch

Remotes

CommandWhat it doesNote
git remote -vList remotes and their URLs
git fetchDownload new commits without changing your files
git pullFetch and merge (or rebase) the upstream branchgit pull --rebase keeps history linear
git pushUpload commits to the upstream branch
git push -u origin <branch>Push a new branch and set its upstream
git push --force-with-leaseForce-push, but only if nobody else pushed meanwhileSafer than --force

Inspect history

CommandWhat it doesNote
git log --oneline --graphCompact history with branches drawn
git log -p <file>History of one file with its changes
git diffUnstaged changes
git diff --stagedStaged changes
git diff main...<branch>What a branch changed since it left main
git show <commit>One commit and its changes
git blame <file>Who last changed each line
git bisect startBinary-search history for the commit that broke something

Undo

CommandWhat it doesNote
git restore <file>Discard unstaged changes to a fileCannot be undone
git restore --staged <file>Unstage a file, keeping the changes
git revert <commit>Make a new commit that undoes a commitSafe on shared branches
git reset --soft HEAD~1Undo the last commit, keep its changes staged
git reset --hard HEAD~1Throw away the last commit and its changesDestructive
git reflogEvery position HEAD has been at: find lost commits
git clean -nPreview untracked files that git clean -f would delete

Stash and tags

CommandWhat it doesNote
git stashPut uncommitted changes aside-u includes untracked files
git stash popReapply the latest stash and drop it
git stash listList stashes
git tag v1.2.0Tag the current commit
git tag -a v1.2.0 -m "Release"Create an annotated tag
git push --tagsPush tags to the remote

Good to know

Rewriting history

commit --amend, rebase and reset change commits. That's fine for commits that only exist on your machine; for commits others have pulled, use git revert instead, or coordinate and push with --force-with-lease.

More references