Home > Software engineering >  Rewriting old commits in the master branch
Rewriting old commits in the master branch

Time:06-24

Say I have this commit history:

A - B - C (master)
         \
          D - E - F (Feature)

I'd like to amend commit B. So, I do:

  • git checkout master
  • git rebase -i <hash of A>

After the rebase is done, I see the change using git log. However when I switch to the Feature branch, commit B is still as it was before the rebase. In other words, when I'm in master branch, the commit history looks like this:

A - B' - C (master)

When I switch to Feature branch, the commit history looks like this:

A - B - C (master)
         \
          D - E - F (Feature)

I understand that rewriting the commit history is not wise, but I have to do this in my case.

How can I amend an old commit and have those changes be reflected on every branch? Am I doing this the wrong way?

The new commit history will be force pushed into Github and I don't want to mess things up.

Any help would be appreciated.

CodePudding user response:

After your rebase, what you actually have is

  B'- C' (master)
 /
A - B - C
         \
          D - E - F (Feature)

So now to complete the operation you'd have to rebase Feature on (the recently rewritten) master

$ git checkout Feature

$ git rebase master

          D'-E'-F' (Feature)
         /
A - B'- C' (master)
 \       
  B - C - D - E - F (soon to be garbage collected)

CodePudding user response:

and have those changes be reflected on every branch?

You need to manually rebase every branch. There is no way of changing the contents of one branch and have those changes be reflected on other branches, that is antithetical to how Git works.

  • Related