如何在Git中将提交从主干移动到分支?

时间:2010-03-04 01:43:28

标签: git git-svn branch git-rebase git-reset

我向主人做了一堆提交,并意识到他们本应该在一个分支中。

我看过各种关于变基,合并和重置主人的事情。但是没有任何操纵尝试产生了一个看起来像我正在尝试做的历史。

我的尝试让我相信它需要rebase --ontoreset --hard的某种组合来及时移动主人。但是我对Git分支的理解还有待改进。这样做的一部分是学习如何使用它。

应该注意的是,我试图移动的所有更改都没有被推出。

电流

  * remote/trunk
--o--a--b--c--d--e--f     <- master
  |
  o                       <- remote branch foo

期望结果

  * remote/trunk
--o                       <- master
  |
  o--a--b--c--d--e--f     <- remote branch foo

3 个答案:

答案 0 :(得分:9)

Martin的回答不一定适用于您的情况,但我想发布它:)

假设您忘记在提交o创建分支,那么您有:

x--y--z--o--a--b--c--d--e--f  master
         |
         +
   [forgot to make a branch here]

然后你意识到你真正想要的是:

x--y--z--o   master
         |
         +--a--b--c--d--e--f  topic

在这种情况下你可以做的是使用它的哈希在o创建一个分支:

git branch topic # creates new branch 'topic' - will be at commit `f`
git checkout o -b newmaster # creates new branch called newmaster pointing on commit `o` (please replace `o` with the actual hash)
git branch -M newmaster master # force rename newmaster to master (means master points on hash `o`)

你将在主分支(提交o),所以作为最后一步你可以:

git checkout topic

哈希当然可以只是前5个字符..

修改

您使用git-svn并不重要,真正重要的是您在o之后的任何时候都没有发布您的主分支

git中的分支实际上只是指向提交的指针。这就是分支如此便宜的原因:你只需创建一个指针,然后你就有了一个分支。

我不知道如何跟踪远程分支,您可能需要在重命名/移动分支后进行设置。

答案 1 :(得分:6)

我不确定重命名分支是正确的解决方案,因为它可以帮助您:

  * remote/trunk
--M--a--b--c--d--e--f     <- master
  |
  F                       <- remote branch foo

为:

--F                       <- master
  |
  M--a--b--c--d--e--f     <- remote branch foo
  * remote/trunk

(如果您重命名remote/foo,这是不可取的:您应首先跟踪它,然后重命名,但即使最终结果与您的需求不同)

这不是你想要的“期望结果”(foo需要从F开始,而不是M):

  * remote/trunk
--M                       <- master
  |
  F--a--b--c--d--e--f     <- remote branch foo

您只能通过 rebase --onto

实现这一目标
git checkout --track -b origin/foo  # create a local branch named after the remote one
git branch tmp                      # mark current foo HEAD to 'F'
git branch -f foo master            # put foo where it should b: at 'f'
git branch -f master tmp^           # reset master to M, parent of tmp
git checkout tmp                    # go to where we must replay the commits
git rebase --onto tmp master foo    # replay a to f on top of tmp
git svn dcommit                     # push the local foo in order to update remote/foo

给你:

  * remote/trunk
--M                             <- master
  |
  F--a'--b'--c'--d'--e'--f'     <- local foo and remote branch foo

答案 2 :(得分:1)

几乎纠正hasen j建议的内容,但我不得不进行一些小修改(我使用git-svn):

# create the branch with your commits
git branch performance
# fork the master to a new branch at the commit before your local, non pushed commits
git branch newmaster 2d0516dfe8252de87
# checkout your branch 
git checkout performance
# rename the newmaster
git branch -M newmaster master
# now checkout the master
git checkout master

你不能重命名你所在的分支,所以我检查了我提交提交的性能分支。

相关问题