windows Git (TortoiseGit) - 如何将单个文件恢复到以前的版本,然后撤消恢复?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1669917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:52:51  来源:igfitidea点击:

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

windowsgitrevisiontortoisegit

提问by Alexander

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

将 Git 与 TortoiseGit 一起使用时:有人知道如何将单个文件(或完整存储库)恢复到以前的修订版?

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.

例如,我有一个包含多个文件的存储库。一个文件存在于三个修订版 (1 ; 2 ; 3) 中。现在我想从修订版 3 改回 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.

TortoiseGit 在“显示日志”对话框中提供了“恢复”功能,允许跳回特定修订,但这将恢复整个存储库而不是单个文件。

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

此外,一旦我恢复了某些内容,我就不知道如何撤消恢复并跳回最新的修订版。

回答by mlibby

From the command line: git checkoutis probably what you want.

从命令行:git checkout可能是您想要的。

The documentation shows an example of:

该文档显示了以下示例:

$ git checkout master~2 Makefile

$ git checkout master~2 Makefile

to revert Makefileto two revisions back in the master branch

Makefile在 master 分支中恢复到两个修订版

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

从 TortoiseGit 中(通过 Windows 资源管理器),您似乎可以通过以下步骤执行此操作:

  • Navigate in Explorer to the folder where the file is.
  • 在资源管理器中导航到文件所在的文件夹。
  • Right-click on the file you want to revert, choose Show logfrom the TortoiseGit context menu
  • 右键单击要还原的文件,Show log从 TortoiseGit 上下文菜单中选择
  • 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
  • 在第三部分(文件列表)中右键单击文件并选择 Revert to this revision
  • You should get a message like 1 files revert to e19a77
  • 你应该收到类似的消息 1 files revert to e19a77
  • 回答by Chris Moschini

    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.

    在 TortoiseGit 中,答案是右键单击项目文件夹并单击 Show Log,然后右键单击要回滚到的更改并单击 Switch/Checkout to this... 。然后它会让你从提交堆栈中那个奇怪的地方开始,或者在你计划提交并希望事情保持理智的情况下分支。

    回答by mxmoss

    It is also possible to get a specific file:

    也可以获取特定文件:

    • Right-click on the file
    • Choose the menu item: Git Show Log
    • 右键单击文件
    • 选择菜单项:Git Show Log

    TortoiseGit show a dialog with a list of all revisions

    TortoiseGit 显示一个包含所有修订列表的对话框

    • Click on the prior revision (2)
    • 点击之前的修订版 (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

    例如: - 原始文件是 file.txt - 修订版 2 将保存为 file-67b51a8.txt

    I'm using TortoiseGit v2.2.0.0

    我正在使用 TortoiseGit v2.2.0.0

    回答by Yue Lin Ho

    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. 签出其先前修订版的一些文件
    2. 测试
    3. 恢复到当前版本


    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 Hardin Log dialog

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

      enter image description here

      enter image description here

    1. 签出其先前修订版的一些文件

      (a) 右键单击​​要还原的文件并显示该文件的日志

      在此处输入图片说明

      (b) 右键单击​​文件列表中的文件并执行Revert to parent revision

      在此处输入图片说明

      在此处输入图片说明

      (c) 重复 (a), (b) 直到您获得所需的所有文件。

      在此处输入图片说明

    2. 测试

    3. 恢复到当前版本

      (a)在资源管理器的上下文菜单中执行Revert...

      这样,您可以选择要还原的文件。

      (b) 或这种快速方法:在日志对话框中执行重置硬

      这样,所有更改的文件都会恢复。(=> 丢失了所有工作目录更改)

      在此处输入图片说明

      在此处输入图片说明

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

    (在 TortoiseGit 1.8.16.1、GfW 2.6.4.windows.1、Win 10 64 位上测试)

    回答by Ovsyanka

    For Tortoise Version 2.3.0:

    对于乌龟 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)
    1. 右键单击文件
    2. 单击菜单选项“与以前的版本不同”
    3. 右键单击文件的左侧(上一个)版本
    4. 单击菜单选项“使用整个文件”
    5. 保存更改 (ctrl+s)