Tokenova Learn

Office-Ready Git · provided by Tokenova

Git cheat sheet

The same task in the terminal, VS Code and GitHub Desktop. Every line links back to the day that teaches it.

PowerShell tip: put HEAD@{1} and stash@{0} in single quotes. Press q to leave a long log or diff.

To print this page, press Ctrl+P.

Open the interactive version

Setup

TaskTerminalVS CodeGitHub DesktopDay
Check which Git version you havegit --versionUse the terminalUse the terminalDay 1
Set the name on your commitsgit config --global user.name "Your Name"Use the terminalFile › Options… › GitDay 1
Set the email on your commitsgit config --global user.email "you@example.com"Use the terminalFile › Options… › GitDay 1
Start new repositories on maingit config --global init.defaultBranch mainUse the terminalFile › Options… › GitDay 1
Read one global settinggit config --global --get core.editorUse the terminalUse the terminalDay 1
Show one setting and the file it comes fromgit config --show-origin --get init.defaultBranchUse the terminalUse the terminalDay 1
Check your laptop's Git setup.\lab doctorRun it in the terminal panelUse the terminalDay 1
Always prune when fetching (a Day 1 course setting)git config --global fetch.prune truegit.pruneOnFetchNot needed: Desktop's fetch prunesDay 8
Make plain git pull rebase instead of merge (a Day 1 course setting)git config --global pull.rebase trueUse the terminalUse the terminalDay 10
Allow only fast-forward pulls (if that's your team's rule)git config --global pull.ff onlyUse the terminalUse the terminalDay 10
See a setting's value and which file sets itgit config --show-origin --get pull.rebaseUse the terminalUse the terminalDay 10
Make VS Code the editor Git opensgit config --global core.editor "code --wait"Use the terminalFile › Options… › Integrations › External editor (only for Desktop's own Open in editor button)Reference
See all settings and where each comes fromgit config --list --show-originUse the terminalUse the terminalReference
Get help for any commandgit help mergeUse the terminalUse the terminalReference
Quick options summary for a commandgit merge -hUse the terminalUse the terminalReference
Leave the scrolling log or diff viewqPress q in the terminalNot neededReference

Basics

TaskTerminalVS CodeGitHub DesktopDay
Turn a folder into a repositorygit initCommand Palette › Git: Initialize RepositoryUse the terminalDay 2
See what's changed and what's stagedgit statusView › Source ControlChangesDay 2
Short status, one line per filegit status -sView › Source ControlChangesDay 2
Stage one filegit add transform.pyCommand Palette › Git: Stage ChangesTick the file in ChangesDay 2
Stage every change (read git status first)git add .Command Palette › Git: Stage All ChangesTick every file in ChangesDay 2
Commit what's stagedgit commit -m "Add .gitignore"CommitCommit n files to branchDay 2
Unstage a file, keep the editsgit restore --staged .envCommand Palette › Git: Unstage ChangesUntick the file in ChangesDay 2
Unstage everything, keep the filesgit resetCommand Palette › Git: Unstage All ChangesUntick every file in ChangesDay 2
Throw away unstaged edits to a filegit restore report.pyCommand Palette › Git: Discard ChangesDiscard changes…Day 2
Stop tracking a file but keep it on diskgit rm --cached .envUse the terminalUse the terminalDay 2

Ignore

TaskTerminalVS CodeGitHub DesktopDay
See which .gitignore rule matches a filegit check-ignore -v .envUse the terminalUse the terminalDay 2
See which .gitignore rule hides a pathgit check-ignore -v playgroundUse the terminalUse the terminalDay 7

History

TaskTerminalVS CodeGitHub DesktopDay
Show history, one line per commitgit log --onelineGraphHistoryDay 3
Create the git lg alias (once per computer; Day 1 step 5 did it)git config --global alias.lg "log --oneline --graph --decorate --all"Use the terminalUse the terminalDay 3
Show the whole graph, every branchgit lgGraphHistory (one branch at a time)Day 3
Show one commit and its changesgit show 3636402Click the commit in GraphClick the commit in HistoryDay 3
Compare two commitsgit diff 3636402 e9797deUse the terminalUse the terminalDay 3
List the commits that changed a filegit log --oneline -- report.pyTimelineUse the terminalDay 3
Find the commit that added a filegit log --diff-filter=A --oneline -- report.pyUse the terminalUse the terminalDay 3
Find commits that added or removed some textgit log -S "0.08" --onelineUse the terminalUse the terminalDay 3
List one person's commitsgit log --author="Alex" --onelineUse the terminalUse the terminalDay 3
See who last changed each linegit blame transform.pyCommand Palette › Git: Toggle Git Blame Editor DecorationUse the terminalDay 3

Commit

TaskTerminalVS CodeGitHub DesktopDay
Commit with a subject and a bodygit commit -m "Subject" -m "Why it was needed"Subject, blank line, body in the message boxSummary (required) and DescriptionDay 3
Fix the message of your last (unpushed) commitgit commit --amend -m "Restore the 8% sales tax rate"Command Palette › Git: Commit (Amend)Amend commit…Day 3
Add a forgotten file to your last (unpushed) commitgit commit --amend --no-editStage the file, then Command Palette › Git: Commit (Amend)Amend commit…Day 3
Park unfinished work in a commit and back it upgit commit -am "WIP: CSV export of the report"Commit, then Publish BranchCommit n files to branch, then Publish branchDay 14

Branch

TaskTerminalVS CodeGitHub DesktopDay
Create a branch and switch to itgit switch -c feature/xCommand Palette › Git: Create Branch...Branch › New branch…Day 4
Switch to another branchgit switch mainCommand Palette › Git: Checkout to... or branchCurrent branchDay 4
Go back to the previous branchgit switch -Use the terminalUse the terminalDay 4
List your branchesgit branchbranchCurrent branchDay 4
Delete a branch that isn't merged (throws its work away)git branch -D feature/xCommand Palette › Git: Delete Branch...Branch › Delete…Day 4
Visit an old commit (detached HEAD)git switch --detach HEAD~2Use the terminalCheckout commitDay 4
Keep commits made in detached HEADgit switch -c rescue/header-fixCommand Palette › Git: Create Branch...Create branch from commitDay 4
Delete a branch you've mergedgit branch -d feature/xCommand Palette › Git: Delete Branch...Branch › Delete…Day 5
See what a branch has that main doesn'tgit log main..docs/contributing-tipsUse the terminalBranch › Compare to branchDay 8
Work on a teammate's branch (linked to GitHub's)git switch alex/fix-roundingCommand Palette › Git: Checkout to..., pick origin/alex/fix-roundingCurrent branchDay 10
Copy one commit onto the current branchgit cherry-pick 843a926Command Palette › Git: Cherry Pick...Cherry-pick commit…Day 11
See each branch's GitHub partner, and which ones are gonegit branch -vvUse the terminalUse the terminalDay 14

