23

I've found instructions on how to revert a single file in a Git repository to an earlier version. But I am curious if anyone knows of a way to do this using Git Extensions for Visual Studio. This tool works great so far and I really like it, but I can't figure out how to do this one thing.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Brady Moritz
  • 8,624
  • 8
  • 66
  • 100

5 Answers5

31

The easiest way to do this in Git Extensions is to open the file history (e.g., from the context menu in Visual Studio) for the file you want to revert. Then find the revision you need and choose "Save As" in the context menu.

To revert changes made to a file in the working directory, just choose "revert changes" from the context menu in Visual Studio.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Henk
  • 617
  • 4
  • 4
  • 3
    that's what I'd done before, so i guess this is the only way when using extensions. – Brady Moritz Mar 05 '11 at 15:25
  • 3
    I believe that "context menu" here refers to the one in Solution Explorer. I was unable to find a "Save As" option within that context menu. So, instead, I opened the version of my file of interest from the file history, and then chose "Save As" from the Visual Studio "File" menu. – DavidRR Feb 09 '18 at 18:51
22

While the technique suggested by @Henk will work, here's the Git way.

  1. Select the commit that changed the file you wish to revert.
  2. Choose the revert commit operation. Be sure you tell Git Extension not to automatically commit the revert operation. This will result in a) A revert operation on the affected file(s) and b) Reverted file(s) being staged

Then you can unstage the file(s) that you do not wish reverted -- leaving only the one file that you do wish to revert to be committed.

While this might seem more tedious that just File > Save As, it comes in handy when you have to revert a handful of files.

Sri Sankaran
  • 8,120
  • 4
  • 38
  • 47
  • @boomhauer Please see my last edit. When you revert a commit (especially one that includes more than the file of interest) you **must** tell Git Extensions not to automatically commit the revert operation. While attempting to state that I had somehow missed out the "not to" part. I apologize for not proof-reading my own post. – Sri Sankaran Oct 30 '11 at 10:52
1

Edited my post. I don't know why I responded about mercurial. Sorry about that. The same idea goes for git. I used tortiseGit (when I used git), but the command line is easier. TortiseGit is still a right click to revert. The command line is just 'git checkout filename' to get the checkout from the HEAD.

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
1

You have to be aware that reverting a commit in the history will revert any other files (if there were any) within that same commit.

If you want to truly revert just a single file from a commit, I find the following works:

  1. For the file in question, right click and view history.

  2. In the history find the commit you want to go back to (not the commit you are reverting). Right click and view commit details.

  3. In the top of the commit details, click the 3 dots and copy the commit id.

  4. Drop down to a command prompt, in the working folder type the following using the commit id from #3

    git checkout [commit id] -- path/to/file

  5. Commit the change, either from command prompt or in Visual Studio

    git commit -m 'message: reverted file'

obaylis
  • 2,904
  • 4
  • 39
  • 66
0

To revert changes in a file, got to its Gitlens File History, and then right click on the previous version (the one before your changes). Then, click on "Restore (Checkout)"

VS Code - revert changes

Jack'
  • 1,722
  • 1
  • 19
  • 27