[英]Move commit to a new branch
I know it's supposed to be easy, only I am new to Git and a little bit confused of its terminology (like HEAD or Origin for instance...are they the same ?) 我知道这应该很容易,只有Git才是我的新手,并且对它的术语有些困惑(例如HEAD或Origin ...是否相同?)
At the moment I have the branch v4000 with the below commits : 目前,我有以下提交的分支v4000:
---X-Y-Z (v4000 branch head)
I realized that branch v4000 Head should be at commit Y, and I need a new branch v4100 and move commit Z to that one : 我意识到分支v4000 Head应该在提交Y处,并且我需要一个新的分支v4100并将提交Z移动到那个:
---X-Y (v4000 head)
\
Z (v4100 head)
Can you please help me how to achieve this from within Visual Studio 2017 Team Explorer ? 您能帮我如何在Visual Studio 2017 Team Explorer中实现这一目标吗?
Thank you in advance. 先感谢您。
A branch is nothing but a pointer to a certain commit. 分支不过是指向某个提交的指针。 As @LethalProgrammer said in the comments, the
HEAD
of your current branch is the commit it points to. 正如@LethalProgrammer在评论中所说,您当前分支的
HEAD
是它指向的提交。 Origin
now is something completely different, it's the default name for the remote repository (ie the repository on the server, where you pull
from and push
to). 现在,
Origin
是完全不同的东西,它是远程存储库(即服务器上的存储库,从中pull
push
到其中的存储库)的默认名称。
When you're on branch v4000
with Z
as it's HEAD
(your current state as far as I understand), you can just create a new branch v4100
and reset
the v4000
branch back to Y
. 当你在分支
v4000
与Z
因为它是HEAD
(你目前的状态,据我所知),你可以创建一个新的分支v4100
和reset
的v4000
分支回Y
。
On the command line, this looks like this: 在命令行上,如下所示:
git stash
stash any uncommited changes git stash
隐藏所有未提交的更改
git branch v4100
create branch v4100
with Z
as it's head git branch v4100
以Z
为头部创建分支v4100
git reset --hard Y
with Y
being the commit' s SHA id: reset branch v4000
's HEAD
to commit Y
git reset --hard Y
, Y
为提交的SHA ID:重置分支v4000
的HEAD
提交Y
git stash pop
restore previously stashed changes, if any git stash pop
恢复以前隐藏的更改(如果有)
You can also do this via VSTeamExplorer, but I don't know how to stash there, so be sure that you have no uncommited changes before you try this: 您也可以通过VSTeamExplorer进行此操作,但是我不知道如何将其存储在其中,因此在尝试此操作之前,请确保您没有未提交的更改:
Select Branches
-> New Branch
, enter v4100
as branch name, uncheck Checkout branch
and click create. 选择
Branches
-> New Branch
,输入v4100
作为分支名称,取消选中Checkout branch
,然后单击创建。 This creates branch 4100
with commit Z
as it's HEAD
. 这将创建带有提交
Z
的HEAD
分支4100
。
Then, still in the Branches
tab, select Actions
-> View History
. 然后,仍在“
Branches
选项卡中,选择“ Actions
->“ View History
。 In the opening window, right-click commit Y
and select Reset
-> Delete Changes (--hard)
. 在打开的窗口中,右键单击提交
Y
并选择Reset
- > Delete Changes (--hard)
。
EDIT: As always, be careful when using git reset --hard
either way, since it can potentially result in data loss. 编辑:与往常一样,无论哪种方式使用
git reset --hard
都要小心,因为它可能会导致数据丢失。
Just this description should be enough to convince you that using git via command line is cleaner and it's much easier for people to explain stuff via native git commands. 仅此描述就足以说服您通过命令行使用git更干净,而且人们通过本机git命令解释内容也更加容易。 And,as @axiac recommended in the comments, reading the Git Book is definitely worth the time.
而且,正如@axiac在评论中建议的那样,阅读Git书绝对值得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.