当git给两个分支分配一些共享历史记录时,是否有一种简单的方法让普通历史保持常见? [英] When git rebasing two branches with some shared history, is there an easy way to have the common history remain common?

查看:90
本文介绍了当git给两个分支分配一些共享历史记录时,是否有一种简单的方法让普通历史保持常见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下修订图:

  AXZ  -  B 
\
\\ \\ C

与A在B和C之前。进一步假设我从上游对A进行rebase,创建新提交A *,然后将B和C重新绑定到A *。生成的修订图如下:

  A * -X'-Z'-B 
\
\ -X-Z-C

请注意,共享历史记录不再共享。有没有一种简单的方法来解决这个问题,比如说,重新定义B,然后将C重新定义到Z'上。换句话说,是否有更好的方法同时自动重新绑定多个分支以保留共享历史记录?只是在分割点人为地放置一个标签,或者手动检查图形以找出提交的sha1以重新分配C以保持共享历史,这似乎有点尴尬,更不用说开放了可能性的错误,特别是因为我每次重新分配时都要这样做,直到我检查到上游分支的更改为止。

解决方案

  git rebase --committer-date-is-author-date --preserve-merges --onto A * AC 
git rebase --committer-date-is-author-date - -preserve-merges -onto A * AB

这应该保持共同的提交具有相同的sha1和任何合并保留。在这种情况下,并不需要保留合并,但会成为一个不太重要的历史问题。



为包含历史记录A的所有分支执行此操作:

  git branch --contains A | xargs -n 1 git rebase --committer-date-is-author-date --preserve-merges --onto A * A 

希望这会有所帮助。

更新:

/ p>

 用于$(git branch --contains A)中的分支。 git rebase --committer-date-is-author-date --preserve-merges --onto A * A $ branch;完成


Suppose we have the following revision graph:

A-X-Z--B
     \
      \-C

with A preceding both B and C. Further suppose I rebase A from upstream, creating a new commit A*, and then rebase both B and C onto A*. The resulting revision graph is the following:

A*-X'-Z'-B
 \
  \-X"-Z"-C

Note that the shared history is no longer shared. Is there a simple way to fix this, other than, say, rebasing B and then rebasing C onto Z' explicitly. In other words is there a better way to automatically rebase multiple branches at the same time in order to preserve shared history? It just seems a little bit awkward to have to either artificially place a tag at the split point, or manually inspect the graph to find out sha1 of the commit on which to rebase C to keep the shared history, not to mention opening up the possibility of mistakes, especially since I have to do this every time I rebase until I check the changes into the upstream branch.

解决方案

git rebase --committer-date-is-author-date --preserve-merges --onto A* A C
git rebase --committer-date-is-author-date --preserve-merges --onto A* A B

This should keep the common commits having the same sha1 and any merges preserved. Preserve merges is not required in this case, but will become an issue with a less trivial history.

To do this for all branches that contain A in their history do:

git branch --contains A | xargs -n 1 git rebase --committer-date-is-author-date --preserve-merges --onto A* A 

Hope this helps.

UPDATE:

This may be cleaner syntax:

for branch in $(git branch --contains A); do git rebase --committer-date-is-author-date --preserve-merges --onto A* A $branch; done

这篇关于当git给两个分支分配一些共享历史记录时,是否有一种简单的方法让普通历史保持常见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