简体   繁体   中英

Revert a single file to earlier commit using Git Extensions for Visual Studio

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.

The easiest way to do this in Git Extensions is to open the file history (eg, 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.

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.

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM