简体   繁体   English

一个Git命令来检索所有未暂存和未提交的已删除文件

[英]One Git command to retrieve all deleted files not staged and committed

In a git repository, I have deleted a number of tracked files (100+) by mistake. git存储库中,我误删除了许多跟踪文件(超过100个)。

These deletions have not been staged and committed yet. 这些删除尚未上演和提交。

Here is what I see when I do git status . 这是我执行git status时看到的内容。 The number of deleted files is 100+, so I can't retrieve each individually. 删除的文件数超过100,因此我无法分别检索每个文件。

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  deleted:    some_tracked_file1.php
  deleted:    some_tracked_file2.php
  deleted:    some_tracked_file3.php

How can I undo these changes and thus retrieve all the deleted files with a single command? 我如何撤消这些更改,从而用一个命令检索所有已删除的文件?

The answer is right there in the output you quote: 答案就在您引用的输出中:

(use "git checkout -- file..." to discard changes in working directory) (使用“ git checkout-file ...”放弃工作目录中的更改)

This will discard changes including deletions. 这将丢弃包括删除在内的更改。

If these are the only changes listed, and you want to get rid of all of them, then you can do: 如果这些是列出的唯一更改,并且您希望摆脱所有更改,则可以执行以下操作:

git reset --hard

This will put your working directory back into the state it was at your last commit, discarding any local or staged changes. 这将使您的工作目录恢复到上次提交时的状态,并丢弃所有本地或分阶段的更改。

If you have other changes that you don't want to revert, you'll have to use the checkout -- command. 如果您不想恢复其他更改, 必须使用checkout --命令。 You can easily script this though, to restore only deleted but not staged files on *nix: 不过,您可以轻松编写脚本,以仅在* nix上还原已删除但未暂存的文件:

git status --porcelain | grep -E '^ D' | awk '{print $2}' | xargs git checkout --

I don't know what an equivalent for Windows might look like, and you'll have issues if you have file names that contain spaces. 我不知道Windows的等效形式如何,如果文件名包含空格,则会出现问题。

如果git reset --hard不起作用,则可以使用git revert HEAD还原到最后一次提交(如果文件在最后一次提交中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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