git 更改分支基数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10853935/
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
Change branch base
提问by Ivan
I've a tree like this:
我有一棵这样的树:
(commit 1) - master
\-- (commit 2) - (commit 3) - demo
\-- (commit 4) - (commit 5) - PRO
and I have to move the PRO branch to master
我必须将 PRO 分支移动到 master
(commit 1) - master
|-- (commit 2) - (commit 3) - demo
\-- (commit 4) - (commit 5) - PRO
I've tried a git rebase master
from PRO branch, but nothing happens.
我试过git rebase master
来自 PRO 分支,但没有任何反应。
To clarify: I was working in master and then I had to make a product demo (git checkout -b demo
and some commits). Then, by mistake, I create another branch from demo (git checkout -b PRO
and some commits) and now I need to move PRO branch to master and leave demo intact. At the end, both demo and PRO will hang from master.
澄清一下:我在 master 工作,然后我不得不做一个产品演示(git checkout -b demo
和一些提交)。然后,我错误地从演示(git checkout -b PRO
和一些提交)创建了另一个分支,现在我需要将 PRO 分支移动到 master 并保持演示完好无损。最后,demo 和 PRO 都将挂在 master 上。
回答by loganfsmyth
Use --onto
for that:
用途--onto
为:
git rebase --onto newBase oldBase feature/branch
Given your case:
鉴于您的情况:
git checkout PRO # Just to be clear which branch to be on.
git rebase --onto master demo PRO
Basically, you take all the commits from after demo
up to PRO
, and rebase them onto the master
commit.
基本上,您获取从 afterdemo
到 的所有提交PRO
,并将它们重新绑定到master
提交上。
回答by ARK
I will try to be as generic as I can be. First, be sure that you are on the desired branch:
我会尽量做到通用。首先,确保您在所需的分支上:
git checkout current-branch
Then use the following command (where new-base-branch
is the branch which you want to be your new base, and current-base-branch
is the branch which is your current base.)
然后使用以下命令(new-base-branch
你想成为新基地的分支在哪里current-base-branch
,是你当前基地的分支。)
git rebase --onto new-base-branch current-base-branch
If you do not have conflicts, then great - you are done. If you do (in most cases), then please read on.
如果您没有冲突,那就太好了 - 您已经完成了。如果您这样做(在大多数情况下),请继续阅读。
Conflicts might arise, and you will have to resolve them manually. Git now tries to do a "3-way merge" between your current-branch
, current-base-branch
and new-base-branch
. Roughly this is how git will work internally:
可能会出现冲突,您必须手动解决它们。Git 现在尝试在您的current-branch
,current-base-branch
和new-base-branch
. git 在内部的工作方式大致如下:
Git will first rebase the
current-base-branch
on top of thenew-base-branch
. There might be conflicts; which you will have to resolve manually. After that is done, you usually dogit add .
andgit rebase --continue
. It will create a new temporary committemp-commit-hash
for this.After this, Git will now rebase your
current-branch
on top oftemp-commit-hash
. There might be further conflicts and again you will have to resolve them manually. Once done, you continue again withgit add .
andgit rebase --continue
, after which you have successfully rebased yourcurrent-branch
on top thenew-base-branch
.
Git 将首先
current-base-branch
在new-base-branch
. 可能会有冲突;您必须手动解决。完成后,您通常会执行git add .
和git rebase --continue
。它将temp-commit-hash
为此创建一个新的临时提交。在此之后,Git 现在将
current-branch
在temp-commit-hash
. 可能会有进一步的冲突,您将不得不再次手动解决它们。完成后,您再次使用git add .
和继续git rebase --continue
,之后您已成功地将您current-branch
的new-base-branch
.
Note:If you start to mess up, then you can do git rebase --abort
anytime during the rebase process and get back to the starting point.
注意:如果你开始搞砸了,那么你可以git rebase --abort
在 rebase 过程中随时做,然后回到起点。
回答by Sajib Khan
Checkout to PRO
branch, Copy the oldest (commit4) and latest (commit5) commit hashes of this branch and paste somewhere else:
结帐到PRO
分支,复制该分支的最旧(commit4)和最新(commit5)提交哈希并粘贴到其他位置:
$ git checkout PRO
$ git log # see the commit history
# copy the oldest & latest commit-hash
Delete the PRO
branch (keep a backup just for safety). Create and checkout to a new PRO
branch from master
:
删除PRO
分支(保留备份只是为了安全)。从以下位置创建并结帐到新PRO
分支master
:
$ git branch PRO.bac # create a new branch PRO.bac = PRO as backup
$ git checkout master
$ git branch -D PRO # delete the local PRO branch
$ git checkout -b PRO # create and checkout to a new 'PRO' branch from 'master'
Take (cherry-pick) the range of commits of Previous PRO
branch into the new PRO
branch:
将 ( cherry-pick) 上一个PRO
分支的提交范围带入新PRO
分支:
$ git cherry-pick commit4^..commit5 # cherry-pick range of commits
# note the '^' after commit4
Now, if all is ok, then do force (-f) push to remote PRO
branch and delete local PRO.bac
branch:
现在,如果一切正常,那么强制 (-f) 推送到remote PRO
分支并删除本地PRO.bac
分支:
$ git log # check the commit history
$ git push -f origin HEAD # replace the remote PRO by local PRO branch history
# git branch -D PRO.bac # delete local PRO.bac branch
回答by madjase
I had a slightly different approach using reset and stashes that avoids deleting and re-creating branches as well as eliminating the need to switch branches:
我使用 reset 和 stashes 的方法略有不同,可以避免删除和重新创建分支以及消除切换分支的需要:
$ git checkout PRO
$ git reset commit4 # This will set PROs HEAD to be at commit 4, and leave the modified commit 5 files in ur working index
$ git stash save -m "Commit message"
$ git reset commit 3
$ git stash save -m "Commit message"
$ git reset master --hard
$ git stash pop
$ git stash pop
$ git push --force # force if its already been push remotely
By reseting the branch on a commit by commit basis your basically just rewinding that branches history a commit at a time.
通过在一次提交的基础上重置分支,您基本上只是一次回滚该分支的历史提交。