Earn it: GitHub Achievements Academy home

Git and GitHub cheat sheet

Every command from the course on one page. Print it (it fits two sides of a page), or keep it open in a tab. <thing> means "put your own value here".

Set up once

CommandWhat it does
git config --global user.name "Name"Name on your commits
git config --global user.email you@example.comMust be a verified email on GitHub
git config --global pull.rebase truePull rebases instead of making merge commits
git config --global fetch.prune trueClean up deleted remote branches on fetch
git config --global alias.lg "log --oneline --graph --all --decorate"Adds a git lg shortcut
gh auth loginSign in the GitHub CLI

Start a project

CommandWhat it does
git initMake the current folder a repo
git clone <url>Copy a repo from GitHub
gh repo create <name> --public --cloneNew GitHub repo, cloned locally
gh repo fork <owner/repo> --cloneFork and clone in one step

The everyday loop

CommandWhat it does
git statusWhat changed, and what's staged
git diffUnstaged changes, line by line
git diff --stagedWhat the next commit will contain
git add <file> · git add -pStage a file · stage piece by piece
git restore --staged <file>Unstage, keeping your edits
git commit -m "Fix crash on empty cart"Save a snapshot
git log --oneline -10The last 10 commits
git show <ref>One commit in full

Branches

CommandWhat it does
git switch -c <name>Create a branch and move to it
git switch <name> · git switch -Move to a branch · back to the last one
git branch · git branch -vvList branches · with tracking info
git branch -d <name>Delete a merged branch (-D to force)
git merge <branch>Bring a branch into the current one
git merge --abortBack out of a conflicted merge
git rebase mainReplay your commits on top of main
git rebase -i mainTidy commits: reword, squash, fixup, drop
git rebase --continue · --abortCarry on after fixing a conflict · cancel

Remotes

CommandWhat it does
git remote -vList remotes
git remote add upstream <url>Track the project you forked from
git fetchDownload new commits, change nothing locally
git pullFetch, then update your branch
git push -u origin <branch>First push of a new branch
git push --force-with-leaseSafe force-push after a rebase, on your own branch only
gh repo syncUpdate your fork from the original

Undo

SituationCommand
Fix the last commit's messagegit commit --amend -m "New message"
Add a forgotten file to the last commitgit add <file> then git commit --amend --no-edit
Throw away changes to a filegit restore <file> (can't be undone)
Undo the last commit, keep the changesgit reset --soft HEAD~1
Undo a commit that's already pushedgit revert <commit>
Find "lost" commitsgit reflog
Bring back a deleted branchgit branch <name> <commit>
Go back to before a bad rebase or resetgit reset --hard ORIG_HEAD
Keep work made in detached HEADgit switch -c <name>

Full explanations are in the Oh no! guide.

Power tools

CommandWhat it does
git stash push -m "note" · git stash popPark work · bring it back
git stash listSee parked work
git cherry-pick <commit>Copy one commit onto your branch
git bisect start · bad · good <ref> · resetBinary-search for the commit that broke something
git blame -L 10,20 <file>Who last changed those lines, and in which commit
git log -S "text"Commits that added or removed some text
git tag -a v1.0.0 -m "msg" · git push origin v1.0.0Tag a release · push the tag
git rm --cached <file>Stop tracking a file, keep it on disk

Refs: ways to name a commit

RefMeans
HEADWhere you are now
HEAD~1 · HEAD~3One or three commits back
main^2The second parent of a merge commit
origin/mainWhere main was on GitHub when you last fetched
HEAD@{2}Where HEAD was two moves ago (from the reflog)
a1b2c3dAny commit, by the start of its ID

GitHub CLI

CommandWhat it does
gh pr create --fillOpen a PR from the current branch
gh pr checkout 123Check out PR #123 locally
gh pr merge --squash --delete-branchMerge and tidy up
gh issue create · gh issue listCreate · list issues
gh release create v1.2.0 --generate-notesPublish a release
gh run watchFollow an Actions run live

Glossary

WordMeaning
Repository (repo)A project's folder plus its full history
CommitA saved snapshot, with a message and a pointer to its parent
BranchA movable label pointing at one commit
HEADWhere you are now: usually a branch, sometimes a commit
Working treeThe files you see and edit
Staging area (index)What goes into the next commit
CloneA full copy of a repo on your computer
ForkYour own copy of someone else's repo, on GitHub
RemoteAnother copy of the repo, such as origin on GitHub
UpstreamThe branch or repo yours tracks or came from
Pull request (PR)A request to merge one branch into another, with review
MergeCombine the work of two branches
Fast-forwardMerge by moving the label, with no merge commit
RebaseReplay commits on top of another branch, as new commits
Merge conflictBoth sides changed the same lines, so Git asks you to choose
SquashCombine several commits into one
Detached HEADYou're on a commit, not a branch
ReflogGit's local diary of everywhere HEAD has been
TagA fixed label on a commit, usually a version
ReleaseGitHub's page for a tag, with notes and downloads
Action / workflowAutomation that runs on GitHub when something happens

Practice any of this safely in the Git sandbox.

Last reviewed November 2026. Improve this page on GitHub. Every fix you send counts toward Pull Shark.