2024-07-29 06:00:00

改变分支基础

我有一棵这样的树:

(commit 1) - master
                \-- (commit 2) - (commit 3) - demo
                                                \-- (commit 4) - (commit 5) - PRO

我必须将PRO分支移动到master

(commit 1) - master
                |-- (commit 2) - (commit 3) - demo
                \-- (commit 4) - (commit 5) - PRO

我已经尝试了一个git rebase master从PRO分支,但什么都没有发生。

澄清一下:我在master中工作,然后我必须做一个产品演示(git checkout -b演示和一些提交)。然后,我错误地从演示创建了另一个分支(git checkout -b PRO和一些提交),现在我需要将PRO分支移动到master,并完好无损地离开演示。最后,demo和PRO都将挂起master。


当前回答

假设newBase是你想要移动你的提交到的分支,oldBase是你的分支的旧基础,你可以使用——onto:

git rebase --onto newBase oldBase feature/branch

鉴于你的情况:

git checkout PRO # Just to be clear which branch to be on.
git rebase --onto master demo PRO

基本上,从演示到PRO的所有提交,并将它们重新基于主提交。

其他回答

我会尽量讲得一般一些。首先,确保你在想要的分支上:

git checkout current-branch

然后使用以下命令(其中new-base-branch是希望作为新基础的分支,current-base-branch是作为当前基础的分支。)

git rebase --onto new-base-branch current-base-branch

如果你们之间没有冲突,那么很好——你完蛋了。如果你知道(在大多数情况下),那么请继续读下去。

可能会出现冲突,您必须手动解决这些冲突。Git现在尝试在你的current-branch、current-base-branch和new-base-branch之间进行“三路合并”。git内部是这样工作的:

Git will first rebase the current-base-branch on 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-hash for this. After this, Git will now rebase your current-branch on 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-branch on top the new-base-branch.


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

签出到PRO分支,复制该分支中最老的(commit4)和最新的(commit5)提交哈希值,并粘贴到其他地方:

$ git checkout PRO
$ git log            # see the commit history
# copy the oldest & latest commit-hash 

删除PRO分支(为安全起见保留备份)。从master创建并签出到一个新的PRO分支:

$ 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'

将前一个PRO分支的提交范围(选择)放入新的PRO分支:

$ git cherry-pick commit4^..commit5   # cherry-pick range of commits
# note the '^' after commit4

现在,如果一切正常,执行force (-f) push到远程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

假设newBase是你想要移动你的提交到的分支,oldBase是你的分支的旧基础,你可以使用——onto:

git rebase --onto newBase oldBase feature/branch

鉴于你的情况:

git checkout PRO # Just to be clear which branch to be on.
git rebase --onto master demo PRO

基本上,从演示到PRO的所有提交,并将它们重新基于主提交。

git branch --set-upstream-to another_branch

我有一个稍微不同的方法,使用重置和存储,避免删除和重新创建分支,以及消除切换分支的需要:

$ 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 commit3
$ 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

通过在每次提交的基础上重置分支,你基本上只需要在每次提交时倒带该分支的历史记录。