2

I am learning git.

In this book I read that rebase is not safe when I pushed commits into public repository. It is clear. But what if I didn't pushed commit-X, but created local branches After commit-X are made and want to change this commit?

I tried to change name of the old commit in master branch using git rebase -i HEAD~3 and see that hash has changed. Meanwhile in my work branch same commit didn't changе and have different hash for this commit in master and work branch now.

The created issue was fixed by simple git rebase master. But I wonder whether I was just lucky and can't I have a problem in such cases?

What is the correct way to rewrite commit history if I have several branches, which depend on commits being rewritten? Is rebasing always safe in such a cases? If not, then when exactly it is not safe? What kind of changes I must avoid here to be safe?

klm123
  • 12,105
  • 14
  • 57
  • 95

1 Answers1

1

If you rewrite commitX, you go from:

              b--b (B1)
             /
x--x--X--y--z  (master)
          \
           b--b--b (B2)

To:

              b--b  (B1)
             /
x--x--X--y--z       
    \     \
     \     b--b--b  (B2)
      \
       x'--y'--z' (master)

Meaning your old branch is still there.

You can then rebase --onto:

git checkout master
git rebase --onto z' z B1
git rebase --onto y' z B2

It is "safe", but can be a lot of work if you have many branches to rebase. In that last case, you can try automating those rebase, but it isn't always trivial: see for instance "Git: How to rebase many branches (with the same base commit) at once?".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @klm123 "disappear", or rather be referenced only by your [local **reflog**](http://stackoverflow.com/a/17859891/6309). – VonC Nov 18 '13 at 07:39
  • I mean physically, will git keep content of this oldMaster branch, or will it calculate it in case reflog is used? – klm123 Nov 18 '13 at 07:48
  • 1
    @klm123 physically, the content is kept, in the reflog, for (by default) 90 days. It isn't calculated on demand. It is stored. – VonC Nov 18 '13 at 07:50
  • Thank you! Now it is much clear about safety during history rewriting. – klm123 Nov 18 '13 at 07:58