DEV Community

Nico Reyes
Nico Reyes

Posted on

Git said everything merged fine. My code was gone.

Merged a feature branch yesterday. Git said zero conflicts. Pushed to main. My entire authentication module disappeared. Three hours of code just vanished.

Spent an hour thinking Git was broken. It wasn't. I was using merge wrong.

What happened

Working on two branches:

  • feature/auth adding OAuth login
  • feature/ui updating frontend

Both touched app.py but different sections. Merged feature/auth first, worked fine. Then merged feature/ui.

git checkout main
git merge feature/ui
# "Auto-merging app.py"
# "Merge made by the 'recursive' strategy"
Enter fullscreen mode Exit fullscreen mode

Zero conflicts. Pushed it. Authentication broke immediately in production.

Pulled the code. My OAuth functions were completely missing. Not commented out, not broken, just gone. The file looked like I never wrote them.

Why it happened

Git merged the file correctly based on the branches. Problem was feature/ui was created BEFORE I merged feature/auth. So from Git's perspective:

  • main at time T: has basic auth
  • feature/ui branched from T: has basic auth
  • feature/auth merged at T+1: adds OAuth to main
  • feature/ui merged at T+2: Git sees "ui branch doesn't have OAuth" and assumes that's intentional

Git thought I deleted the OAuth code on purpose. Technically correct merge behavior, but my code was still gone.

What I should have done

Rebase the feature branch BEFORE merging:

# Update feature/ui with latest main changes
git checkout feature/ui
git rebase main

# This replays your ui commits on top of the new main
# Now feature/ui includes the OAuth code

git checkout main  
git merge feature/ui
# Now the merge keeps everything
Enter fullscreen mode Exit fullscreen mode

This makes Git replay your feature branch changes on top of the current main (which includes the OAuth code). So the merge knows about both sets of changes.

Other option that works

Merge main INTO your feature branch first:

git checkout feature/ui
git merge main

# Resolve any conflicts here (in the feature branch)
# Test that everything works

git checkout main
git merge feature/ui  
# Clean merge, keeps everything
Enter fullscreen mode Exit fullscreen mode

Less clean history than rebase but safer if you're not comfortable with rebasing.

What actually saved me

git reflog showed every commit including the "deleted" OAuth code:

git reflog
# Found: abc1234 HEAD@{3}: commit: Add OAuth functions

git checkout abc1234 -- app/auth.py
# Restored the file from that commit
Enter fullscreen mode Exit fullscreen mode

Recovered the code in 2 minutes once I found reflog. Should've known about this years ago honestly.

When this bites you

You'll hit this if:

  • Multiple feature branches touch the same file
  • You merge them without updating each branch first
  • No obvious conflicts appear (so Git doesn't warn you)
  • Code just silently disappears

Happens more with old feature branches. If your branch is 3 days old and main moved forward, rebase before merging.


Not sure if everyone does rebase before merge or if this is just me being sloppy. Works now tho.

Top comments (0)