简体   繁体   English

通过先前的提交再次删除文件,该提交首先删除了该文件

[英]Delete again a file via a previous commit that deleted that file in the first place

Scenario:设想:

  • delete file.txt file, commitA and push删除file.txt文件,提交A并推送
  • restore file.txt via git checkout commitA~1 file.txt , commitB and push通过git checkout commitA~1 file.txt , commitB 和 push 恢复file.txt

I want to delete the file again by invoking commitA by running the following checkout command:我想通过运行以下 checkout 命令调用commitA再次删除文件:

git checkout commitA --file.txt

but I get但我明白了

error: pathspec 'file.txt' did not match any file(s) known to git

Is there a way to re-delete the file?有没有办法重新删除文件? I could just delete the file add it and commit but I am wondering if I can keep the history clean by reusing an older commit ?!我可以删除文件添加它并提交,但我想知道是否可以通过重用旧提交来保持历史记录干净?!

Edit: I removed the Source from the file path to avoid confusions编辑:我从文件路径中删除了 Source 以避免混淆

I want to [use git checkout to] delete the file again by invoking [the commit in which I deleted it] ...我想 [使用git checkout ] 通过调用 [我删除它的提交]再次删除文件 ...

You cannot do that.你不能这样做。 This form of git checkout (or git restore for that matter) can only add or replace the file, not remove it.这种形式的git checkout (或git restore )只能添加或替换文件,不能删除它。

If you know that the file was removed in some specific commit and would like to remove it again, use git rm (or git rm --cached if the idea includes avoiding modifying the working tree).如果您知道该文件在某个特定提交中被删除并且想再次删除它,请使用git rm (或git rm --cached如果想法包括避免修改工作树)。

If you don't know whether the file existed or not, and simply want to put it back into the same state—that is, restore it to the way it was if it existed , or remove it entirely if it did not exist , you will need an if/then test: "Did file F exist in commit C? If yes, use git checkout / git restore ; if no, use git rm ".如果您不知道文件是否存在,只是想将其恢复到相同的状态,即如果它存在则将其恢复到原来的状态,或者如果它不存在则将其完全删除,您将需要一个if/then测试:“提交 C 中是否存在文件F 如果是,请使用git checkout / git restore ;如果否,请使用git rm ”。 To test whether it existed, consider using git ls-tree (useful if you want to check many paths) or git rev-parse (useful if you want to check a single path).要测试它是否存在,请考虑使用git ls-tree (如果你想检查许多路径很有用)或git rev-parse (如果你想检查单个路径很有用)。 You could even use the error from git checkout as an indicator that the file did not exist, although that seems a bit clumsy and perhaps error-prone.您甚至可以使用来自git checkout的错误作为文件不存在的指示,尽管这看起来有点笨拙并且可能容易出错。

because your command is wrong it should go like this git restore --Source HEAD~1 file.txt note that your HEAD will be detached因为你的命令是错误的,它应该像这样git restore --Source HEAD~1 file.txt注意你的 HEAD 将被分离

and if what you want to correct is on-stage use this git restore --staged file.txt to unstage it.如果您要更正的是在舞台上使用这个git restore --staged file.txt来取消它。

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

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