问题描述
我已经完成了一些工作(你的分支在37次提交之前就超过了'origin / master'),它确实应该进入它自己的分支,而不是进入 master
。这些提交只存在于我的本地机器上,并没有被推送到 origin
,但情况有些复杂,其他开发者一直在推动原点/ master>
,并且我已经将这些更改拉下来了。
如何追溯地将我的37个本地提交移动到新分支上?基于文档,看起来像 git rebase - 在my-new-branch master
或 ... origin / master
应该这样做,但都给我错误致命的:需要一次修订。 man git-rebase
没有提供对 rebase
进行修改的任何内容,其示例不会这样做,所以我没有想法如何解决这个错误。
(注意,这是不是重复的或作为这些问题的处理方式?如何将我的本地未提交的更改合并到另一个Git分支?在本地工作树中有未提交的更改,而不是本地提交的更改。)
这应该很好,因为你还没有推动你的提交任何其他地方,你可以在 origin / master
之后自由地重写分支的历史。首先,我将运行 git fetch origin
以确保 origin / master
是最新的。假设您目前位于 master
,您应该可以这样做:
git rebase origin / master
...这将重放所有不提交的提交,在 origin / master
到 origin / master
中。 rebase的默认行为是忽略合并提交(例如,可能引入了 git pull
s)的合并提交,它只会尝试应用每个提交引入的修补程序到 origin / master
。 (你可能需要解决一些冲突。)然后你可以根据结果创建你的新分支:
git分支新建
...然后重新设置您的 master
返回至 origin / master
:
#用于care - 确保git status是干净的,而且你仍然是主人:
git reset --hard origin / master
当使用 git branch
, git reset
等操作分支时, 。我发现使用 gitk --all
或类似的工具频繁查看提交图很有用,只是为了检查我是否理解所有不同的引用指向的地方。
或者,您可以根据主人的位置创建主题分支( git branch new-work-including-合并
),然后像上面那样重置 master
。但是,由于您的主题分支将包含来自 origin / master
的合并,并且尚未推送您的更改,所以我建议您进行重新组合,以便历史记录更加整洁。 (另外,当您最终将主题分支合并回主人时,更改将更加明显。)
I've done a fair bit of work ("Your branch is ahead of 'origin/master' by 37 commits.") which really should have gone into its own branch rather than into master
. These commits only exist on my local machine and have not been pushed to origin
, but the situation is complicated somewhat in that other devs have been pushing to origin/master
and I've pulled those changes.
How do I retroactively move my 37 local commits onto a new branch? Based on the docs, it appears that git rebase --onto my-new-branch master
or ...origin/master
should do this, but both just give me the error "fatal: Needed a single revision". man git-rebase
says nothing about providing a revision to rebase
and its examples do not do so, so I have no idea how to resolve this error.
(Note that this is not a duplicate of Move existing, uncommited work to a new branch in Git or How to merge my local uncommitted changes into another Git branch? as those questions deal with uncommitted changes in the local working tree, not changes which have been committed locally.)
This should be fine, since you haven't pushed your commits anywhere else yet, and you're free to rewrite the history of your branch after origin/master
. First I would run a git fetch origin
to make sure that origin/master
is up to date. Assuming that you're currently on master
, you should be able to do:
git rebase origin/master
... which will replay all of your commits that aren't in origin/master
onto origin/master
. The default action of rebase is to ignore merge commits (e.g. those that your git pull
s probably introduced) and it'll just try to apply the patch introduced by each of your commits onto origin/master
. (You may have to resolve some conflicts along the way.) Then you can create your new branch based on the result:
git branch new-work
... and then reset your master
back to origin/master
:
# Use with care - make sure "git status" is clean and you're still on master:
git reset --hard origin/master
When doing this kind of manipulating branches with git branch
, git reset
, etc. I find it useful to frequently look at the commit graph with gitk --all
or a similar tool, just to check that I understand where all the different refs are pointing.
Alternatively, you could have just created a topic branch based on where your master is at in the first place (git branch new-work-including-merges
) and then reset master
as above. However, since your topic branch will include merges from origin/master
and you've not pushed your changes yet, I'd suggest doing a rebase so that the history is tidier. (Also, when you eventually merge your topic branch back to master, the changes will be more obvious.)
这篇关于将已提交(但未推送)的更改移至新分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!