如何将当前在一个分支上的一些最后的 Git 提交移动到另一个分支上?
我有一个存储库,我一直在 master
分支上工作,最后添加了大约 10 个左右的提交,我现在希望这些提交位于另一个分支上,因为它们描述了我现在认为是实验性的工作(我仍在学习良好的 Git 实践)。
考虑到这一点,我现在希望将最后 10 次提交形成自己的分支,这样我就可以清理 master
并仅为“发布”/“稳定”提交保留。
为了说明这一点,我所拥有的是:
b--b (feature B)
/
X--X--X--Z--Z--Z--Z--Z--Z (master)
\
a--a--a (feature A)
您可以看到标有 X
和 Z
的提交都在 master
分支上,而我想要的是用 Z
标记的提交(现在被认为是实验性的“功能 Z”工作)位于自己的分支和以最右边的 X
结尾的 master
上。为了说明这一点,所需的图表:
b--b (feature B)
/
X--X--X (master)
\ \
\ Z--Z--Z--Z--Z--Z (feature Z - the new branch I want)
\
a--a--a (feature A)
这样我将保留我的 master
用于发布和其他稳定提交,能够根据需要合并 A、B 和 Z 功能。
那么如何将“Z”提交移动到它们自己的分支上呢?
I have a repository where I had been working on master
branch having last added some 10 or so commits which I now wish were on another branch, as they describe work that I now consider experimental (I am still learning good Git practices).
In light of this consideration, I would now like to have these last 10 commits form their own branch so to speak, so that I can have master
clean and reserved for "release" / "stable" commits only.
To illustrate, what I have is:
b--b (feature B)
/
X--X--X--Z--Z--Z--Z--Z--Z (master)
\
a--a--a (feature A)
You can see that commits marked with X
and Z
are all on the master
branch, while what I want is commits marked with Z
(the now considered experimental "feature Z" work) to lie on their own branch and master
ending with the rightmost X
. To illustrate, the desired graph:
b--b (feature B)
/
X--X--X (master)
\ \
\ Z--Z--Z--Z--Z--Z (feature Z - the new branch I want)
\
a--a--a (feature A)
That way I will have my master
reserved for releases and otherwise stable commits, being able to merge in A, B and Z features as needed.
So how do I move the "Z" commits onto their own branch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其中 commit_id 是 b 分支之前最后一次提交的标识符。
where commit_id is an identifier of that last X commit before b branches off.
为了完整起见,答案在这里 - http://git-scm.com/docs/git-reset - 搜索文本“撤消提交,使其成为主题分支” - 该示例显示将最后 3 个提交设为一个分支,并将 master 重置为这 3 个提交之前的提交:
For completeness, the answer is here - http://git-scm.com/docs/git-reset - search for the text "Undo a commit, making it a topic branch" - the example shows making the last 3 commits a branch and resetting master to the commit previous to those 3 commits:
只需重命名 master 并在最后一个 X 处启动一个新的 master:
git checkout master; git 分支 -m 功能; git checkout -b master HEAD~6
Simply rename master and start a new master at the last X:
git checkout master; git branch -m feature; git checkout -b master HEAD~6
在所需的新分支中创建并签出
强制主分支 N 提交回来(在您的情况下为 10)
Create and checkout in desired new branch
Force master branch N commits back (in your case 10)