0

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.

4
  • 1
    "I know it's supposed to be easy, only I am new to Git and a little bit confused of its terminology" -- read the Git book and it will be easy for you too.
    – axiac
    Commented Dec 4, 2017 at 11:30
  • Origin is the remote server. HEAD is just the reference to the last commit what made in the currently checked out branch. When you switch branch HEAD points at the top of the new branch. You can think of HEAD and tail analogy. Commented Dec 4, 2017 at 11:31
  • 1
    @LethalProgrammer : Thanks for the explanation
    – user4676463
    Commented Dec 4, 2017 at 11:53
  • 1
    This question is the same as yours, except that it is not about how to do this in Visual Studio specifically. But I suppose the steps outlined in the answers to that question can also be taken in Visual Studio, if that is what you are looking for. Commented Dec 4, 2017 at 13:10

1 Answer 1

2

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 (i.e. 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.

2
  • Good explanation. If there are more commits to be moved would you rebase? Commented Dec 5, 2017 at 23:47
  • Note that you do not really move any commits here, but only make the branches point to the right commits. Thus, rebasing is not necessary here. An arbitrary number of commits could exist between Y and Z, the commands would still be the same.
    – kowsky
    Commented Dec 6, 2017 at 8:03