The Git Story
It was late on a Thursday night, and I had just written the best code of my life. Two days of work. Clean functions. Green tests. I typed git push like a man signing his own trophy.
Git said no.
“Updates were rejected because the remote contains work that you do not have locally.”
I had forgotten to pull. For two whole days, I had been coding in 1955 while the rest of my team had moved on to 1985.
Great Scott, I Forgot to Pull
If you’ve seen Back to the Future, you know the plot. Marty McFly travels back in time, changes a few small things, and comes home to a world that no longer matches the one he left.
That was me. My local branch was a time capsule. Meanwhile, main had lived a full life without me: new features, renamed files, a teammate who had “just cleaned up” the exact class I’d been rewriting.
So I did what every panicked developer does. I ran git pull.
The terminal turned into a crime scene. Conflict after conflict. Files I had never touched were somehow my problem now. I stared at this for a long time:
<<<<<<< HEAD
my beautiful code
=======
their beautiful code
>>>>>>> origin/main
Two versions of reality, stacked on top of each other, and Git politely asking me to choose which universe survives.
The Flux Capacitor: What git pull Actually Does
Here’s what nobody told me in college: git pull isn’t one command. It’s two, wearing a trench coat.
git fetch # download what changed on the remote (safe, changes nothing locally)
git merge # glue those changes into your branch (this is where the drama starts)
git fetch is just looking at the future through a window. You can always run it. It never touches your work. Want to see what’s coming before you jump?
git fetch
git log --oneline HEAD..origin/main # commits on the remote that you don't have yet
The lesson I learned that night is the first rule of time travel: check what year it is before you start changing things. Pull before you write a single line. Every morning. Every time you come back from lunch. It takes five seconds and saves you an evening.
Doc’s Chalkboard: Branches Are Timelines
In Back to the Future Part II, Doc Brown grabs a chalkboard and draws a line: that’s the timeline. Then he draws a second line splitting off from it: that’s the alternate timeline, the one where everything went wrong.
That chalkboard is the best Git tutorial ever made.
main is the real timeline. A branch is you saying, “Let me go try something in a parallel universe, where I can’t ruin anything for anyone.”
git switch main
git pull # always start from the latest timeline
git switch -c feature/login-fix # split off into your own universe
Want to see your own chalkboard? This one command draws it in the terminal:
git log --oneline --graph --all
The longer your branch lives on its own, the further its universe drifts from reality. A branch that lives for two hours comes home easily. A branch that lives for two weeks comes home to a town where nobody recognises it.
Merge or Rebase: Two Ways to Fix the Timeline
Back to my Thursday night. Once you’ve diverged from main, there are two ways to bring the timelines back together, and I’ve had bad nights with both.
Merge is the honest way. It takes both timelines and ties them together with a knot called a merge commit.
git fetch
git merge origin/main
Your history now says, truthfully: “These two universes existed, and on this day they met.” Nothing is rewritten. The downside? Do this often enough and your history looks like a plate of noodles.
Rebase is the time-traveller’s way. It picks up your commits, goes back, and replays them one by one on top of the latest main, as if you had started your work this morning with everything already up to date.
git fetch
git rebase origin/main
The history comes out as one clean, straight line. Beautiful. But here’s the catch nobody warns you about: rebase replays your commits one at a time. So if three of your commits touch the same messy file, you might resolve the same conflict three times. That’s not a bug. That’s time travel. Every jump has consequences.
When a conflict appears in the middle of either one, the process is the same:
git status # which files are fighting?
# open each file, pick the right code, delete the <<<<<<< ======= >>>>>>> markers
git add <file>
git rebase --continue # or: git commit, if you were merging
And the most underrated commands in all of Git, for when you’ve made things worse and want to go back to before the jump:
git rebase --abort
git merge --abort
These are the “take me home, Doc” buttons. No shame in pressing them.
So which one? My rule now: rebase your own branch onto main to keep it fresh, and merge when the branch goes back into main (usually through a pull request). And the golden rule, the one that is never, ever broken:
Never rebase a branch someone else is working on. Rebase rewrites history. If you rewrite history that a teammate has already pulled, you’ve just changed their past without telling them. That’s not a merge conflict anymore. That’s a paradox.
The Fading Photograph: When Your Commits Disappear
There’s a scene where Marty looks at a photo of himself and his siblings, and they slowly start fading away because the timeline is collapsing.
I had that exact feeling the first time I ran this:
git reset --hard HEAD~3
Three commits. Gone. I watched them fade.
Except they weren’t gone. Git has a secret diary called the reflog. It remembers everywhere your branch has been, even the places you tried to erase.
git reflog # find the commit from before you panicked
git reset --hard <that-commit> # bring the photograph back
If you remember one thing from this article, remember this: in Git, almost nothing is truly lost, as long as you committed it. Uncommitted work, though? That really does fade. Commit early, commit often.
And if you want to undo something that’s already been pushed and shared, don’t erase the past. Add a new commit that cancels it out:
git revert <commit>
reset changes history. revert adds to it. On shared branches, always add, never erase.
Don’t Run Into Your Past Self: Force Pushing
The fastest way to make enemies on a team is this:
git push --force
It tells the remote: “Whatever is up there, throw it away and take mine.” If a teammate pushed something in the last hour, it’s now erased from the timeline. They didn’t do anything wrong. They just existed in the wrong universe.
If you truly have to force push (say, after rebasing your own branch), use the safe version:
git push --force-with-lease
It checks first: “Has anyone else changed this since I last looked?” If yes, it refuses. It’s the difference between jumping through time blindfolded and checking that nobody’s standing where you’ll land.
Hide the Almanac: git stash
Sometimes you’re halfway through something, the code doesn’t even compile, and someone asks you to quickly fix a bug on another branch. You can’t commit half-broken work, and you can’t switch with it either.
So you hide it, like a certain sports almanac stuffed under a jacket:
git stash # tuck your unfinished work away
git switch hotfix # go save the day
git switch - # come back
git stash pop # take your work back out
Just don’t forget what you hid. git stash list tells you. I once found a stash that was four months old. I had no idea what past-me was trying to do.
Doc’s Rules for Time Travellers
Everything I learned the hard way, so you don’t have to:
- Pull before you start. Check what year it is before you change anything.
- One branch per task, and keep it short-lived. The longer you’re away, the stranger home looks.
- Commit small and often. Small commits mean small conflicts, and the reflog can only save what you committed.
- Write commit messages for future-you. “fix stuff” helps nobody in six months. “Fix null check in login when email is empty” does.
- Rebase your own branch, merge shared ones. Never rebase what someone else has pulled.
--force-with-lease, never plain--force.git statusis free. Run it constantly. It’s your dashboard clock.- When in doubt,
--abort. You can always go back and try again.
The Commands I Actually Use Every Day
| What I want | Command |
|---|---|
| See what’s going on | git status |
| Get the latest without touching my work | git fetch |
| Get the latest and update my branch | git pull --rebase |
| Start a new task | git switch -c feature/name |
| Save my work | git add -p then git commit -m "..." |
| Fix my last commit message | git commit --amend |
Catch up with main |
git rebase origin/main |
| Draw the timeline | git log --oneline --graph --all |
| Hide unfinished work | git stash / git stash pop |
| Undo a shared commit | git revert <commit> |
| Find “lost” commits | git reflog |
| Escape a bad rebase or merge | git rebase --abort / git merge --abort |
Bonus: if you want git pull to rebase by default instead of merging, run this once and forget about it:
git config --global pull.rebase true
Back to the Present
That Thursday night, I did eventually get my code in. It took hours, a lot of coffee, and at least one moment where I seriously considered deleting the whole folder and cloning again. (Everyone has done this. Don’t let anyone tell you otherwise.)
But I came out of it understanding something. Git isn’t just a tool for saving code. It’s a machine for managing timelines: yours, your teammates’, and the one everyone shares. Most of the pain doesn’t come from Git being hard. It comes from forgetting that other people are living in the same timeline as you.
So pull before you start. Keep your branches short. And if you ever see your commits fading like that photograph, take a breath and type git reflog.
Your history isn’t written in stone. That’s the whole point.