How do you get a specific version from Git in Visual Studio 2015?
Solution 1
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.
Solution 2
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 Visual Studio 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.
- View History on the branch (Team Explorer → Branches → right-click on branch)
- Right-click on the desired commit and select Reset → Reset and Keep Changes (--mixed). Your local code will still be what is in the latest commit, but all changes since the desired commit will be shown as pending changes in Team Explorer → Changes. Your branch pointer is now on the commit that you reset on, but the code is still what is in the commit you started with (latest commit).
- Go to Team Explorer → Changes, right-click on the file for which you want to use the version in the desired commit and select "Undo Changes...". This will revert that file to the commit that you reset on - undoing back to what is in that commit.
You will now have the latest of every file in the repository except for the file that you just undid the changes on. You could now reset mixed again on the latest commit to see only the one file that you are using the old version of in Team Explorer → Changes, but if all you're trying to do is run the solution, this step is unnecessary.
To reset the entire solution/source repository to a specific commit:
Using Visual Studio 2015 Update 3:
IMPORTANT
With this approach, any outgoing commits will be lost.
Make sure to perform step 1 (push any outgoing commits)
- Make sure you don't have any outgoing commits - perform a Push, if you do have outgoing commits (*Team Explorer → Sync → Outgoing Commits)
- View History on the branch (Team Explorer → Branches → right-click on branch)
Right-click on the desired commit and select Reset → Reset and Delete Changes (--hard). . In Team Explorer → Sync and then 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.
Solution 3
(This works in both VS2015 and VS2017.)
Note this question, and this answer, is for a single file (or small number of files). If you want to do this for many files, simply make a new branch from, or reset hard to, the desired commit like the other answers propose. But since checking out an old commit can take a lot of time, especially for large repos, I believe this is the simplest way for a single file:
- In Solution Explorer, open the current version of your file.
- Still in Solution Explorer, right click on the file and choose "View History".
- In the history window, find the commit for the version you want to test with. Right click on it and choose "Open". This pops up a temporary file with the contents of the desired version.
- Copy the contents of that version (Ctrl-A Ctrl-C) and paste it over the actual current version.
Now you can do whatever you want to do with that file, and then undo the pending change when you're done.
TchiYuan
Software Developer from Montreal Canada. http://www.linkedin.com/in/tchiyuan
Updated on September 18, 2020Comments
-
TchiYuan over 3 years
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.
-
Matt Frear over 6 yearsThe 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.
-
Chris Tossing almost 6 years@MattFrear You are correct, sir! I have updated the answer accordingly. Thank you for pointing that out!
-
Chris Tossing over 5 yearsThe question was how to get a previous version of a specific file. This answer will get a previous version of the entire solution. If you want reset just one file to a specific commit while leaving the rest of the solution on a different commit, see this answer
-
Chris Tossing over 5 yearsThat answer also contains, at the bottom, steps to reset the entire solution to a specific commit without having to create a temporary branch as shown in this answer.
-
Irf about 5 yearskudos @Dave for using/showing through Visual Studio shortcuts
-
picolino about 4 yearsSeems like if you need to test about 816 changed files you'll probably die before finished
-
TTT about 4 years@picolino in that case of course you just checkout an older commit. But this question is how to test a specific version of a single file (presumably without taking the time to checkout and/or branch and reload everything).
-
Yonatan Nir about 2 yearsJust to update that the same procedure as the accepted answer works in VS 2019