Skip to content
ohsudo, Home
CI/CD

Git Cheat Sheet

The Git commands you use every day, and how to undo almost anything: commits, staging, branches, merges, stash, remotes and recovering lost work

Updated November 6, 2026 · Applies to Git 2.30+

Setup

Terminal window
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase false # merge on pull (true = rebase)
git config --list --show-origin # what's set, and where

Start

Terminal window
git init # new repository here
git clone git@github.com:acme/app.git # copy an existing one
git clone --depth 1 URL # only the latest commit
git remote add origin URL # connect to a remote
git remote -v # which remotes?

Every day

Terminal window
git status # what changed?
git diff # changes not staged yet
git diff --staged # changes about to be committed
git add file.txt # stage a file
git add -p # stage part of a file, piece by piece
git commit -m "Message" # commit what's staged
git pull # fetch and merge
git push # send your commits
git push -u origin my-branch # first push of a new branch

Undo

Terminal window
git restore file.txt # discard changes in a file (they're gone)
git restore --staged file.txt # unstage, and keep the changes
git commit --amend # fix the last commit (message or content)
git reset --soft HEAD~1 # undo the last commit, keep it staged
git reset HEAD~1 # undo the last commit, keep changes unstaged
git reset --hard HEAD~1 # undo the last commit and DELETE its changes
git revert abc1234 # new commit that undoes an old one (safe if pushed)
git clean -nd # list untracked files that would be removed
git clean -fd # remove them

Warning

reset --hard, restore and clean -f delete uncommitted work for good, as Git never saved it. On commits you’ve already pushed and others may have, use revert, not reset.

Recover

Terminal window
git reflog # everywhere HEAD has been, even after a reset
git reset --hard HEAD@{1} # go back to where you just were
git branch rescue abc1234 # put a branch on a "lost" commit
git checkout abc1234 -- file # get one file back from a commit
git log --diff-filter=D -- file # in which commit was this file deleted?

Branches

Terminal window
git branch # local branches
git branch -a # with the remote ones
git switch my-branch # go to a branch
git switch -c new-branch # create it and go there
git branch -d my-branch # delete, if it's merged
git branch -D my-branch # delete, even if it isn't
git branch -m old new # rename
git push origin --delete old # delete on the remote
git fetch --prune # forget remote branches that are gone

Merge and rebase

Terminal window
git merge feature # bring feature into this branch
git merge --abort # give up on a merge with conflicts
git rebase main # replay this branch on top of main
git rebase --continue # after fixing a conflict
git rebase --abort # back to where you started
git cherry-pick abc1234 # copy one commit here
git checkout --theirs file # in a conflict, take their version
git checkout --ours file # in a conflict, take ours

In a rebase, “ours” and “theirs” swap places: “ours” is the branch you’re replaying onto.

Stash

Terminal window
git stash # set changes aside
git stash -u # with untracked files
git stash list # what's there
git stash pop # bring back the latest, and drop it
git stash apply stash@{1} # bring back one, and keep it
git stash drop stash@{1} # delete one

Look around

Terminal window
git log --oneline --graph --all -n 20 # the shape of history
git log -p file # every change to a file
git log --author="Ana" --since="2 weeks ago"
git log -S"functionName" # commits that added or removed this text
git show abc1234 # one commit
git blame file # who last changed each line
git diff main...feature # what feature adds since it branched

Remotes

Terminal window
git fetch origin # get updates, change nothing
git pull --rebase # replay your commits on top
git push --force-with-lease # overwrite, unless the remote has moved
git remote set-url origin URL # change the address
git tag v1.2.3 # tag this commit
git push origin v1.2.3 # send a tag

Common errors