Search code examples
gitvisual-studiogit-detached-head

Detached HEAD in Visual Studio


Is there a way to get into a detached HEAD state using Visual Studio (2015 on Win10 x64, specifically) without having to go to the command line and do it manually?

Edit: to give context, a) I'm new to git, coming from TFVC world, and b) I'm trying locally to view and compile the project to get a screenshot of the old state of the website; I don't need to make changes and after that I'll move the head & index back to the furthest commit - perhaps there are other ways to accomplish this?


Solution

  • No, and having a detached HEAD is not generally a desirable state to be in, it's generally an implementation detail of an operation in Git (for example, it's something that rebase does.)

    If you want to work on an old version, you can simply reset your current branch to that commit. (You may wish to create a new branch just to do this, so that you can easily switch back to your master branch.)

    For example, if you're on the master branch and you want to temporarily check out an older commit, first make sure that you do not have any uncommitted changes. This process will reset the state of your repository.

    After you are sure you are in a safe state to continue, create and checkout a new temporary branch:

    Open the Team Explorer Branches page. Select the master branch. Right click, and select "New Local Branch From".

    New Local Branch From master

    Enter a new branch name, for example: old. Keep the "Checkout branch" checked, and select "Create Branch".

    Create Branch

    Still in the Branches psage, right click on the old branch and select "View History".

    History View

    In the History view, find the commit that you want to check out. Right click on the commit and select Reset > Reset and Delete Changes.

    Reset Hard

    Once you have finished building and examining this old commit, you can switch back to the master branch and (optionally) delete the old branch.

    Note: you need Visual Studio 2015 Update 2 or better in order to Reset.