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

后端 未结 3 744
孤街浪徒
孤街浪徒 2020-12-25 09:43

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 solutio

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-25 10:10

    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.

    1. View History on the branch (Team ExplorerBranches → right-click on branch)
    2. Right-click on the desired commit and select ResetReset 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 ExplorerChanges. 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).
    3. Go to Team ExplorerChanges, 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 ExplorerChanges, 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)

    1. Make sure you don't have any outgoing commits - perform a Push, if you do have outgoing commits (*Team Explorer → SyncOutgoing Commits)
    2. View History on the branch (Team ExplorerBranches → right-click on branch)
    3. Right-click on the desired commit and select ResetReset and Delete Changes (--hard). . In Team ExplorerSync 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.

    4. When you're done, perform a pull in Team ExplorerSync 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.

提交回复
热议问题