git 更改分支基础

4smxwvx5  于 2023-08-01  发布在  Git
关注(0)|答案(6)|浏览(112)

我有一棵这样的树:

(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


我试过PRO分支的git rebase master,但没有任何React。

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

cx6n0qe3

cx6n0qe31#

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


基本上,您将从demo之后到PRO(包括PRO)的所有提交,并将它们转换为master提交。

imzjd6km

imzjd6km2#

我会尽我所能做到一般化。首先,确保您在所需的分支上:

git checkout current-branch

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

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

**如果你没有冲突,那么很好-你完成了。如果你有(在大多数情况下),那么请继续阅读。

可能会出现冲突,您必须手动解决它们。Git现在尝试在current-branchcurrent-base-branchnew-base-branch之间进行“3-way merge”。大致上,这就是git内部的工作方式:

  1. Git首先会在new-base-branch之上重定current-base-branch的基。可能会有冲突;你得手动解决在此之后,通常执行git add .git rebase --continue。它将为此创建一个新的临时提交temp-commit-hash
    1.在此之后,Git将在temp-commit-hash之上重定current-branch的基。可能会有更多的冲突,您将不得不再次手动解决它们。完成后,再次使用git add .git rebase --continue继续,之后,您已经成功地将current-branch重定在new-base-branch之上。

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

zaqlnxep

zaqlnxep3#

checkout 到PRO分支,复制该分支的最旧(* commit 4 )和最新( commit 5 *)提交哈希并粘贴到其他地方:

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

字符串
删除PRO分支(为了安全起见,保留一个备份)。从master创建并 checkout 到新的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分支的提交范围(cherry-pick)放入新的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

ao218c7q

ao218c7q4#

我有一个稍微不同的方法,使用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 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

字符串
通过在一个提交一个提交的基础上重置分支,你基本上只是一次一个提交地倒回分支历史。

5fjcxozz

5fjcxozz5#

另一个没有提到的选项是使用interactive rebase,你可以简单地标记commit 2 & commit 3删除,只保留commit 1 & commit 4 & commit 5

# Run interactive rebase
git rebase -i HEAD^5

# rebase options will get displayed in text editor
pick a89b78c Commit1
pick b136d0a Commit2
pick c231e8d Commit3
pick d54274b Commit4
pick e87218e Commit5

# keep lines with commits you want in branch and save file
pick a89b78c Commit1
pick d54274b Commit4
pick e87218e Commit5

字符串

kgsdhlau

kgsdhlau6#

git branch --set-upstream-to another_branch

字符串

相关问题