Emergency server help: get in touch

Git Cheat Sheet: Branches, Undo, Remotes and Deploys

The Git commands you need day to day, from first commit to undoing mistakes, plus a push-to-deploy setup for servers.

Published 6 min read

Short answer: Day to day, Git comes down to git status, git add, git commit, git log and git diff. You also need git switch and git merge for branches, git fetch, git pull and git push for remotes. To undo, use git restore for files, git revert for shared commits and git reset for local ones. git reflog finds commits you thought were lost. The tables below group the commands by task.

The examples in this Git cheat sheet were run with Git 2.43.0 in a scratch repository on 9 October 2026, including the merge, rebase, revert, reset, stash and push-to-deploy steps.

Setup and first commit

CommandWhat it does
git config --global user.name "Jane Admin"Name recorded in commits (also set user.email)
git config --list --show-originEvery setting and the file it comes from
git init -b mainNew repository with main as the first branch
git clone https://example.com/repo.gitCopy a remote repository. Use git@host:repo.git for SSH
git status --shortCompact status: ?? untracked, M modified, M staged
git add nginx.conf / git add -AStage one file / everything, including deletions
git add -pStage hunk by hunk; answer y or n for each change
git commit -m "Add nginx config"Commit what is staged
git commit -am "msg"Stage tracked files and commit in one step (new files are not included)
git check-ignore -v .envWhich .gitignore line ignores a file

A .gitignore for a config repository usually starts with secrets and noise:

*.log
.env
*.key
*.pem

Viewing changes and history

CommandWhat it does
git diffUnstaged changes
git diff --stagedWhat the next commit will contain
git diff --stat HEAD~3Files changed in the last three commits, with line counts
git log --oneline --graph --decorate --allCompact history of every branch as a graph
git log --since="2026-10-01" --author="Jane" --format='%h %an %ad %s' --date=shortFilter by date and author with a custom format
git log -p -S 'ssl_protocols' -- nginx.confCommits that added or removed a string (the “pickaxe”)
git show --stat HEADLast commit: message and files
git show HEAD~2:nginx.confA file as it was two commits ago
git blame -L 10,20 nginx.confWho last changed lines 10 to 20, and in which commit
git shortlog -sn HEADCommit count per author
git grep -n gzipSearch tracked files only

In a script or cron job, give git shortlog an explicit revision such as HEAD. Without one, and with no terminal attached, it waits to read a log from standard input, and our test run hung until it was stopped.

Branches and merging

CommandWhat it does
git switch -c feature/tlsCreate a branch and move to it (older form: git checkout -b)
git switch mainMove to an existing branch
git branch -aLocal and remote-tracking branches
git branch --show-currentCurrent branch name, handy in prompts and scripts
git merge --no-ff feature/tlsMerge and always create a merge commit
git merge --ff-only feature/tlsMerge only if no merge commit is needed; otherwise stop
git rebase mainReplay the current branch on top of main for a straight history
git cherry-pick 8605c9cCopy one commit onto the current branch
git branch --mergedBranches already merged into the current one, safe to delete
git branch -d feature/tls / -DDelete a merged branch / force-delete an unmerged one

Only rebase commits that nobody else has pulled. Rebasing rewrites commit IDs, and anyone who based work on the old ones will have to untangle it.

Undoing changes

SituationCommand
Discard edits to a filegit restore nginx.conf
Unstage a file but keep the editsgit restore --staged nginx.conf
Bring back an older version of a filegit restore --source=HEAD~2 nginx.conf
Fix the last commit message or add a forgotten filegit add file then git commit --amend -m "New message"
Undo a pushed commit safelygit revert <commit> (adds a new commit that reverses it)
Undo a merge commitgit revert -m 1 <merge-commit> (without -m, Git refuses)
Remove the last commit, keep its changes stagedgit reset --soft HEAD~1
Remove the last commit, keep changes unstagedgit reset HEAD~1 (the default --mixed)
Throw away the last commit and all local changesgit reset --hard HEAD~1
Find a lost commitgit reflog, then git switch -c rescue <hash>
Delete untracked filesgit clean -n to preview, then git clean -f (-d for directories)

git reset --hard and git clean -f delete uncommitted work for good. The reflog can bring back commits, but never changes that were not committed. Use revert, not reset, on branches that others have pulled.

Stashing work in progress

CommandWhat it does
git stash push -m "wip readme"Put tracked changes aside and clean the working tree
git stash -uInclude untracked files as well
git stash listStashes, newest as stash@{0}
git stash popRe-apply the newest stash and drop it
git stash apply stash@{1}Re-apply a stash but keep it in the list
git stash drop stash@{1}Delete one stash

Remotes, fetch, pull and push

CommandWhat it does
git remote -vConfigured remotes and their URLs
git remote add origin git@example.com:ops/configs.gitAdd a remote
git push -u origin mainFirst push; sets upstream so later git push and git pull need no arguments
git fetch originDownload new commits without touching your files
git status -sbShows [behind 1] or [ahead 2] against the upstream
git log --oneline main..origin/mainCommits on the remote that you do not have yet
git pull --rebaseFetch, then replay your local commits on top
git push --force-with-leaseForce-push only if the remote has not moved since your last fetch. Safer than --force

Tags and releases

CommandWhat it does
git tag -a v1.0 -m "First release"Annotated tag (stores author, date and message)
git tag -lList tags
git describe --tagsNearest tag, plus commits since it, for version strings
git push origin v1.0 / git push origin --tagsTags are not pushed by default
git switch --detach v1.0Check out a tag to inspect it

Deploying configs with Git

For server admins, a bare repository with a post-receive hook is a simple way to deploy configuration or a static site on push. On the server:

git init --bare /srv/git/site.git
cat > /srv/git/site.git/hooks/post-receive <<'EOF'
#!/bin/sh
git --work-tree=/var/www/site --git-dir=/srv/git/site.git checkout -f main
EOF
chmod +x /srv/git/site.git/hooks/post-receive

On your workstation, git remote add live ssh://deploy@203.0.113.10/srv/git/site.git and then git push live main. We tested this flow with local paths: each push updated the work tree straight away. The hook runs as the SSH user, so that user needs write access to the target directory.

Never commit private keys, .env files or database passwords, even to a private repository. Removing them later does not remove them from history. If one slips in, rotate the secret first, then clean up.

Official documentation (checked 9 October 2026): Git Reference · Pro Git book · git-restore documentation · git-switch documentation · git-stash documentation · gitignore documentation

Git Cheat Sheet at a glance

Git Cheat Sheet – short answer
In short: The Git commands you need day to day, from first commit to undoing mistakes, plus a push-to-deploy setup for servers.
Git Cheat Sheet – what this guide covers
Git Cheat Sheet: what this guide covers.

Related: Git Commands: 42 Everyday Commands with Examples · .gitignore Generator · Diff Checker: Compare Two Texts.

Reading a lot of our guides? Go ad-free with srvScripts Pro for $6 a month. It also pays for new guides. Cancel any time.

Share this guide

Free website test

Is your website set up right?

Check SSL, security headers, redirects, robots.txt, sitemap, llms.txt and security.txt in one test. It takes about 30 seconds.