How do you get a specific version from Git in Visual Studio 2015?

Multi tool use


How do you get a specific version from Git in Visual Studio 2015?
Is there a way to get a specific version (from a specific commit) of a file in Visual Studio 2015 - Team Explorer/Team Services Git?
I simply wish to run the solution with a previous version of a file just to see how things used to run and then come back to the latest version to continue development.
I did not create any branches. I kept on committing in the "master" branch.
2 Answers
2
In Visual Studio 2015, if you do View History (from the Actions menu on the Changes panel in Team Explorer):
Then right click on the commit you're interested in:
You can create a branch from there:
I cannot see a way to just checkout to the commit in Visual Studio.
Working with the command line you can do a checkout of the commit SHA you want to use:
git checkout 9eab01d9
When you are done, just check out master again:
git checkout master
You might get warnings about working on a detached head, in that case you could create a branch temporarily:
git checkout -b temp-branch-name 9eab01d9
It is a good idea to get comfortable with the Git command line, the Visual Studio tooling is coming along, but it misses a lot of features.
Thanks to Matt Frear for pointing out that my original answer would
reset the entire solution to a specific commit, whereas the question
was how to use a specific version of one file while keeping the
latest of everything else. Keeping the original post contents at the
bottom, in case someone finds that useful.
To keep your entire solution at the latest but use an older version of an individual file:
Using VS 2015 Update 3:
I will add screenshots and clean-up/reformat the answer to be easier to follow, but I wanted to get an amended answer out there which addresses the question more accurately until I have time to revisit it.
To reset the entire solution/source repository to a specific commit:
Using VS 2015 Update 3:
IMPORTANT
With this approach, any outgoing commits will be lost.
Make sure to perform step 1 (push any outgoing commits)
Right-click on the desired commit and select "Reset > Reset and Delete Changes (--hard)"
In Team Explorer > Sync and the in the View History window, you will end up with Incoming Commits from the desired commit to the latest commit in the remote branch, and your local code will match the desired commit.
When you're done, perform a Pull in Team Explorer > Sync to bring your local branch to the remote branch's latest commit.
See this great answer which explains the 'git reset' command and the difference between --hard vs --mixed.
@MattFrear You are correct, sir! I have updated the answer accordingly. Thank you for pointing that out!
– Chris Tossing
May 24 at 20:13
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The question was how to get a previous version of a specific file. What you have suggested will get a previous version of the entire solution.
– Matt Frear
Sep 2 '17 at 13:48