如何在git中将我的十个提交从master转移到一个新的分支?

时间:2013-06-04 17:16:14

标签: git branching-and-merging

我需要做什么: 我有10个提交在master上。我需要将这些提交移动到一个新的分支。我一直在拉扯,但我还没推动我的工作。我怎样才能将我的十个提交变成一个我可以推送的新分支?

我已尝试执行in this question here步骤,但它们不起作用。具体来说,当我进行rebase时,我最终得到的就是master上的提交,而不是我的新分支上的提交。没有任何提交被移动到新的分支,我不知道如果在问题中提供的步骤可以做到这一点。

3 个答案:

答案 0 :(得分:2)

根据我的理解,这就是你所拥有的:

* commit 10 (master)
|
* commit 9
|
...
|              * (origin/master)
* commit 1     |
|.------------^
*

我认为你想要的是在一个新的分支上提交1-10。

为此,您可以使用分支名称简单地标记分支,然后将master重置为origin / master具有的内容。

所以:

git checkout master # your current latest set of changes (commit 10)
git branch feature # the name of your branch
git reset --hard origin/master # sets master to point to origin/master
git pull --rebase # updates master with origin/master
git checkout feature && git merge master # updates feature branch with what is on origin/master

这将最终得到:

* commit 11 (feature) merge commit
|^------------.
* commit 10    |
|              ...
...
|              * (master, origin/master)
* commit 1     |
|.------------^
*

这是你想做的吗?

答案 1 :(得分:0)

一种选择就是将你的工作推向一个新的分支:

git push HEAD:branchname

这将在服务器上创建一个新的分支branchname,其中包含您当前的工作。 - 在这种情况下,它可能会让人感到困惑,因为你的工作仍在当地的主人身上。

要做到这一点,您只需将本地主服务器重置为远程版本:

git reset --hard @{u}

请注意,这会丢掉所有未提交的机会。

要在Branchname上工作,您可以git checkout branchname

答案 2 :(得分:0)

只需重命名您的分支并推送它:

git branch -m branchname
git push -u branchname

此处-u对于更新跟踪分支非常重要。

相关问题