Git Commands Cheat Sheet

The git commands you use every week, each with a one-line explanation, plus an undo section organised by situation: fix the last commit, take back a push, recover a deleted branch or move a commit off main. Search for a command or for what went wrong.

Section

49 entries

Set up

git config --global user.name "Your Name"

Name written into every commit you make on this machine.

git config --global user.email "[email protected]"

Email for your commits. Use the one linked to your GitHub or GitLab account so commits show up on your profile.

git config --global init.defaultBranch main

New repositories start on main instead of master.

git init

Turns the current folder into a git repository.

git clone <url> [folder]

Copies a remote repository, with full history, into a new folder.

git clone [email protected]:owner/repo.git
git clone --depth 1 <url>

Shallow clone with only the latest commit. Faster for CI and large repos.

Daily work

git status

What changed, what is staged, and which branch you are on. Run it often.

git add <file>

Stages a file for the next commit. git add . stages everything in the current folder.

git add -p

Stages changes hunk by hunk, so one commit holds one idea.

git commit -m "message"

Records the staged changes with a message.

git commit -m "fix(cart): round GST to two decimals"
git commit -am "message"

Stages every tracked, modified file and commits in one step. New files are not included.

git diff

Unstaged changes. git diff --staged shows what will go into the next commit.

git log --oneline --graph --decorate

Compact history with branch lines. Add -n 20 to limit it.

git show <commit>

The message and changes of one commit.

git blame <file>

Who last changed each line, and in which commit.

Branches

git switch -c <branch>

Creates a branch and moves to it (newer form of git checkout -b).

git switch -c feature/PROJ-142-upi-refunds
git switch <branch>

Moves to an existing branch.

git branch

Lists local branches. -a includes remote ones, -vv shows what each tracks.

git branch -d <branch>

Deletes a merged branch. -D forces delete of an unmerged one.

git branch -m <new-name>

Renames the current branch.

git merge <branch>

Brings another branch into the current one, with a merge commit if they diverged.

git rebase <branch>

Replays your commits on top of another branch for a straight history. Do not rebase commits others have already pulled.

git cherry-pick <commit>

Copies one commit onto the current branch, for example a hotfix from main into a release branch.

Remotes

git remote -v

Lists remotes and their URLs.

git remote add origin <url>

Connects a local repository to a remote.

git fetch

Downloads new commits and branches without changing your files.

git pull

Fetch, then merge (or rebase, if pull.rebase is set) into the current branch.

git pull --rebase

Puts your local commits on top of what was pulled, avoiding merge commits.

git push -u origin <branch>

Pushes a new branch and remembers the remote, so next time git push is enough.

git push --force-with-lease

Overwrites the remote branch after a rebase, but refuses if someone else pushed in the meantime. Safer than --force.

git push origin --delete <branch>

Deletes a branch on the remote.

Stash and tags

git stash

Puts uncommitted changes aside so you can switch branches. -u also stashes new untracked files.

git stash pop

Brings the last stash back and removes it from the list. git stash apply keeps it.

git stash list

All stashes, newest first.

git tag -a v1.4.0 -m "Release 1.4.0"

Creates an annotated tag for a release.

git push origin --tags

Pushes tags, which git push does not send by default.

Undo recipes

Pick the situation you are in. Commands marked "rewrites history" are fine on your own branch, not on one others have pulled.

Discard changes to a file

Throw away unstaged edits to one file and go back to the last commit.

git restore <file>

The edits are gone for good.

Unstage a file

Took git add too far? Keep the edits, just remove them from the next commit.

git restore --staged <file>
Fix the last commit message

Typo in the message of the commit you just made.

git commit --amend -m "new message"

Rewrites history: only before pushing, or push with --force-with-lease.

Add a forgotten file to the last commit

Stage the file, then amend without changing the message.

git add <file>
git commit --amend --no-edit

Rewrites history.

Undo the last commit, keep the changes

The commit goes, your edits stay staged so you can redo it.

git reset --soft HEAD~1

Rewrites history.

Undo the last commit and its changes

Removes the commit and everything in it from your working copy.

git reset --hard HEAD~1

Deletes work. Uncommitted changes are lost too; check git status first.

Undo a commit that is already pushed

Makes a new commit that reverses an old one. Safe on shared branches.

git revert <commit>
Get back a commit after a bad reset

The reflog lists every position HEAD has been at, so "lost" commits can usually be found for about 90 days.

git reflog
git reset --hard <commit-from-reflog>
Committed to the wrong branch

Move the last commit from main to a new branch.

git branch feature/my-work
git reset --hard HEAD~1
git switch feature/my-work

Rewrites main locally; do it before pushing.

Restore a deleted branch

Find the last commit of the branch in the reflog and recreate it there.

git reflog
git branch <branch> <commit>
Stop a merge or rebase in progress

Conflicts got messy? Go back to where you started.

git merge --abort
git rebase --abort
Remove a file from git but keep it on disk

For a file that should have been in .gitignore, such as .env.

git rm --cached .env
echo ".env" >> .gitignore

If the file had secrets and was pushed, rotate them: they remain in history.

Delete untracked files

Preview first with -n, then remove. -d includes folders.

git clean -n
git clean -fd

Deleted files do not go to the recycle bin.

Features

  • Setup, daily work, branches, remotes, stash and tags

  • Thirteen undo recipes written as situations, with the exact commands

  • Warnings on commands that delete work or rewrite shared history

  • Modern commands (git switch, git restore) alongside the classic ones

  • Copy any command in one click

  • Search by command or by problem, such as "undo commit"

Who uses this cheat sheet

Students and new developers

Learn the small set of commands that covers most daily work, without reading the whole manual.

Fixing a mistake quickly

Committed to main, pushed a secret, lost a branch? Find the situation and follow the recipe.

Team onboarding

Share one page with the commands and branch habits your team expects.

Safe habits with git

Reset or revert?

git reset moves your branch back and drops commits from it; that rewrites history, which is fine on your own branch but breaks things for anyone who already pulled. git revert adds a new commit that undoes an old one, so it is the safe choice for main or any shared branch.

The reflog is your safety net

Almost nothing committed is lost straight away. git reflog lists where HEAD has been, including commits a reset removed, and git keeps those entries for about 90 days by default. Uncommitted changes are different: git restore and git reset --hard remove them for good.

Before you force-push

  • Use --force-with-lease, which refuses if someone pushed after your last fetch.
  • Never force-push to main unless the team agreed.
  • If a secret was pushed, removing the commit is not enough: rotate the secret.

Git Commands Cheat Sheet - FAQ