简体   繁体   中英

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 ?)

At the moment I have the branch v4000 with the below commits :

---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 :

---X-Y (v4000 head)
      \
       Z (v4100 head)

Can you please help me how to achieve this from within 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. 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).

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 .

  • On the command line, this looks like this:

    git stash stash any uncommited changes

    git branch v4100 create branch v4100 with Z as it's head

    git reset --hard Y with Y being the commit' s SHA id: reset branch v4000 's HEAD to commit Y

    git stash pop restore previously stashed changes, if any

  • 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:

    Select Branches -> New Branch , enter v4100 as branch name, uncheck Checkout branch and click create. This creates branch 4100 with commit Z as it's HEAD .

    Then, still in the Branches tab, select Actions -> View History . In the opening window, right-click commit Y and select Reset -> Delete Changes (--hard) .

EDIT: As always, be careful when using git reset --hard either way, since it can potentially result in data loss.

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. And,as @axiac recommended in the comments, reading the Git Book is definitely worth the time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM