在 git 中更改分支的根

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5712133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:21:52  来源:igfitidea点击:

Change root of a branch in git

gittagsbranchresetrebase

提问by micha149

I'm using git and want to change the base of an exiting branch. This is caused by a deployment system, which pulls this explicit branch into my production environment. When planning my releases, I create a tag every time I want to go live. But my branch has special changes too, so git reset --hard v1.0won't work.

我正在使用 git 并想更改现有分支的基础。这是由部署系统引起的,它将这个显式分支拉入我的生产环境。在计划发布时,我每次想要上线时都会创建一个标签。但是我的分支也有特殊的变化,所以git reset --hard v1.0不起作用。

Here a small example. I want this

这里有一个小例子。我要这个

      C---D---E deploy
     /
A---B---F---G master
     \
      v1.0

to become this

成为这个

                          C---D---E deploy
                         /
A---B---F---G---H---I---J---K master
     \                   \
      v1.0                v1.1

Maybe git rebaseis what I am looking for, but the man pages don't help me. Thanks for your replies!

也许git rebase是我正在寻找的,但手册页对我没有帮助。感谢您的回复!

采纳答案by VonC

git rebaseshould, like you say, allow you to change the base of deploy:

git rebase应该像您说的那样,允许您更改部署的基础:

git checkout deploy
git rebase v1.1 # using the tag
(or:
 git rebase J # SHA1 of J
 or
 git rebase master~1
)

But you will end up with

但你最终会得到

C'---D'---E' deploy

That is, the SHA1 of the commits part of deploybranch are rewritten, which isn't too bad if nobody cloned said deploybranch and was working on it.
Since it is a branch for deployment, that is most likely the case (i.e. nobody was working on a clone of said branch).

也就是说,deploy分支的提交部分的 SHA1被重写,如果没有人克隆所述deploy分支并正在处理它,这还不算太糟糕。
因为它是一个用于部署的分支,所以很可能是这种情况(即没有人在处理所述分支的克隆)。

回答by Bruce

I don't understand why you'd want to lose your original branch. What I would do in such a case:

我不明白你为什么想要失去原来的分支。在这种情况下我会怎么做:

 # create a new branch from your 1.1 tag
 git checkout -b deploy1.1 v1.1 
 # merge your existing branch into this one
 git merge deploy

EDIT: added schema

编辑:添加架构

You'll end up with something like that

你会得到这样的结果

       C---D---E deploy
       /        \_______ 
      /                  F deploy1.1
     /                  /
A---B---F---G--H--I--J--K--L
     \                   \
    v1.0                 V1.1

回答by knittl

yes, you can use rebase to achieve the desired effect. the following command will checkout the deploybranch and replay all its commits, which are not reachable through v1.1, on top of v1.1:

是的,你可以使用 rebase 来达到预期的效果。以下命令将检出deploy分支并重播其所有提交,这些提交无法通过v1.1, 在 , 之上v1.1

git rebase v1.1 deploy

(the verbose way would be: git rebase --onto v1.1 v1.0 deploy)

(冗长的方法是:git rebase --onto v1.1 v1.0 deploy

but why rebasing and altering history? you can simply change the mainline of development into your deployment-branch:

但是为什么要重新定位和改变历史呢?您可以简单地将开发主线更改为您的部署分支:

git checkout deploy
git merge v1.1

this will leave all your commit hashes intact, your history will then look like this (Mbeing the merge commit):

这将使您的所有提交哈希保持不变,您的历史记录将如下所示(M即合并提交):

      C---D---E-----------M deploy
     /                   /
A---B---F---G---H---I---J---K master
     \                   \
      v1.0                v1.1

since conflicts might arise during rebase as well as during merge, you will have a history of merge conflicts when using the merge based approach. with rebase you don't have a history of conflicts which happened during rebase operation. using a merge based workflow, you can later see your conflicts in the (combined) diff of the merge commits.

由于在 rebase 和合并期间可能会出现冲突,因此在使用基于合并的方法时,您将有合并冲突的历史。使用变基,您没有变基操作期间发生的冲突历史。使用基于合并的工作流,您稍后可以在合并提交的(组合)差异中看到您的冲突。

回答by manojlds

git rebase should work for you:

git rebase 应该适合你:

git checkout deploy
git rebase master~1

or

或者

git rebase v1.1

Have a look at http://progit.org/book/ch3-6.html- should help you understand rebase better I think

看看http://progit.org/book/ch3-6.html- 我认为应该可以帮助你更好地理解 rebase