Skip to content

Git cheat sheet

The git commands you use every week, grouped by task, plus the safe way to undo things.

26 entries

Setup

git config --global user.name "Ada Lovelace"
git config --global user.email ada@example.com

Identity recorded in every commit.

git config --global init.defaultBranch main

Default branch name for new repos.

git init
git clone https://github.com/user/repo.git

Start a repo or copy an existing one.

Stage & commit

git status
git diff
git diff --staged

What changed; unstaged vs staged diff.

git add file.py
git add -p

Stage a file; -p stages hunk by hunk.

git commit -m "Add login form"

Commit staged changes.

git commit --amend --no-edit

Add staged changes to the last commit (before pushing).

git log --oneline --graph --all

Compact history graph.

Branches

git switch -c feature/login
git switch main

Create and switch; switch back.

git branch
git branch -d feature/login

List; delete a merged branch.

git merge feature/login

Merge a branch into the current one.

git rebase main

Replay your commits on top of main (don’t rebase shared branches).

git rebase -i HEAD~3

Interactive rebase: squash, reword, reorder the last 3 commits.

git cherry-pick a1b2c3d

Copy one commit onto the current branch.

Remotes

git remote -v
git remote add origin URL

List / add remotes.

git fetch
git pull --rebase

Download; update current branch by rebasing.

git push -u origin feature/login

Push and set upstream.

git push --force-with-lease

Safer force-push: refuses if the remote moved.

Undo

git restore file.py

Discard unstaged changes in a file.

git restore --staged file.py

Unstage, keep the changes.

git reset --soft HEAD~1

Undo the last commit, keep changes staged.

git revert a1b2c3d

New commit that reverses an old one (safe on shared branches).

git reflog

Find “lost” commits after a bad reset or rebase.

Stash & tags

git stash push -m "wip"
git stash list
git stash pop

Shelve work in progress and bring it back.

git tag -a v1.2.0 -m "Release 1.2.0"
git push --tags

Annotated release tag.

Feature-branch workflow

git switch main && git pull
git switch -c fix/typo
# edit…
git add -A && git commit -m "Fix typo"
git push -u origin fix/typo
# open a pull request, then after merge:
git switch main && git pull && git branch -d fix/typo

The everyday loop used on GitHub and GitLab.

Frequently asked questions

What is the difference between git fetch and git pull?

fetch downloads new commits from the remote without changing your branch. pull is fetch followed by a merge (or a rebase with --rebase) into your current branch.

Merge or rebase?

Merge keeps history exactly as it happened and is safe on shared branches. Rebase produces a linear history by rewriting your commits; use it on your own local or feature branches before sharing.

How do I undo a pushed commit?

Use git revert <sha>. It creates a new commit that undoes the change without rewriting history that others may have pulled.

Related cheat sheets