Pergunta

Consider this line of commits.

foo: a => b => c => d
                      \bar: => e => f => g

I start out with branch foo and occasionally create the new branch bar from foo. Now, as I am working on commit h in branch bar, I realize that I made a mistake in commit c. So, I run git rebase and open up commit c for editing. I make my changes, stage them and run git rebase --continue.

Now, my bar branch is updated. However, the foo branch still points to the original flawed version of commit c.

Is there a way of running a repo-wide rebase in which all branches that contains pointers to a particular commit are identified and rebased, one at a time?

I know the manual way - rebase foo and insert/cherry-pick the new SHA, but it requires me to know what branches reference the commit. And it requires manual labor.

Foi útil?

Solução

I think you're misunderstanding how branches work in git. They're not whole histories, just pointers to a single commit. That is, foo is really just a synonym for d, and bar is a synonym for g. Rebasing bar doesn't affect foo in any way.

If you want to set foo to your new d (let's call it d'), just do:

git checkout foo
git reset --hard d'

Here's an example based on yours:

a <- b <- c <- d <- e <- f <- g
               ^              ^
               |              |
              foo            bar

Now, after you do the rebase on bar, you have divergent history:

          /- c <- d   
         /        ^
a <- b <-         |
         \       foo
          \
           \- c' <- d' <- e' <- f' <- g'
                                      ^
                                      |
                                     bar

All you want to do is point foo at d', using the reset command described above:

  a <- b <- c' <- d' <- e' <- f' <- g'
                  ^                 ^
                  |                 |
                 foo               bar

This operation orphans c and d - they'll get garbage collected eventually.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top