Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to prevent generating a new commit sandwiched in the middle during a git rebase?

I have a branch like this A->B

I mistakenly added an extra string to the original string Foo and it became Fooo in commit A.

And I remove the o in commit B. (i.e. Fooo -> Foo)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The purpose of both A & B are just adding new files so I want to eliminate all the confusing changes on Foo

After removing extra o at commit A, I git commit –amend and get a new commit A'

Now I want to make the branch like this: A'->B'

Here is the step:

git checkout B

git rebase -i A'

A file shows and lets me to edit, such as something like : pick:B, I don’t change anything.

I expect that I could edit the conflict, but after git rebase --continue the rebase finished immediately.

Finally I get a stupid result A'->A''->B'.

The commit A'' add the extra o I removed at A', then the B' removed it again.

How can I do to eliminate all the confusing changes on Foo?


I figure out how to do it simply.

The content of file shows after rebase is:
pick:A
pick:B

After removing the line pick:A, than I rebase it smoothly.

I think the solution for squash A” & B’ also works.

Thanks for helping.

>Solution :

Assuming that you still have:
A' -> A'' -> B'
what we need to do is combine A'' and B' together. Once you do this, both the addition and removal of 'o' will be in a single commit (say B'') and they will cancel each other out, i.e., they won’t show up in a commit patch.

To accomplish this, you can do the following:

Execute:

git rebase -i A'

Now, you will get an editor with commits A'' and B' listed. Change the pick beside B' to squash. Save and close the editor.

Next, git will open an editor where you can provide the commit message you want commit B'' to have. Git will have the editor pre-filled with the messages of A'' and B' in it, so you just have to remove the message of A'' from it, save and exit the editor.

This should get you, A'' -> B'' as you require.


I’m not sure exactly what went wrong in what you originally did, but you may have used checkout while you were in the middle of a rebase.
What I would do in your original situation would be:

  1. Remove the removal of 'o' from B first using a normal git commit --amend.
  2. Use interactive rebase (git rebase -i <parent of A'>)
  3. Change pick beside A to edit. Save and exit.
  4. Now you will be at commit A. Remove the 'o' from A using git commit --amend.
  5. git rebase --continue to finish the rebase.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading