3

I checked Git: How to rebase to a specific commit? but I couldn't understand, my problem is not similar

  • I have a branch in which I have 2 commits x and y.

  • I add another commit z in that branch and want to squash it with x.

  • Every time I open a text editor to rebase, it shows commit y only and I am not able to rebase it with x.

Edit:

I tried using:

git rebase -i x
VIAGC
  • 639
  • 6
  • 14

1 Answers1

1

You can approach this one way by just making the z commit on top of the x and y commits:

# from your branch
git commit -m 'z commit'

Then, we can do an interactive rebase, reorder the commits, and squash. Type the following:

git rebase -i --root your_branch

This should bring up an editor window, showing your three commits from oldest to newest:

pick dj39fm2 message for commit x
pick m83mdk2 message for commit y
pick 3kme92w message for commit z

Now, reorder the commits such that z comes right after x, and then change the option for commit z to squash, to tell Git that you want to squash it down to the x commit:

pick dj39fm2 message for commit x
squash 3kme92w message for commit z
pick m83mdk2 message for commit y

Now save the file, exit the editor, and allow Git to start the rebase. Note that you might get some merge conflicts during the squash, or as the y commit reapplied to a new base.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360