改变分支基础

Iva*_*van 113 git

我有这样一棵树:

(commit 1) - master
                \-- (commit 2) - (commit 3) - demo
                                                \-- (commit 4) - (commit 5) - PRO
Run Code Online (Sandbox Code Playgroud)

我必须将PRO分支移动到掌握

(commit 1) - master
                |-- (commit 2) - (commit 3) - demo
                \-- (commit 4) - (commit 5) - PRO
Run Code Online (Sandbox Code Playgroud)

我试过git rebase masterPRO分支,但没有任何反应.

澄清一下:我在大师工作,然后我不得不做一个产品演示(git checkout -b demo和一些提交).然后,我错误地从demo(git checkout -b PRO和一些提交)创建另一个分支,现在我需要将PRO分支移动到master并保持demo完整.最后,demo和PRO都将从master中挂起.

log*_*yth 217

使用--onto为:

git rebase --onto newBase oldBase feature/branch
Run Code Online (Sandbox Code Playgroud)

鉴于你的情况:

git checkout PRO # Just to be clear which branch to be on.
git rebase --onto master demo PRO
Run Code Online (Sandbox Code Playgroud)

基本上,您从之后获取所有提交,并将它们demo重新PRO绑定到master提交.

  • 我在`--onto`上阅读了这篇[指南](https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase),他们的写作如何帮助我`git rebase --onto newBase oldBase功能/ branch` (6认同)

Saj*_*han 14

Checkout to PRObranch,复制此分支的最旧(commit4)和最新(commit5)提交哈希并粘贴到其他位置.

$ git checkout PRO
$ git log            # see the commit history
# copy the oldest & latest commit-hash 
Run Code Online (Sandbox Code Playgroud)

然后,删除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'
Run Code Online (Sandbox Code Playgroud)

把(挑选)Previous PRO分支的提交范围带到新的PRO分支.

$ git cherry-pick commit4^..commit5   # cherry-pick range of commits
# note the '^' after commit4
Run Code Online (Sandbox Code Playgroud)

现在,如果一切正常,那么强制(-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
Run Code Online (Sandbox Code Playgroud)


Raj*_*Raj 14

我将尝试尽可能地通用.首先要确保你在你的CURRENT分支上.

git checkout current-branch
Run Code Online (Sandbox Code Playgroud)

然后使用以下命令.new-base-branch是你想成为新基地的分支机构.current-base-branch分支是你目前的基础.

git rebase --onto new-base-branch current-base-branch
Run Code Online (Sandbox Code Playgroud)

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

可能会出现冲突,您必须手动解决它们.Git的现在尝试你之间的"三路合并" current-branch,current-base-branchnew-base-branch.大致这是current-base-branch内部如何工作: -

1.)Git将首先new-base-branch在顶部进行重组git add ..可能存在冲突; 你必须手动解决.之后做到这一点,你通常会做git rebase --continuetemp-commit-hash.它将current-branch为此创建一个新的临时提交.

2.)在此之后,Git现在将temp-commit-hash在你的基础上重新定位git add ..可能存在进一步的冲突,您将不得不手动解决它们.一旦这样做,你再继续git rebase --continuecurrent-branch,之后您已成功重订你new-base-branch在上面的git rebase --abort.

注意: - 如果你开始陷入困境,那么你可以new-base-branch在rebase过程中的任何时候做,并回到起点.