Search code examples
gitversion-controlgit-rebase

Rewriting old commits in the master branch


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.


Solution

  • 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)