Git/Cheat sheet

Last edited by dave on 01/12/2025, 21:45:34 UTC

Git / Cheat sheet

You were redirected here from Git cheat sheet.

Contents

Check out the other cheat sheets!

A concise, battle-tested guide to everything you actually use in Git — from setup to wizard-level commands.
(For when you forget whether it’s git pull --rebase or git rebase --pull... which, by the way, doesn’t exist.)


⚙️ Setup

Configuration (Linux etc.)

git config --global user.name "Your Name" git config --global user.email "you@example.com" git config --global core.editor "code --wait" # or vim, nano, etc. git config --global color.ui auto git config --list # verify configuration

Starting a Repository

git init # start a new repo git clone <url> # copy an existing repo

📦 Basic Workflow

git status # check what’s going on git add <file> # stage a specific file git add . # stage everything git commit -m "message" # commit staged changes git log # view commit history git diff # see unstaged changes git diff --staged # see staged changes

Undo oops moments:

git restore <file> # undo unstaged changes git restore --staged <file> # unstage a file git reset --hard HEAD # nuke all uncommitted changes

🌳 Branching & Merging

git branch # list branches git branch <name> # create branch git checkout <branch> # switch branch git switch <branch> # modern version of checkout git switch -c <branch> # create + switch git merge <branch> # merge into current branch git branch -d <branch> # delete branch

Rebase instead of merge (cleaner history):

git rebase main

Abort if things get weird:

git merge --abort git rebase --abort

🌐 Working with Remotes

git remote -v # list remotes git remote add origin <url> git fetch origin # get changes, don’t merge git pull # fetch + merge git pull --rebase # fetch + reapply local commits git push origin main # push commits git push -u origin main # push and set upstream

Rename or remove remotes:

git remote rename origin upstream git remote remove upstream

🧩 Staging & Commits

git add <file> # stage file git commit -m "message" # commit git commit --amend # edit last commit (use carefully)

Partial commits:

git add -p # add selected chunks interactively

🔍 Viewing History

git log # full commit log git log --oneline # compact view git log --graph --decorate --all git show <commit> # details of one commit

Compare commits or branches:

git diff <commit1> <commit2> git diff main feature-branch

🧰 Stash — Temporary Storage

git stash # save uncommitted work git stash list # list stashes git stash apply # reapply latest stash git stash pop # reapply + delete stash git stash drop # delete stash git stash clear # delete all

Name your stash:

git stash push -m "WIP: half-broken feature"

🪄 Reset, Revert & Undo

Soft, Mixed, Hard — choose your poison

git reset --soft HEAD~1 # undo commit, keep changes staged git reset --mixed HEAD~1 # undo commit, keep changes unstaged git reset --hard HEAD~1 # undo commit and all changes

Revert a commit (safe for shared branches):

git revert <commit-hash>

🔖 Tags

git tag # list tags git tag v1.0.0 # create tag git tag -a v1.0.0 -m "Release 1.0.0" # annotated tag git push origin v1.0.0 # push tag to remote git push origin --tags # push all tags

🧬 Cherry-Picking & Patching

Copy a commit to another branch:

git cherry-pick <commit-hash>

Export changes:

git diff > patch.diff git apply patch.diff

🚀 Collaboration Flow (Typical)

git checkout -b feature/awesome # ... work ... git add . git commit -m "Add awesome feature" git push -u origin feature/awesome # → open pull request

After merge:

git checkout main git pull git branch -d feature/awesome

⚖️ Clean Up & Maintenance

git fetch --prune # remove deleted remote branches git gc # clean up unnecessary files git reflog # show all actions (even deleted commits) git fsck # check repo health

Remove untracked files:

git clean -fd

🧠 Useful One-Liners

git shortlog -sn # list contributors git blame <file> # who wrote what (and when) git grep "keyword" # search codebase git log --since="2 weeks ago" --author="Alice" git diff --stat # summary of file changes

🔐 .gitignore Essentials

Typical .gitignore:

node_modules/
dist/
.env
*.log
.DS_Store
coverage/

🧑‍🏫 Pro Tips

  • Always pull --rebase before pushing (keeps history clean).
  • Avoid committing large binaries (use Git LFS if you must).
  • Commit early, commit often.
  • Write meaningful messages — future you will thank you.
  • Use git alias to save typing:
    git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit

🐧 Bonus: Visual Helpers

  • tig — terminal Git UI.
  • gitk — classic visual history browser.
  • VS Code Source Control — friendlier for beginners.
  • GitKraken / Fork / Tower — GUI clients for power users.

🧾 Git Reference Mental Map

AreaCommandPurpose
Creategit init, git cloneStart projects
Stagegit add, git resetPrepare commits
Commitgit commit, git amendSave work
Branchgit branch, git switch, git mergeOrganize changes
Syncgit pull, git push, git fetchCollaborate
Inspectgit log, git diff, git showReview history
Undogit revert, git reset, git restoreFix mistakes
Cleangit stash, git clean, git gcTidy up

“Git is like a time machine — powerful, but don’t push the red button unless you know what it does.”

Backlinks (3)
Categories (0)

    No categories assigned to this page.

Edit Level

> Signed In Users