it-tips#git#version-control#cheat-sheet
Git Cheat Sheet: Every Command You'll Actually Use
From `git init` to `git rebase -i` — the practical Git workflow you'll use daily, with what each command really does.
Series
Linux & Git Command Reference
Git — the whole daily workflow
Mental model
+---------------+ add +---------+ commit +----------+ push +---------+
| working dir | ------> | staging | ------> | local | ----> | remote |
| (your edits) | | area | | repo | | (GitHub)|
+---------------+ +---------+ +----------+ +---------+
^ |
|------------------------ pull / clone ----------------------------|
Set up
git config --global user.name "Priya Sharma"
git config --global user.email "priya@example.com"
git config --global init.defaultBranch main
Start a repo
git init # new local repo
git clone https://github.com/x/y.git # clone existing
Everyday flow
git status # what changed?
git add file.py # stage one file
git add . # stage everything
git commit -m "Add login form" # commit staged
git log --oneline --graph --decorate # nice history
git push # send to remote
git pull # bring remote changes
Branching
git branch # list local branches
git branch feature-login # create branch
git checkout feature-login # switch to it
git checkout -b feature-login # create + switch (shortcut)
# In newer git:
git switch feature-login
git switch -c feature-login
Merge vs Rebase
Merge Rebase
A---B---C main A---B---C---F'---G' main
\ \
D---E---F feature (D, E, F are re-applied on top of C)
\
G merge commit
git merge feature-login— keeps history as it happened.git rebase main(fromfeature-login) — replays your commits on top ofmain, cleaner history.
Rule: never rebase branches other people are also using.
Undoing
git restore file.py # discard local edit (unstaged)
git restore --staged file.py # unstage (keep edits)
git commit --amend # edit the last commit
git reset --soft HEAD~1 # undo commit, keep staged
git reset --mixed HEAD~1 # undo commit + unstage (default)
git reset --hard HEAD~1 # undo commit + throw away edits (danger!)
git revert <commit> # safe undo — makes a new commit
Stash — hide changes temporarily
git stash # tuck away current edits
git stash list # see stashes
git stash apply # bring them back (keep in stash)
git stash pop # bring back + remove from stash
Cherry-pick — grab one commit
git cherry-pick <sha>
Remote & push
git remote -v # show remotes
git remote add origin URL # add remote called origin
git push -u origin main # first push (sets upstream)
.gitignore in one minute
# comments start with #
node_modules/
*.log
.env
build/
Real-world flow (feature branch)
git checkout main && git pull
git checkout -b feature-search
# ... edit code ...
git add . && git commit -m "Add fuzzy search on notes"
git push -u origin feature-search
# open Pull Request on GitHub → review → merge
git checkout main && git pull
git branch -d feature-search
Beginner tips
- Small commits > huge commits. Reviewers thank you.
- Never commit secrets (
.envfiles). Add to.gitignorebefore your first commit. - Use
git log --onelineto make history readable. - Learn
git bisect— it finds which commit introduced a bug inlog(N)steps.
Keep reading
You may also like
it-tips
Linux Cheat Sheet Part 2: Text, Networking, Archives, Scripting
grep / sed / awk, ssh / curl / netstat, tar / zip, and just enough Bash to write your own scripts.
Read
it-tips
10 Terminal Aliases That Save Hours a Week
Small `.zshrc` tweaks with an outsized impact on daily workflow.
Read
it-tips
10 Linux Commands Every Fresher Should Know
The commands you'll type a hundred times a week. Master these ten and you'll look like a five-year veteran.
Read
Discussion (0)
No comments yet. Be the first to weigh in.