Click any command to expand the explanation and examples.
π¦ Setup & Config
git init basics
git init git init my-projectCreates a hidden
.git folder. Run this once per project.
git clone <url> basics
git clone https://github.com/user/repo.git git clone git@github.com:user/repo.git git clone https://github.com/user/repo.git my-folder
git config basics
git config --global user.name "Your Name" git config --global user.email "you@example.com"Check current config
git config βlist
Set for this repo only (no βglobal)
git config user.email βwork@company.comβ
πΈ Staging & Committing
git status daily
git status git status -s # short formatRun this constantly. It's your "where am I?" command.
git add daily
git add file.txt # stage one file git add src/ # stage a directory git add -A # stage everything (new, modified, deleted) git add -p # interactively stage parts of files
git add -p is underrated β it lets you commit only part of a file's changes.
git commit daily
git commit -m "Add login page" git commit -am "Fix typo" # add + commit tracked files git commit --amend # edit the last commit git commit --amend --no-edit # add staged files to last commit
--amend rewrites history β only use on unpushed commits.
git diff daily
git diff # unstaged changes git diff --staged # staged changes (about to commit) git diff main..feature # difference between branches git diff HEAD~3 # changes in last 3 commits
git stash useful
git stash # stash changes git stash pop # restore and remove from stash git stash list # see all stashes git stash apply # restore but keep in stash git stash drop # delete a stashUse when you need to switch branches but aren't ready to commit.
πΏ Branches
git branch daily
git branch # list local branches git branch -a # list all (including remote) git branch feature # create new branch git branch -d feature # delete (safe β won't delete unmerged) git branch -D feature # force delete git branch -m old new # rename a branch
git checkout / git switch daily
# Modern way (Git 2.23+) git switch main git switch -c new-feature # create and switchClassic way
git checkout main git checkout -b new-feature # create and switch
Restore a file
git checkout β file.txt # discard changes to file
git switch is the modern replacement for git checkout for branch switching.
git merge daily
git merge feature # merge feature into current branch git merge --no-ff feature # always create a merge commit git merge --abort # cancel a merge with conflicts
git rebase advanced
git rebase main # rebase current branch onto main git rebase -i HEAD~3 # interactive rebase last 3 commits git rebase --abort # cancel if something goes wrongβ οΈ Never rebase commits that have been pushed and shared with others.
π Remote (GitHub/GitLab)
git push daily
git push # push current branch git push origin main # push to specific remote/branch git push -u origin feature # push and set upstream (first time) git push --force-with-lease # safe force push (checks for others' changes)Never use
git push --force on shared branches. Use --force-with-lease instead.
git pull daily
git pull # fetch + merge git pull --rebase # fetch + rebase (cleaner history) git pull origin main # pull specific branch
git pull --rebase avoids unnecessary merge commits.
git fetch useful
git fetch # fetch all remotes git fetch origin # fetch specific remote git fetch --prune # remove deleted remote branchesUse
fetch when you want to see what changed before merging.
git remote setup
git remote -v # list remotes git remote add origin <url> # add a remote git remote set-url origin <new-url> # change remote URL git remote remove origin # remove a remote
π History & Inspection
git log daily
git log # full log git log --oneline # compact (one line per commit) git log --oneline --graph # visual branch graph git log --author="name" # filter by author git log -5 # last 5 commits git log -- file.txt # history of specific file
git show useful
git show abc1234 # show commit details + diff git show HEAD # show latest commit git show main:file.txt # show file from another branch
git blame useful
git blame file.txt git blame -L 10,20 file.txt # only lines 10-20
βͺ Undoing Things
git reset advanced
git reset HEAD file.txt # unstage a file git reset --soft HEAD~1 # undo last commit, keep changes staged git reset --mixed HEAD~1 # undo last commit, keep changes unstaged git reset --hard HEAD~1 # undo last commit, DELETE changesβ οΈ
--hard permanently deletes changes. Use with caution.
git revert safe
git revert abc1234 # revert a specific commit git revert HEAD # revert the last commitUnlike
reset, this doesn't rewrite history β safe to use on pushed commits.
git restore daily
git restore file.txt # discard unstaged changes git restore --staged file.txt # unstage a file git restore --source=HEAD~1 file.txt # restore from previous commit
π·οΈ Tags
git tag releases
git tag v1.0.0 # lightweight tag git tag -a v1.0.0 -m "Release" # annotated tag (recommended) git tag # list tags git push origin v1.0.0 # push a tag git push origin --tags # push all tags
Quick Reference Table
| What you want to do | Command |
|---|---|
| Start a new repo | git init |
| Download a repo | git clone <url> |
| See what changed | git status |
| Stage everything | git add -A |
| Commit | git commit -m "message" |
| Push to GitHub | git push |
| Pull latest changes | git pull |
| Create a branch | git switch -c name |
| Switch branches | git switch name |
| Merge a branch | git merge name |
| Undo last commit (keep changes) | git reset --soft HEAD~1 |
| Discard all local changes | git restore . |
| See commit history | git log --oneline |