DEV Community

Nico Reyes
Nico Reyes

Posted on

I lost 4 hours of work to a Git rebase. Here's what I should've done

I lost 4 hours of work to a Git rebase. Here's what I should've done

Last week I tried to clean up my commit history before pushing. Ended up nuking half my changes and panicking for way too long.

I had like 15 commits on a feature branch. Messy stuff ("fix", "actually fix", "why doesnt this work"). Wanted to squash them into something clean before the PR.

Ran git rebase -i HEAD~15 and started combining commits. Looked good in the editor. Hit save.

Git threw merge conflicts. Started resolving them. More conflicts. Somehow lost track of which version was correct. Tried to abort but already marked conflicts as resolved. Branch was in a weird half rebased state.

Spent 2 hours trying to fix it. Gave up and force reset to origin. Lost 4 hours of uncommitted debugging work that wasnt staged.

The backup branch thing

Before any rebase just do this:

# Create backup branch
git branch backup-feature-branch

# Now rebase safely
git rebase -i HEAD~15
Enter fullscreen mode Exit fullscreen mode

If it explodes:

git rebase --abort
git reset --hard backup-feature-branch
Enter fullscreen mode Exit fullscreen mode

Takes 5 seconds. Wouldve saved me everything.

The staging mistake

I had changes I was testing that werent committed. Rebase doesnt touch unstaged files but conflicts can mess with them.

Should've stashed first:

git stash
git rebase -i HEAD~15
git stash pop
Enter fullscreen mode Exit fullscreen mode

Or just commit the WIP:

git add .
git commit -m "WIP debugging"
# rebase
# then squash the WIP commit too
Enter fullscreen mode Exit fullscreen mode

But I didnt. And Git decided to make my life harder.

When it gets messy

Dont panic and start editing random files like I did. Check what actually conflicts:

# See conflicted files
git status

# See what changed
git diff

# If too messy, just abort
git rebase --abort
Enter fullscreen mode Exit fullscreen mode

I kept trying to fix conflicts without understanding what I was even conflicting with. Shouldve aborted way earlier instead of digging deeper into the mess.

What I do now

Honestly I just avoid interactive rebase for big history changes. If I need to clean commits:

# Soft reset to X commits back
git reset --soft HEAD~15

# Everything now staged as one change
git commit -m "Clean commit message"
Enter fullscreen mode Exit fullscreen mode

Way simpler. Same result but no conflict resolution maze.

For small stuff (last couple commits) interactive rebase is fine. For 10+ commits I just soft reset.

VSCode Git Graph extension helps visualize what youre about to do too. Better than guessing in the terminal.

Still annoyed about those 4 hours honestly.

Top comments (0)