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:
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
--globalsettings apply to every repo on your machine- Run
git statusearly and often git help <command>opens any command’s manual
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
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
Check What Changed
$ git status -s
M main.js # Modified, not staged
?? README.md # Untracked
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
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
.gitignoreitself so the whole team shares it - Ignoring doesn’t untrack: use
git rm --cachedfirst
A Node .gitignore
$ cat .gitignore
node_modules/
dist/
.env
*.log
Debug and Untrack
$ git check-ignore -v .env
.gitignore:3:.env .env
$ git rm --cached secrets.txt
Inspect Changes and History
git diffcompares against the stage; new files don’t show- Press
qto leave the pager HEADis your current commit;HEAD~1is its parent
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"));
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
Free Bonus: Download the Git Cheat Sheet PDF and keep the essentials at hand.
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
Create and Switch
$ git switch -c add-farewell # New branch
$ git branch
* add-farewell
main
$ git switch main
$ git switch - # Back to previous branch
Merge and Clean Up
$ git merge add-farewell
Fast-forward
$ git branch -d add-farewell
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
originis the conventional name for your main remotefetchdownloads;pull=fetch+merge- Pull before you push to avoid rejected pushes
Connect and Publish
$ git remote add origin <url>
$ git remote -v
$ git push -u origin main # First push
$ git push # After that
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
popapplies and deletes;applykeeps the stash
Park and Restore
$ git stash push -m "wip"
$ git stash list
stash@{0}: On main: wip
$ git stash pop
Undo Mistakes
restoreon a file discards edits for good- Shared commits:
revert, neverresetor--amend reset --hardalso 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 |
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
Delete Untracked Files
$ git clean -n # Dry run first
Would remove tmp.log
$ git clean -f
Want to go deeper on Git?
RealJS teaches the mental models that let you write, read, and review modern JavaScript with confidence, whether you or an AI wrote it.
Learn more at realjs.com 💡
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: