8

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.

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • What you are asking for does not make sense. When you are comparing 2 commits, it just shows the diff between 2 commits. If you want to make changes to your current working, uncommitted version, you just go ahead and edit the file. These are two separate operations. – junkangli Apr 09 '18 at 12:32
  • @junkangli does the ability to edit the current file version not make sense to you? Then if you right-click a file in your git repo and hit "Compare with unmodified", you will find your ability to edit the current version very nonsensical :p I can't make you think it makes sense, but I want to know how to make it possible in a slightly different way. – user1306322 Apr 09 '18 at 13:40
  • When you choose to "Compare with unmodified", you are comparing the changes you have made on that file. That is different from selecting 2 commits and comparing them. – junkangli Apr 09 '18 at 13:45
  • Yes, but in that view I want to be able to view cumulative diffs in each file and be able to edit the current version. Which is the same as "Compare [this file, not 2 commits] with earlier revision". – user1306322 Apr 09 '18 at 13:47
  • This is where it does not make sense. I suggest you read up about git basics https://git-scm.com/book/en/v2/Getting-Started-Git-Basics – junkangli Apr 09 '18 at 13:57
  • @junkangli does "Compare with unmodified" make sense to you? Have you used Visual Studio's git tools? – user1306322 Apr 09 '18 at 13:58
  • 1
    This seems to be a question about the Visual Studio tool, not about Git itself. If one treats the tags as "and" (which makes sense) the tagging is all good. Unfortunately many readers treat the tags as "or" requests... – torek Apr 09 '18 at 16:52
  • I think you might be wanting better access to the indexed state and the HEAD commit, seems to me you're asking for GUI access to an ordinary Git workflow, selectively reverting diff hunks from commit ranges, yes? With Git the easiest way to excise hunks is often `git (checkout|reset|add) --patch` with maybe some `git reset --soft`ing, and I'm gathering VS doesn't reach that, right? It's a bit difficult to understand what you're after, does VS really not allow you to edit files while looking at diffs? – jthill Apr 22 '18 at 02:13
  • @jthill it does, please read the post and the comments again and try it – user1306322 Apr 22 '18 at 08:47
  • @user1306322 Following your edited question, I have completed my answer below accordingly. – VonC Apr 22 '18 at 09:39

2 Answers2

7

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.

  • Workdir changes only:
    • Diff workdir with HEAD, workdir content editable in diff.
  • Staged changes only:
    • Diff workdir with HEAD (since workdir content matches index), workdir content editable, changes to workdir are not automatically staged.
  • Staged and workdir changes:
    • Diff staged with HEAD, staged content is read-only.
    • Diff workdir with index (staged), workdir content is editable.

Hope this helps, and thanks for raising this question.

Chad B
  • 1,416
  • 9
  • 12
  • Are you one of the VS developers? It would help if you added some information into your profile page, especially to find out how credible your sources are – user1306322 Apr 23 '18 at 16:23
  • 1
    Thanks for the feedback. I updated my profile. Yes, I wrote a lot of the code in Team Explorer, Git integration in VS, etc. – Chad B Apr 23 '18 at 16:37
  • @ChadB is the most credible source I know when it comes to Team Explorer and Git integration with Windows. That and an awesome dev. – jessehouwing Apr 29 '18 at 07:49
  • 1
    A related ask that I would personally use at least 10 times per day: when viewing a list of files in a diff or a commit, have an option to simply open the local (current HEAD) version. Currently when you right click a file you get options for "Open" or "Compare with source". An additional option for "Open current version", or even just opening the containing folder of the current file, would be outstanding. – TTT Mar 23 '21 at 18:16
2

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • My question isn't about editing 2 old already committed revisions. It's about editing the current uncommitted working copy in a diff view with *any* older committed revision (not only the latest, as visual studio allows at the time of posting). – user1306322 Apr 22 '18 at 10:04
  • @user1306322 Then the first part of my answer is *precisely* about " editing the current uncommitted working copy in a diff view with any older committed revision" – VonC Apr 22 '18 at 10:07
  • I'm just clarifying! A lot of people don't understand what the question is about. – user1306322 Apr 22 '18 at 10:08
  • @user1306322 OK. Hopefully, I did. – VonC Apr 22 '18 at 10:08
  • I have 0 experience with console git, and I don't know what HEAD is, believe it or not. You just kinda explained in different terms *how* it works and described my workaround. I don't know about the rest of your answer, but I already said in my question that it's undesirable to switch around the "current" local state (the inconvenience of it) just to remove the restriction on editing the file with uncommitted changes. Now that the person who I trust is an official source on this topic said this isn't working by design and may be reconsidered in a future release, I think I got my answer :p – user1306322 Apr 24 '18 at 19:27
  • @user1306322 OK. I'll leave this answer here, as understanding what the tool is doing behind the scene (in term of Git commands) could help other users. – VonC Apr 25 '18 at 06:49