Lucy Bain
Lucy Bain

Reputation: 2616

How can I undo a sha change from git rebase interactive?

I did an interactive rebase with more commits than I needed to change. I was surprised that the shas of the unnessisary changes were updated. Unfortunately I only realised this happened after I force pushed my updates.

What I did

Starting point

X - Y \
A - B - C  *master (C is a merge commit)
         \
          - D - E *feature-branch

My expectation was that everything would look like this:

X - Y \
A - B - C  *master
         \
          - D1 *feature-branch

When I went to my PR, things actually looked like this:

X - Y \
A - B - C  *master
         \
          - A1 - B1 - C1 - D1 *feature-branch

Where A1, B1, and C1 had the same commit messages and changes as A, B, and C, but with different shas.

Questions

Upvotes: 1

Views: 386

Answers (2)

Borealid
Borealid

Reputation: 98509

The easiest way if you just want to delete A1/B1/C1 is to do a git rebase -i C. Mark A1 as 'reword', and B1 through D1 as 'fixup'. Edit the commit message for A1 to be what you want as the final message.

Now, as to what went wrong: C is a merge commit.

That means your history graph probably actually looked like this:

   A - B - C  *master
          / \
...-X-Y-Z    - D - E *feature-branch

When you said HEAD~5, that took the first parent of HEAD five times. Let's say the first parent of C is Z instead of B. That means you're rebasing onto commit X, and one of the commits you're planting there is A. The new A gets a new commit hash because its ancestry (and possibly its content) changed.

You might not have noticed Y and Z showing up in the rebase-interactive editor window and just left them as pick.

Basically, the rebase takes your nonlinear history and squishes it into being a line. I can't say I recommend using a rebase across a merge commit.

Upvotes: 4

Gaurav Tiwari
Gaurav Tiwari

Reputation: 352

If you run git revert for A1 - B1 - C1 , your new branch will effectively be the one that you desire. And this operation is safer

Upvotes: -1

Related Questions