Cheat sheets /

Git cheat sheet

The git commands you actually use, and the ones you always have to look up: undoing the last commit, cleaning up branches, finding who changed a line, and getting back a commit you thought you lost.

Download all 51 for Snippet Keeper Get Snippet Keeper

In Snippet Keeper, click Import at the bottom of the sidebar and choose the file. They land in your git app, and ones you already have are skipped. The highlighted parts become fill-in fields: type a value (or pick one), and the finished command is on your clipboard.

Setup

Set your name

git config --global user.name "name"

Set your email

git config --global user.email "email"

Set the default branch name for new repos

git config --global init.defaultBranch branch

Clone a repository

git clone url

Clone only recent history

Much faster for big repositories when you don’t need the full history.

git clone --depth depth url

Everyday

Short status with branch info

git status -sb

Stage parts of a file

Walks through each change and asks whether to stage it.

git add -p

Commit with a message

git commit -m "message"

Add changes to the last commit

Stage your fixes first. Don’t amend commits you’ve already pushed to a shared branch.

git commit --amend --no-edit

Show unstaged changes

git diff

Show staged changes

git diff --staged

Branches

Create a branch and switch to it

git switch -c branch

Switch back to the previous branch

git switch -

List branches, most recent first

git branch --sort=-committerdate

Rename the current branch

git branch -m new_name

Delete a merged branch

Use -D to delete a branch that hasn’t been merged.

git branch -d branch

Delete a remote branch

git push origin --delete branch

Delete every local branch already merged

Keeps the current branch and the base branch.

git branch --merged base | grep -vE '^\*|^\s*base$' | xargs git branch -d

Syncing

Fetch and drop deleted remote branches

git fetch --prune

Pull, rebasing your commits on top

git pull --rebase

Push a new branch and track it

git push -u origin HEAD

Force push without clobbering others’ work

Refuses if someone else pushed since you last fetched.

git push --force-with-lease

Add a remote

git remote add name url

Update a fork from upstream

git fetch upstream && git rebase upstream/branch

Undo

Undo the last commit, keep the changes

git reset --soft HEAD~count

Unstage a file

git restore --staged file

Discard changes to a file

git restore file

Make the branch match the remote

Throws away local commits and changes.

git fetch && git reset --hard origin/branch

Revert a commit with a new commit

The safe way to undo something that’s already pushed.

git revert commit

Find a lost commit

Every place HEAD has been. Find the commit, then git switch -c rescue <commit>.

git reflog

Preview deleting untracked files

Lists what would be deleted. Run again without -n to delete.

git clean -fdn

Stash

Stash everything, including new files

git stash push -u -m "message"

List stashes

git stash list

Apply the latest stash and drop it

git stash pop

Apply a specific stash

git stash apply stash@{index}

Stash just one file

git stash push file

History

One-line log graph

git log --oneline --graph --decorate --all -n count

History of a file, with diffs

git log -p --follow -- file

Search commit messages

git log --oneline --grep="text"

Find commits that added or removed text

git log -S "text" --oneline

Who last changed these lines

git blame -L start,end file

Show a file as it was at a commit

git show commit:path

Commits on this branch that aren’t on main

git log --oneline base..HEAD

Rewriting

Interactive rebase

Squash, reword or reorder commits before opening a pull request.

git rebase -i base

Squash fixups automatically

Pairs with git commit --fixup <commit>.

git rebase -i --autosquash base

Copy a commit onto this branch

git cherry-pick commit

Abort a rebase or merge in progress

git operation --abort

Find the commit that broke something

Then mark each commit good or bad; git bisect reset when done.

git bisect start && git bisect bad && git bisect good good_commit

Tags and worktrees

Create an annotated tag

git tag -a tag -m "message"

Push tags

git push origin --tags

Check out a branch in another folder

Work on two branches at once without stashing.

git worktree add ../folder branch

Keep these one keystroke away.

Snippet Keeper is a Mac app for the commands you keep looking up. Press ⌥⇧Space in any app, type a few letters, fill in the blanks, and it’s on your clipboard.

Snippet Keeper asking for the values in a kubectl command before copying it