One of those sessions that makes you genuinely more dangerous with Git. Not just knowing the commands but understanding why they work the way they do. This one's worth bookmarking.
Push and Pull Only Talk to Their Associated Branch
This is a common source of confusion early on. When you do git push or git pull on a branch, Git only communicates with that branch's associated remote — nothing else. git push newBranch goes to origin/newBranch and nowhere else. Same logic applies to pulling.
If you want to pull from a different branch than the one you're on:
git pull main or git pull origin main
And to push your local branch up to the remote:
git push origin
Simple rule — if you're not being specific, Git assumes you mean the current branch's remote counterpart.
git status and git log — Use Them Constantly
Seriously, before doing anything in Git — git status. Before merging, before pushing, before deleting a branch. It takes one second and it tells you exactly where you stand. git log gives you the commit history so you can see what's actually happened. These two commands alone prevent a huge number of mistakes.
The Right Way to Merge
Merging direction trips people up at first. The flow is: go to the branch that should receive the changes, then merge the other one in.
git checkout main
git merge feature-branch
You're standing in main saying "bring feature-branch's changes into me." Not the other way around.
Three Things to Keep in Mind When Working With Git
These three concepts explain almost every confusing Git behaviour:
HEAD — a pointer that tracks where you currently are in the commit history. It moves as you commit, checkout, or reset.
Local branch — lives on your machine. Only you can see it until you push.
Remote/origin branch — lives on the server (GitHub, GitLab etc.). This is what your team sees.
Deleting Branches — Local vs Remote, and -d vs -D
This is one area where getting it wrong can cause real headaches, so let's be precise:
Delete a local branch (safe — won't delete if unmerged):
git branch -d branchname
Delete a local branch (force — deletes regardless of merge status):
git branch -D branchname
Worth knowing: -d and --delete are exactly the same thing. -D and --force --delete are exactly the same thing. Just shorthand vs longhand.
Delete the actual remote branch on GitHub/GitLab:
git push origin --delete branchname
Note — no origin/ prefix in the branch name here. Just the plain branch name.
Delete only your local reference to a remote branch (not the actual remote):
git branch -D --remote origin/branchname
This one catches people off guard. It looks like you're deleting the remote branch but you're really just removing your local tracking reference to it.
Clean up stale local references to branches already deleted on remote:
git fetch --prune
Run this periodically to keep your local branch list tidy.
git diff — Staged, Unstaged, and Word-by-Word
Three variations worth knowing:
See changes in files you haven't staged yet:
git diff origin/main
See changes in files you have staged:
git diff --staged origin/main
See changes word by word instead of line by line (great for prose or small tweaks):
git diff origin/main --word-diff
git log --oneline — The Clean View
git log --oneline
Every commit on one line — hash and message. This is the one you'll actually use when you want a quick overview of history without scrolling through walls of text.
git reflog — Your Personal Git Black Box
git reflog is like a private diary of every move your HEAD pointer has made — checkouts, resets, merges, rebases, all of it. It doesn't show up to your teammates and it's not pushed to remote. But it's incredibly useful when you've accidentally reset something and need to get back to where you were. Think of it as an undo history for your entire Git session.
Top comments (0)