Git Cheat Sheet

This page condenses everyday Git into one page: staging, committing, branching, remotes, stashing, and undoing mistakes, with examples for JavaScript projects. You can also download the information as a printable cheat sheet:

Free Bonus: Git Cheat Sheet

Get the Git Cheat Sheet (PDF), a printable reference to setup, staging and commits, .gitignore, branches and merging, remotes, stash, and undoing mistakes:

Git Cheat Sheet

Want a guided path instead of a reference? Start learning at realjs.com.

Getting Started

  • Git runs locally; GitHub is just one place to host a repo
  • --global settings apply to every repo on your machine
  • Run git status early and often
  • git help <command> opens any command’s manual
Language: Shell Filename: Install Check and Identity
$ git --version
git version 2.43.0
$ git config --global user.name "Your Name"
$ git config --global user.email \
    "[email protected]"
$ git config --global init.defaultBranch main
Language: Shell Filename: Start or Copy a Repository
$ mkdir hello-git && cd hello-git
$ git init         # Creates hidden .git/
$ git clone <url>  # Copy an existing repo

Stage and Commit

  • Working directory → git add → staging area → git commit → history
  • Commit one logical change at a time
  • Messages: imperative mood, first line ≤ 50 characters
Language: Shell Filename: Check What Changed
$ git status -s
 M main.js     # Modified, not staged
?? README.md   # Untracked
Language: Shell Filename: Stage Changes
$ git add main.js   # One file
$ git add .         # Everything below here
$ git status -s
A  README.md        # Staged new file
M  main.js          # Staged modification
Language: Shell Filename: Save a Snapshot
$ git commit -m "Add README"
$ git commit -am "Fix typo"  # Tracked only
$ git commit  # Opens editor for long message

Ignore Files

  • Never commit secrets, node_modules/, or build output
  • Commit .gitignore itself so the whole team shares it
  • Ignoring doesn’t untrack: use git rm --cached first
Language: Shell Filename: A Node .gitignore
$ cat .gitignore
node_modules/
dist/
.env
*.log
Language: Shell Filename: Debug and Untrack
$ git check-ignore -v .env
.gitignore:3:.env   .env
$ git rm --cached secrets.txt

Inspect Changes and History

  • git diff compares against the stage; new files don’t show
  • Press q to leave the pager
  • HEAD is your current commit; HEAD~1 is its parent
Language: Shell Filename: Review a Diff
$ git diff           # Unstaged changes
$ git diff --staged  # What you'll commit
@@ -1,4 +1,4 @@
-console.log(greet("World"));
+console.log(greet("Git learner"));
Language: Shell Filename: Browse the Log
$ git log --oneline
1c692b1 Update greeting and add README
dd5a68a Add .gitignore
$ git log --oneline --graph --all
$ git log -p main.js    # One file's history
$ git show HEAD         # Last commit + diff
$ git blame main.js     # Who wrote each line

Branches and Merging

  • A branch is a movable pointer to a commit; they’re cheap
  • Commit or stash before you switch branches
  • Merge into the branch you’re on
Language: Shell Filename: Create and Switch
$ git switch -c add-farewell  # New branch
$ git branch
* add-farewell
  main
$ git switch main
$ git switch -    # Back to previous branch
Language: Shell Filename: Merge and Clean Up
$ git merge add-farewell
Fast-forward
$ git branch -d add-farewell
Language: Shell Filename: Resolve a Conflict
$ git merge shout
CONFLICT (content): Merge conflict in main.js
<<<<<<< HEAD
  return `Hi, ${name}!`;
=======
  return `HELLO, ${name}!`;
>>>>>>> shout
$ git add main.js && git commit  # Resolved
$ git merge --abort              # Or give up

Remote Repositories

  • origin is the conventional name for your main remote
  • fetch downloads; pull = fetch + merge
  • Pull before you push to avoid rejected pushes
Language: Shell Filename: Connect and Publish
$ git remote add origin <url>
$ git remote -v
$ git push -u origin main  # First push
$ git push                 # After that
Language: Shell Filename: Stay in Sync
$ git fetch         # Download, don't merge
$ git pull          # Download and merge
$ git pull --rebase # Replay yours on top

Stash Work in Progress

  • Stash parks uncommitted changes so you can switch tasks
  • pop applies and deletes; apply keeps the stash
Language: Shell Filename: Park and Restore
$ git stash push -m "wip"
$ git stash list
stash@{0}: On main: wip
$ git stash pop

Undo Mistakes

  • restore on a file discards edits for good
  • Shared commits: revert, never reset or --amend
  • reset --hard also wipes uncommitted work
Situation Command
Unstage a file git restore --staged f
Discard file edits git restore f
Fix last message git commit --amend
Undo commit, keep work git reset --soft HEAD~1
Undo a pushed commit git revert <hash>
Rename a tracked file git mv old new
Language: Shell Filename: Recover a Lost Commit
$ git reset --hard HEAD~1  # Oops!
$ git reflog
5349a96 HEAD@{0}: reset: moving to HEAD~1
5708734 HEAD@{1}: commit: Say hi instead
$ git reset --hard 5708734  # Back again
Language: Shell Filename: Delete Untracked Files
$ git clean -n  # Dry run first
Would remove tmp.log
$ git clean -f

You can download this information as a printable cheat sheet:

Free Bonus: Git Cheat Sheet

Get the Git Cheat Sheet (PDF), a printable reference to setup, staging and commits, .gitignore, branches and merging, remotes, stash, and undoing mistakes:

Git Cheat Sheet