Stash

TaskTerminalVS CodeGitHub DesktopDay
Shelve uncommitted changesgit stash push -m "currency wip"Command Palette › Git: StashBranch › Stash all changesDay 4
Shelve changes including new filesgit stash push -u -m "currency wip"Command Palette › Git: Stash (Include Untracked)Use the terminalDay 4
See what's on the stash shelfgit stash listCommand Palette › Git: Pop Stash... lists themStashed ChangesDay 4
Bring back the newest stashgit stash popCommand Palette › Git: Pop Stash...RestoreDay 4
Peek inside a stashgit stash show -p 'stash@{0}'Use the terminalView › Show stashed changesDay 4

Merge

TaskTerminalVS CodeGitHub DesktopDay
Merge a branch into the one you're ongit merge feature/xCommand Palette › Git: Merge...Branch › Merge into current branch…Day 5
Merge only if it can fast-forwardgit merge --ff-only feature/xUse the terminalUse the terminalDay 5
Always create a merge commitgit merge --no-ff feature/xUse the terminalUse the terminalDay 5

Conflict

TaskTerminalVS CodeGitHub DesktopDay
See which files are conflictedgit statusView › Source Control, under Merge ChangesThe Resolve conflicts before Merge dialogDay 5
Mark a conflicted file as resolvedgit add transform.pyCommand Palette › Git: Stage ChangesAutomatic once the markers are goneDay 5
Finish a merge after resolvinggit commit --no-editContinueContinue mergeDay 5
Cancel a merge in progressgit merge --abortCommand Palette › Git: Abort MergeAbort mergeDay 5

