Cheat Sheet Library

Git Cheat Sheet

Version control for your code. Working folder → git add → staging → git commit → local repo → git push → remote (GitHub).

  • 62 entries
  • 12 sections
  • Updated
  • Reviewed
  • Click a command to see all its options

62 entries

On this page

First-Time Setup #

CommandAction
Your name on commits
Your email on commits
New repos start on main
Use VS Code for commit messages
rebases instead of merging
Show all settings

Start a Repo #

CommandAction
Turn the current folder into a repo
Copy a remote repo
Clone into a specific folder

Daily Workflow #

CommandAction
What changed? (use it all the time)
Stage a file
Stage everything in this folder
Stage piece by piece (interactive)
Commit staged changes
Stage all tracked files and commit
Get the remote changes (stops if branches diverged; see pull.rebase)
Send your commits to the remote
First push of a new branch (sets upstream)

Branches #

CommandAction
List local branches (-a includes remote ones)
Switch branch
Create and switch
Merge name into the current branch
Replay your branch on top of main
Delete a merged branch (-D = force)
Delete a remote branch
Rename the current branch

Inspect #

CommandAction
Compact history with branches
History of one file with its changes
Unstaged changes
Staged changes
Difference between branches
One commit in detail
Who changed each line
Commits per author

Undo (from safe to dangerous) #

CommandAction
⚠ Discard unstaged changes in a file (can't be undone)
Unstage (keeps the changes)
Fix the last commit message / add forgotten files
Undo the last commit, keep changes staged
Undo the last commit, keep changes unstaged
New commit that reverses an old one (safe for pushed commits)
⚠ Delete the last commit and its changes
/ Preview / ⚠ delete untracked files and folders
Every place HEAD has been; rescue "lost" commits

Never reset --hard or push --force on shared branches. If you must, use git push --force-with-lease.

Stash (park changes) #

CommandAction
Park uncommitted changes
Include untracked files
List stashes
Re-apply the latest stash and remove it
Apply a specific stash and keep it
Delete the latest stash

Remotes #

CommandAction
Show remotes
Link a remote
Change the URL (for example HTTPS → SSH)
Download without merging
Pull and put your commits on top

Tags & Releases #

CommandAction
Lightweight tag
Annotated tag
Push tags

Advanced but Useful #

CommandAction
Copy one commit onto the current branch
Squash, reorder or edit the last 3 commits
/ good / badBinary-search for the commit that caused a bug
Check out a second branch in another folder
Stop tracking a file (keeps it on disk)

.gitignore #

node_modules/
dist/
.env
*.log
.DS_Store
.vscode/*
!.vscode/settings.json

Already committed a file that should be ignored? git rm --cached file, then commit.

Merge Conflicts #

  1. git status shows the conflicted files.
  2. Edit the files and keep what you need between <<<<<<<, ======= and >>>>>>>.
  3. git add file → git commit (or git rebase --continue).
  4. Panic button: git merge --abort / git rebase --abort.