233

我有一棵这样的树:

(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 demo和一些提交)。然后,我错误地从演示(git checkout -b PRO和一些提交)创建了另一个分支,现在我需要将 PRO 分支移动到 master 并保持 demo 完好无损。最后,demo 和 PRO 都会挂在 master 上。

4

6 回答 6

472

假设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

基本上,您从 afterdemo到包括的所有提交PRO,并将它们重新定位到master提交。

于 2012-06-01T16:20:25.790 回答
36

我会尽量做到通用。首先,确保您在所需的分支上:

git checkout current-branch

然后使用以下命令(new-base-branch您想成为新基地的分支在哪里,并且current-base-branch是您当前基地的分支。)

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

如果你没有冲突,那就太好了——你已经完成了。如果您这样做(在大多数情况下),请继续阅读。

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

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

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


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

于 2018-03-08T20:16:53.557 回答
33

签出到PRO分支,复制此分支的最旧 ( commit4 ) 和最新 ( commit5 ) 提交哈希并粘贴到其他位置:

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

删除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'

将(cherry-pick)Previous 分支的提交范围PRO带入新PRO分支:

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

现在,如果一切正常,则强制 (-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
于 2017-09-28T04:41:05.343 回答
3

我使用 reset 和 stash 的方法略有不同,可以避免删除和重新创建分支以及消除切换分支的需要:

$ 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

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

于 2018-01-25T20:55:26.203 回答
0

我知道这个问题已经很老了,但如果它对某人有帮助,请分享。

我的情况相同,我从不同的基础分支提出 PR,并且更改基础分支和变基显示 200 多个文件冲突。

即使解决它也显示了我不想要的旧提交。所以我所做的是

  1. 从本地删除分支
  2. 创建具有相同名称的新分支,确保这次选择正确的基础分支
  3. Cherry-pick commit id 如果你有的话

最后在命令下运行

git push origin +branchName:branchName

在上面,您也可以-f使用+. 上面的命令使我的分支处于一种状态,而不会影响我的拉取请求。

于 2021-06-07T12:43:18.297 回答
-4
git branch --set-upstream-to another_branch
于 2021-11-12T17:32:30.090 回答