Remote

TaskTerminalVS CodeGitHub DesktopDay
Clone a repositorygit clone https://github.com/acme/invoice-pipeline.gitCommand Palette › Git: CloneFile › Clone repository…Day 6
See where origin pointsgit remote -vUse the terminalRepository › Repository settings…Day 6
List local and remote-tracking branchesgit branch -aCommand Palette › Git: Checkout to...Current branchDay 6
Download news from GitHub without changing your workgit fetchCommand Palette › Git: FetchFetch originDay 6
Connect a local repo to a GitHub repogit remote add origin https://github.com/you/repo.gitCommand Palette › Git: Add Remote...Publish repository (creates the GitHub repo too)Day 7
See the remote's URLgit remote -vUse the terminalRepository › Repository settings…Day 7
Switch a remote from HTTPS to SSHgit remote set-url origin git@github.com:you/repo.gitUse the terminalRepository › Repository settings…Day 7
Add the original repository as a remotegit remote add upstream https://github.com/acme/invoice-pipeline.gitCommand Palette › Git: Add Remote...Added for you when you clone a forkBonus B3
List your remotesgit remote -vUse the terminalUse the terminalBonus B3
Download the original's new commitsgit fetch upstreamCommand Palette › Git: Fetch, then pick upstreamFetch originBonus B3

Pull

TaskTerminalVS CodeGitHub DesktopDay
Bring GitHub's new commits into your branchgit pullCommand Palette › Git: PullPull originDay 6
Pull and replay your commits on top (straight line)git pull --rebaseCommand Palette › Git: Pull (Rebase)Pull origin while pull.rebase is true, as on your laptopDay 6
Pull with a merge commit, whatever your settings saygit pull --no-rebaseCommand Palette › Git: Fetch, then Command Palette › Git: Merge... and pick origin/mainUse the terminalDay 6
Join an unrelated GitHub history oncegit pull --no-rebase --allow-unrelated-histories origin mainUse the terminalUse the terminalDay 7

Push

TaskTerminalVS CodeGitHub DesktopDay
Send your commits to GitHubgit pushCommand Palette › Git: Push or Sync ChangesPush originDay 6
Publish a new branch and link itgit push -u origin feature/7-supplier-filterPublish BranchPublish branchDay 6
List commits you haven't pushed yetgit log --oneline '@{u}..HEAD'GraphHistoryDay 6
First push of a branch, and link it to origingit push -u origin mainPublish BranchPublish branchDay 7
Push every local branch and link each onegit push -u origin --allUse the terminalUse the terminalDay 7
Force-push safely after a rebasegit push --force-with-lease --force-if-includesCommand Palette › Git: Push (Force) (needs git.allowForcePush)Force push originDay 10
List commits that are on no remote (unpushed)git log --branches --not --remotes --onelineUse the terminalPush origin shows how many commits are waiting on the current branchDay 14
Publish a rewritten branch of your owngit push --force-with-leaseCommand Palette › Git: Push (Force)Force push originBonus B1

Auth

TaskTerminalVS CodeGitHub DesktopDay
Test your SSH key with GitHubssh -T git@github.comUse the terminalUse the terminalDay 7

