如何在 Visual Studio 2015 中从 Git 获取特定版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38599554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get a specific version from Git in Visual Studio 2015?
提问by TchiYuan
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?
有没有办法在 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.
我没有创建任何分支。我继续在“主”分支中提交。
回答by DaveShaw
In Visual Studio 2015, if you do View History(from the Actionsmenu on the Changespanel in Team Explorer):
在 Visual Studio 2015 中,如果您执行查看历史记录(从Team Explorer的Changes面板上的Actions菜单):
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.
我看不到只检出 Visual Studio 中提交的方法。
Working with the command line you can do a checkout of the commit SHA you want to use:
使用命令行,您可以检查要使用的提交 SHA:
git checkout 9eab01d9
When you are done, just check out master again:
完成后,只需再次检查 master:
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.
熟悉 Git 命令行是个好主意,Visual Studio 工具正在出现,但它缺少很多功能。
回答by Chris Tossing
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 filewhile keeping the latest of everything else. Keeping the original post contents at the bottom, in case someone finds that useful.
感谢 Matt Frear 指出我最初的答案会将整个解决方案重置为特定的提交,而问题是如何使用一个文件的特定版本,同时保持其他所有内容的最新版本。将原始帖子内容保留在底部,以防有人觉得有用。
To keep your entire solution at the latest, but use an older version of an individual file:
要使整个解决方案保持最新,但使用单个文件的旧版本:
Using Visual Studio 2015 Update 3:
使用 Visual Studio 2015 更新 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.
- 查看分支上的历史记录(团队资源管理器→分支→ 右键单击分支)
- 右键单击所需的提交并选择Reset→ Reset and Keep Changes (--mixed)。您的本地代码仍将是最新提交中的代码,但自所需提交以来的所有更改都将在Team Explorer→ Changes 中显示为挂起的更改。您的分支指针现在位于您重置的提交上,但代码仍然是您开始的提交(最新提交)中的代码。
- 转到Team Explorer→ Changes,右键单击要在所需提交中使用版本的文件,然后选择“Undo Changes...”。这将将该文件恢复到您重置的提交 - 撤消回该提交中的内容。
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.
您现在将拥有存储库中每个文件的最新版本,但您刚刚取消更改的文件除外。您现在可以在最新提交时再次重置混合,以仅查看您在Team Explorer→ Changes中使用旧版本的一个文件,但如果您尝试做的只是运行解决方案,则此步骤是不必要的。
To reset the entire solution/source repository to a specific commit:
要将整个解决方案/源存储库重置为特定提交:
Using Visual Studio 2015 Update 3:
使用 Visual Studio 2015 更新 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 → Sync→ Outgoing Commits)
- View Historyon the branch (Team Explorer→ Branches→ right-click on branch)
Right-click on the desired commit and select Reset→ Reset and Delete Changes (--hard). Reset and Delete Changes (--hard)"">
. In Team Explorer→ Syncand 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→ Syncto bring your local branch to the remote branch's latest commit.
- 确保您没有任何传出提交 - 如果您有传出提交,则执行推送(*团队资源管理器 →同步→传出提交)
- 查看分支上的历史记录(团队资源管理器→分支→ 右键单击分支)
右键单击所需的提交并选择Reset→ Reset and Delete Changes (--hard)。 重置和删除更改(--hard)”">
. 在Team Explorer→ Sync 中,然后在 View History 窗口中,您最终将收到从所需提交到远程分支中最新提交的传入提交,并且您的本地代码将匹配所需的提交。
完成后,在Team Explorer→ Sync 中执行拉取操作,将本地分支带到远程分支的最新提交。
See this great answerwhich explains the 'git reset' command and the difference between --hard vs --mixed.
请参阅这个很好的答案,它解释了“git reset”命令以及 --hard 与 --mixed 之间的区别。
回答by TTT
(This works in both VS2015 and VS2017.)
(这适用于 VS2015 和 VS2017。)
I believe this is the simplest way:
我相信这是最简单的方法:
- 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.
- 在解决方案资源管理器中,打开文件的当前版本。
- 仍然在解决方案资源管理器中,右键单击该文件并选择“查看历史记录”。
- 在历史记录窗口中,找到要测试的版本的提交。右键单击它并选择“打开”。这会弹出一个包含所需版本内容的临时文件。
- 复制该版本的内容 (Ctrl-A Ctrl-C) 并将其粘贴到实际的当前版本上。
Now you can do whatever you want to do with that file, and then undo the pending change when you're done.
现在您可以对该文件执行任何您想做的操作,然后在完成后撤消挂起的更改。