Reputation: 8721
In Visual Studio (if you're using a git repository) you can right-click a modified file and select "Compare with unmodified". There you can see a diff in which you can edit the current version of the file. To clarify: You can edit your current uncommitted changes in source files, in a diff view compared to the latest commit. To contrast that, comparing 2 commits in diff view only shows you read-only copies of these files in temp folder which you cannot edit.
But when you're looking at a diff between the current revision and some older revision (by selecting the top commit and one of the older ones with Ctrl-click and in the right-click menu selecting "Compare commits...") you cannot edit the files from the current revision.
Is there a way to enable editing the current revision files in diff view in Visual Studio when comparing any older revision (not just the previous one) with the current revision?
Side note: One weird trick which developers hate is resetting to the desired older revision with "keep changes" and making a commit, thus making it possible to "compare with unmodified" as if it was a previous commit. But this is hacky and undesirable.
Upvotes: 8
Views: 2858
Reputation: 1506
This is unfortunately not currently supported when diffing two versions from the VS 2017 Git History tool window, even if one of the versions is the current workdir version. We also don't currently have the ability to choose a specific version with which to do the diff from the Changes page. I will add this to our backlog for consideration for a future Visual Studio release.
As you noticed, the diff is sometimes editable from the Git Changes page in Team Explorer. Here's a summary of how we determine if the diff window will be editable.
Hope this helps, and thanks for raising this question.
Upvotes: 8
Reputation: 1324827
First, make sure to use the (latest 2.41 or more, Feb. 2018) Visual Studio: issue 1454 reported that "Compare with Unmodified" had some issue before.
Second, "Compare with Unmodified" is the equivalent of a git diff HEAD
(see "what the difference between git diff HEAD
vs --staged
")
That is why the "hack" was to "reset to the desired older revision with "keep changes" and making a commit": the HEAD was reset to a commit reflecting an old commit, while your local changes are still there, thus enabling the "Compare with Unmodified" feature.
While "hacky and undesirable", it is one to simulate that feature with an older commit while staying in Visual Studio (as opposed to switching to the command line)
Maybe another approach would be to create and checkout a new branch on the old commit. As explained in "Why git keeps showing my changes when I switch branches (modified,added, deleted files) no matter if I run git add or not?", your current changes should be preserved, but HEAD would have changed to the old commit, again allowing for a "Compare with Unmodified".
The OP adds:
To contrast that, comparing 2 commits in diff view only shows you read-only copies of these files in temp folder which you cannot edit.
It make sense comparing between two older version does not allow editing: where would you apply those changes?
It is best to create a new branch on one of those versions, and then compare with the other commit.
Upvotes: 2