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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:50:29  来源:igfitidea点击:

Change branch base

git

提问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 masterfrom 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 demoand some commits). Then, by mistake, I create another branch from demo (git checkout -b PROand 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 --ontofor 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 demoup to PRO, and rebase them onto the mastercommit.

基本上,您获取从 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-branchis the branch which you want to be your new base, and current-base-branchis 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-branchand new-base-branch. Roughly this is how git will work internally:

可能会出现冲突,您必须手动解决它们。Git 现在尝试在您的current-branch,current-base-branchnew-base-branch. git 在内部的工作方式大致如下:

  1. Git will first rebase the current-base-branchon top of the new-base-branch. There might be conflicts; which you will have to resolve manually. After that is done, you usually do git add .and git rebase --continue. It will create a new temporary commit temp-commit-hashfor this.

  2. After this, Git will now rebase your current-branchon top of temp-commit-hash. There might be further conflicts and again you will have to resolve them manually. Once done, you continue again with git add .and git rebase --continue, after which you have successfully rebased your current-branchon top the new-base-branch.

  1. Git 将首先current-base-branchnew-base-branch. 可能会有冲突;您必须手动解决。完成后,您通常会执行git add .git rebase --continue。它将temp-commit-hash为此创建一个新的临时提交。

  2. 在此之后,Git 现在将current-branchtemp-commit-hash. 可能会有进一步的冲突,您将不得不再次手动解决它们。完成后,您再次使用git add .和继续git rebase --continue,之后您已成功地将您current-branchnew-base-branch.



Note:If you start to mess up, then you can do git rebase --abortanytime during the rebase process and get back to the starting point.

注意:如果你开始搞砸了,那么你可以git rebase --abort在 rebase 过程中随时做,然后回到起点。

回答by Sajib Khan

Checkout to PRObranch, 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 PRObranch (keep a backup just for safety). Create and checkout to a new PRObranch 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 PRObranch into the new PRObranch:

将 ( 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 PRObranch and delete local PRO.bacbranch:

现在,如果一切正常,那么强制 (-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.

通过在一次提交的基础上重置分支,您基本上只是一次回滚该分支的历史提交。