All cheat sheets

Cheat sheet

Git

The everyday Git workflow, plus how to get out of trouble when a commit, a branch or a rebase goes sideways.

Nothing you type here is sent or saved. Privacy policy

Fill in a branch name or a message once and every command below uses it. Messages and names with spaces are quoted for you.

Getting started

git clone git@example.com:me/project.git

Copy a repository, with its whole history, into a new folder.

git init

Start a new repository in the current folder.

git config --global user.name 'Jo Bloggs'

Set the name that goes on your commits.

git config --global user.email jo@example.com

Set the email that goes on your commits.

Everyday work

git status -sb

What's changed, in a short list, with the branch at the top.

-s
short format
-b
show the branch and how far ahead or behind it is
git diff

The changes you haven't staged yet.

git diff --staged

Exactly what the next commit will contain.

git add README.md

Stage a file for the next commit.

git add -p

Go through your changes piece by piece and pick what to stage. y yes, n no, s split it smaller.

git commit -m 'Fix the login redirect'

Commit what's staged.

git commit --amend --no-edit

Add what's staged to your last commit. Only before you've pushed it.

git pull --rebase

Get the latest, and put your own commits on top of it instead of making a merge commit.

git push -u origin feature/login

The first push of a new branch. After this, plain git push is enough.

-u
remember where this branch pushes to

Branches

git switch -c feature/login

Make a new branch and move to it.

-c
create
git switch feature/login

Move to a branch that already exists.

git branch -a

Every branch, including the ones on the remote.

git merge feature/login

Bring another branch's work into the one you're on.

git rebase feature/login

Replay your branch's commits on top of another branch, for a straight-line history.

git branch -d feature/login

Delete a branch you've finished with. Git refuses if it isn't merged yet (-D insists).

git push origin --delete feature/login

Delete the branch on the remote too.

git fetch --prune

Catch up with the remote, and forget branches that were deleted there.

Looking back

git log --oneline --graph -20

The last 20 commits, one line each, with branches drawn in.

git log -p -- README.md

Every change ever made to one file.

-p
show the change itself, not just the message
--
everything after this is a file name, not a branch
git show a1b2c3d

One commit: who, when, why and what.

git blame README.md

Who last changed each line, and in which commit.

git log -S TODO --oneline

The commits that added or removed some text. Finds when something appeared or vanished.

git grep -n TODO

Search every tracked file. Faster than grep, and it skips what Git ignores.

Undoing things

git restore README.md

Throw away your unsaved-to-Git changes to a file. They're gone for good.

git restore --staged README.md

Unstage a file, keeping the changes.

git restore --source=a1b2c3d README.md

Put a file back the way it was in an older commit.

git reset --soft HEAD~1

Undo the last commit, but keep its changes staged, ready to commit again.

git revert a1b2c3d

Make a new commit that undoes an old one. The safe way to undo something already pushed.

git reset --hard origin/feature/login

Make your branch match the remote exactly. Throws away your local commits and changes, so be sure.

git reflog

Everywhere your branch has pointed lately. How to find a commit "lost" to a bad reset or rebase.

git clean -n

Show the untracked files that git clean -f would delete. Look before you leap.

git clean -fd

Delete untracked files and folders.

-f
force (Git won't without it)
-d
folders too

Stashing

git stash push -u -m 'Fix the login redirect'

Park your changes for later, so you can switch branches with a clean slate.

-u
include new files Git isn't tracking yet
-m
a label, so you can tell stashes apart
git stash list

Everything you've parked.

git stash pop

Bring the latest stash back, and drop it from the list.

Rewriting history

git rebase -i HEAD~3

Tidy up your last few commits before you push: reorder, squash, reword or drop them.

pick
keep it as it is
reword
keep it, but change the message
squash
fold it into the commit above
drop
remove it
git rebase --continue

Carry on after fixing a conflict (git add the fixed files first).

git rebase --abort

Give up and go back to exactly where you were before the rebase.

git cherry-pick a1b2c3d

Copy one commit from another branch onto this one.

Tags and remotes

git tag -a v1.2.0 -m 'Fix the login redirect'

Mark a release.

-a
an annotated tag, with a message, date and author
git push origin v1.2.0

Push a tag. Plain git push leaves tags behind.

git remote -v

Where this repository fetches from and pushes to.

git remote add origin git@example.com:me/project.git

Add another remote.

git remote set-url origin git@example.com:me/project.git

Point a remote at a new address, after a move.