Clone

TaskTerminalVS CodeGitHub DesktopDay
Clone into a folder you choosegit clone https://github.com/you/repo.git playground\team\repoCommand Palette › Git: CloneFile › Clone repository…Day 7

Pull requests

TaskTerminalVS CodeGitHub DesktopDay
Push a new branch and link it (before opening a PR)git push -u origin docs/contributing-tipsPublish BranchPublish branchDay 8
Open a pull requestgit push -u origin docs/contributing-tipsPush, then use GitHub.com: Compare & pull requestBranch › Create pull requestDay 8
Sync main after a merge and drop deleted branchesgit pull --pruneCommand Palette › Git: Fetch (Prune), then Command Palette › Git: PullFetch origin (prunes), then Pull originDay 8
Delete a branch whose PR was squash-mergedgit branch -D docs/contributing-tipsCommand Palette › Git: Delete Branch...Branch › Delete…Day 8
Open a pull request from the terminalgh pr create --fillUse the terminalBranch › Create pull requestBonus B5
See your pull requests, checks and reviewsgh pr statusUse the terminalUse the terminalBonus B5
Test a teammate's pull request locallygh pr checkout 12Use the terminalUse the terminalBonus B5
Open this branch's pull request in the browsergh pr view --webUse the terminalBranch › Create pull request (it reads View pull request on GitHub once a PR exists)Bonus B5
Squash-merge a pull request and delete its branchgh pr merge --squash --delete-branchUse the terminalUse the terminalBonus B5

Sync

TaskTerminalVS CodeGitHub DesktopDay
Remove origin/... bookmarks of branches deleted on GitHubgit fetch --pruneCommand Palette › Git: Fetch (Prune)Fetch originDay 8
Pull, with your unpushed commits replayed on topgit pull --rebaseCommand Palette › Git: Pull (Rebase)Pull origin (with rebase while pull.rebase is true)Day 10
Bring the latest main into your branch (merge)git merge origin/mainCommand Palette › Git: Merge...Branch › Update from branchDay 10
Rebase your branch onto the latest maingit rebase origin/mainCommand Palette › Git: Rebase Branch...Branch › Rebase current branch…Day 10
See what GitHub's branch has that yours doesn'tgit log --format="%h %an %s" HEAD..origin/feature/export-csvUse the terminalUse the terminalDay 10
Copy one commit onto your branchgit cherry-pick ba23084Command Palette › Git: Cherry Pick...Cherry-pick commit…Day 10

CI

TaskTerminalVS CodeGitHub DesktopDay
Run the tests exactly like the CI workflowpython -m unittest discover -s tests -t . -vUse the terminalUse the terminalDay 9
Follow a GitHub Actions run livegh run watchUse the terminalUse the terminalBonus B5

Review

TaskTerminalVS CodeGitHub DesktopDay
Bring a committed suggestion (or any GitHub commit) to your laptopgit pullCommand Palette › Git: PullFetch origin, then Pull originDay 9
Add a fix to an open pull requestgit pushCommand Palette › Git: PushPush originDay 9

Rebase

TaskTerminalVS CodeGitHub DesktopDay
Continue a rebase after fixing a conflictgit rebase --continueContinueThe continue button in the conflicts dialogDay 10
Drop the commit a rebase stopped ongit rebase --skipUse the terminalUse the terminalDay 10
Cancel a rebase in progressgit rebase --abortCommand Palette › Git: Abort RebaseThe abort button in the conflicts dialogDay 10
Tidy your branch's commits (squash, reword, reorder, drop)git rebase -i mainRun it in the terminal; the todo list opens as a tabDrag commits in HistoryBonus B1
Commit a fix meant for an earlier commitgit commit --fixup dd82378Use the terminalUse the terminalBonus B1
Fold fixup! commits into their targetsgit rebase -i --autosquash mainUse the terminalUse the terminalBonus B1
Continue after fixing a stop in a rebasegit rebase --continueContinueUse the terminalBonus B1
Cancel a rebase in progressgit rebase --abortUse the terminalUse the terminalBonus B1
Undo a finished rebasegit reset --hard ORIG_HEADUse the terminalUse the terminalBonus B1

Undo

TaskTerminalVS CodeGitHub DesktopDay
Undo the last commit, keep its changes stagedgit reset --soft HEAD~1Command Palette › Git: Undo Last CommitUndoDay 11
Undo the last commit, keep its changes unstagedgit reset HEAD~1Use the terminalReset to commit…Day 11
Undo the last commit and throw its changes awaygit reset --hard HEAD~1Use the terminalUse the terminalDay 11
Undo a pushed commit with a new commitgit revert 700d044Use the terminalRevert changes in commitDay 11
Undo a pushed mergegit revert -m 1 a7ecd9bUse the terminalUse the terminalDay 11
See everywhere HEAD has beengit reflogUse the terminalUse the terminalDay 11
Go back to where you were before the last movegit reset --hard 'HEAD@{1}'Use the terminalUse the terminalDay 11
Save a lost commit on a new branchgit branch rescue/report-work 'HEAD@{1}'Use the terminalCreate branch from commit (if it's still in History)Day 11
Move unpushed commits from main to a new branchgit branch feature/csv-export; git reset --hard origin/mainUse the terminalBranch › New branch…, then Reset to commit… on mainDay 11
Preview which untracked files git clean would deletegit clean -ndUse the terminalUse the terminalDay 11
Delete untracked files and foldersgit clean -fdUse the terminalDiscard changes… (to the Recycle Bin)Day 11

Data

TaskTerminalVS CodeGitHub DesktopDay
See which rule ignores a filegit check-ignore -v .envUse the terminalUse the terminalDay 12
Add a file even though it's ignoredgit add -f data/export.parquetUse the terminalUse the terminalDay 12
Stop tracking a file but keep it on diskgit rm --cached .envUse the terminal, then Command Palette › Git: Add to .gitignoreUse the terminalDay 12
Clear a notebook's outputspython -m nbstripout notebooks/explore.ipynbClear All Outputs, then saveClear them in VS Code firstDay 12
Strip notebook outputs automatically on every commitnbstripout --install --attributes .gitattributesUse the terminal (after pip install nbstripout)Use the terminalDay 12
Keep one whole side of a conflicted filegit restore --theirs notebooks/explore.ipynbUse the terminalUse the modified file from branchDay 12
Undo the last commit (not pushed), keep its changes stagedgit reset --soft HEAD~1Command Palette › Git: Undo Last CommitUndoDay 12
Set up Git LFS (once per computer)git lfs installUse the terminalUse the terminalDay 12
Store a file type with Git LFSgit lfs track "*.parquet"Use the terminalUse the terminalDay 12
List the files stored in LFSgit lfs ls-filesUse the terminalUse the terminalDay 12
Diff without line-ending noisegit diff --ignore-cr-at-eolUse the terminalUse the terminalDay 12
Re-apply .gitattributes line-ending rules to every filegit add --renormalize .Use the terminalUse the terminalDay 12

Release

TaskTerminalVS CodeGitHub DesktopDay
Create an annotated (release) taggit tag -a v1.0.1 -m "Fix tax rounding"Command Palette › Git: Create Tag... (type a message)Create Tag…Day 13
Create a lightweight taggit tag v1.0.0-rc1Command Palette › Git: Create Tag... (leave the message empty)Use the terminalDay 13
List tags with their messagesgit tag -nUse the terminalUse the terminalDay 13
Show a tag and its commitgit show v1.0.1 --no-patchUse the terminalUse the terminalDay 13
Push one taggit push origin v1.0.1Use the terminalYour next Push origin takes itDay 13
Push commits plus their annotated tagsgit push --follow-tagsUse the terminalUse the terminalDay 13
Push every local taggit push --tagsCommand Palette › Git: Push TagsUse the terminalDay 13
See which tags GitHub hasgit ls-remote --tags originUse the terminalUse the terminalDay 13
Delete a local tag (not pushed yet)git tag -d v1.0.1Use the terminalUse the terminalDay 13
Start a branch from a taggit switch -c hotfix/1.0.1 v1.0.0Command Palette › Git: Create Branch From...Create branch from commitDay 13
Copy a hotfix commit onto maingit cherry-pick hotfix/1.0.1Command Palette › Git: Cherry Pick...Cherry-pick commit…Day 13
Start a hotfix branch at a release taggit switch -c hotfix/1.2.1 v1.2.0Command Palette › Git: Create Branch From...History: right-click the tagged commit, Create branch from commitDay 14
Push a branch and a tag in one gogit push origin hotfix/1.2.1 v1.2.1Publish Branch, then Command Palette › Git: Push TagsPublish branch (pushes tags made in Desktop too)Day 14

Bisect

TaskTerminalVS CodeGitHub DesktopDay
Start a bisect sessiongit bisect startUse the terminalUse the terminalBonus B2
Mark the current commit badgit bisect badUse the terminalUse the terminalBonus B2
Mark a known good commitgit bisect good v1.0.0Use the terminalUse the terminalBonus B2
Let Git test every stepgit bisect run python -m unittestUse the terminalUse the terminalBonus B2
Skip a commit you can't testgit bisect skipUse the terminalUse the terminalBonus B2
See your verdicts so fargit bisect logUse the terminalUse the terminalBonus B2
End the session and go backgit bisect resetUse the terminalUse the terminalBonus B2

Fork

TaskTerminalVS CodeGitHub DesktopDay
Catch your main up with the originalgit merge --ff-only upstream/mainCommand Palette › Git: Merge...Branch › Merge into current branch…Bonus B3
Update your fork on GitHubgit push origin mainCommand Palette › Git: PushPush originBonus B3
Delete a merged branch from your forkgit push origin --delete docs/add-contributorUse the terminalBranch › Delete…Bonus B3

Hooks

TaskTerminalVS CodeGitHub DesktopDay
See the example hooksGet-ChildItem .git\hooks -NameUse the terminalUse the terminalBonus B4
Install the team's pre-commit hooks (once per clone)pre-commit installUse the terminalUse the terminalBonus B4
Run all pre-commit hooks on every filepre-commit run --all-filesUse the terminalUse the terminalBonus B4
Commit without running hooks (emergencies only)git commit --no-verify -m "Fix CI config"Use the terminalUse the terminalBonus B4

Worktree

TaskTerminalVS CodeGitHub DesktopDay
Check out a branch in a second foldergit worktree add ../hotfix-1.2.1 hotfix/1.2.1Use the terminalUse the terminalBonus B4
List your worktreesgit worktree listUse the terminalUse the terminalBonus B4
Remove a worktree foldergit worktree remove ../hotfix-1.2.1Use the terminalUse the terminalBonus B4

GitHub

TaskTerminalVS CodeGitHub DesktopDay
Sign in the GitHub CLIgh auth loginUse the terminalNot needed: Desktop signs in on its ownBonus B5

Issues

TaskTerminalVS CodeGitHub DesktopDay
List open issuesgh issue listUse the terminalUse the terminalBonus B5

Internals

TaskTerminalVS CodeGitHub DesktopDay
Show the full id of a commitgit rev-parse HEADUse the terminalUse the terminalBonus B6
Show an object's typegit cat-file -t 683a182Use the terminalUse the terminalBonus B6
Show an object's contentsgit cat-file -p HEADUse the terminalUse the terminalBonus B6
See which branch HEAD points toGet-Content .git\HEADUse the terminalUse the terminalBonus B6
Count loose and packed objectsgit count-objects -vUse the terminalUse the terminalBonus B6