Git (TortoiseGit) - How to revert a single file to a previous revision and then undo the revert?

30,747

Solution 1

From the command line: git checkout is probably what you want.

The documentation shows an example of:

$ git checkout master~2 Makefile

to revert Makefile to two revisions back in the master branch

From within TortoiseGit (via Windows Explorer) it looks like you can do this with the following steps:

  • Navigate in Explorer to the folder where the file is.
  • Right-click on the file you want to revert, choose Show log from the TortoiseGit context menu
  • In the top section ("graph") select the revision that has the version of the file you want to revert to
  • In the third section (file list) right-click the file and choose Revert to this revision
  • You should get a message like 1 files revert to e19a77
  • Solution 2

    It is also possible to get a specific file:

    • Right-click on the file
    • Choose the menu item: Git Show Log

    TortoiseGit show a dialog with a list of all revisions

    • Click on the prior revision (2)

    In a list in the bottom of the dialog it shows all the files associated with the commit

    • Find the file you are looking to view
    • Right-click on that file and Save Revision To
    • This will save that previous revision of the file with the commit number concatenated on the file

    For example: - original file is file.txt - revision 2 will save as file-67b51a8.txt

    I'm using TortoiseGit v2.2.0.0

    Solution 3

    In TortoiseGit the answer is to right-click the project folder and click Show Log, then right-click the change you want to roll back to and click Switch/Checkout to this... . It will let you then proceed from that weird place in the commit stack, or branch in case you plan to commit and want things to stay sane.

    Solution 4

    Updated my answer, based on these comments:


    Suppose the working tree is clean and you want:

    1. Checkout some file(s) of its previous revision
    2. Testing
    3. Revert to current revision

    1. Checkout some file(s) of its previous revision

      (a) Right click the file you want to revert and Show Log for that file

      enter image description here

      (b) Right click the file in file list and perform Revert to parent revision

      enter image description here

      enter image description here

      (c) repeat (a), (b) until you get all files you want.

      enter image description here

    2. Testing

    3. Revert to current revision

      (a) perform Revert... in context menu of explorer

      This way, you can choose the file(s) you want to revert.

      (b) or this quick way: perform Reset Hard in Log dialog

      This way, all changed files revert. (=> Lost all working dir changes)

      enter image description here

      enter image description here

    (Tested on TortoiseGit 1.8.16.1, GfW 2.6.4.windows.1, Win 10 64bit)

    Solution 5

    For Tortoise Version 2.3.0:

    You can do it in several ways, but fastest i think is that:

    1. Right click on the file
    2. Click the menu option "Diff with previous version"
    3. Right click on the left (previous) version of file
    4. Click the menu option "Use this whole file"
    5. Save the changes (ctrl+s)
    Share:
    30,747
    Alexander
    Author by

    Alexander

    Updated on January 30, 2020

    Comments

    • Alexander
      Alexander over 4 years

      When using Git with TortoiseGit: Does somebody know how to revert a single file(or a complete repository) to a previous revision?

      For example I have a repository containing multiple files. One file exists in three revisions (1 ; 2 ; 3). Now I want to change from revision 3 back to 2.

      TortoiseGit offers a "Revert" function in the "Show log" dialog which allows to jump back to a specific revision, but this will revert your whole repository instead of a single file.

      Also once I have reverted something, I don't have a clue how to undo the revert and jump back to the newest revision.

    • Alexander
      Alexander over 14 years
      Yes you are right, this works perfect with git command line tools, but i am searching for the corresponding functionality in TortoiseGit which also uses git command line tools in the background.
    • Alexander
      Alexander over 14 years
      Seems to be not possible with the current version of TortoiseGit.
    • sam1132
      sam1132 over 14 years
      I think I figured this out with TortoiseGit, see edited answer.
    • Chris Moschini
      Chris Moschini over 8 years
      This is incorrect. What version of TortoiseGit is this? I don't think "Revert to parent revision" is even present anymore. Revert is for destroying local workspace changes. The question here is asking to roll back to an older version and return to the current version later, not destroy the current version.
    • Chris Moschini
      Chris Moschini over 8 years
      The Revert feature is still doing something different from what the asker was looking for.
    • Justin
      Justin over 7 years
      This is the best answer! the other answers are probably for previous versions of TortoiseGit before they added this functionality.
    • quangkid
      quangkid over 6 years
      I think it will change all project
    • Chris Moschini
      Chris Moschini almost 6 years
      The question asks for either: "revert a single file(or a complete repository) to a previous revision"
    • Yue Lin Ho
      Yue Lin Ho over 4 years
      Yap! Fast way. :)
    • bobobobo
      bobobobo over 2 years
      You can also directly click Revert to this revision to reset only that file to what it looked like in that commit. This doesn't mess with the branch you're checked out on.
    • bobobobo
      bobobobo over 2 years
      This other answer shows how to revert only 1 file to a previous commit