简体   繁体   English

使用 git rm 命令删除 .gitignore 中列出的文件

[英]Files listed in .gitignore are removed with git rm command

I have setup a .gitignore file with just one file in it:我已经设置了一个.gitignore文件,其中只有一个文件:

 cat .gitignore
 README.md

The .gitignore file has these permissions: .gitignore文件具有以下权限:

ls -l README.md
-rw-r--r-- 1 tdun0002 1049089 113 Mar 21 18:54 README.md

I'm not sure what the problem is but when I do a git rm --force * everything is removed including the README.md file listed in the .gitignore file.我不确定问题是什么,但是当我执行git rm --force *所有内容都被删除,包括.gitignore文件中列出的README.md文件。

What am I doing wrong?我究竟做错了什么?

The .gitignore file is a way of specifying which untracked files will remain untracked even if you try to add them (and it's generally a good idea that any changes to it should be fully committed before attempting operations that use those changes). .gitignore文件是一种指定哪些未跟踪文件将保持未跟踪状态的方法,即使您尝试添加它们(并且在尝试使用这些更改的操作之前,应该完全提交对其的任何更改通常是一个好主意)。 It is not a way to tell general git commands to not do stuff with a file.这不是告诉一般git命令不要对文件做任何事情的方法。

In particular, if a file is already in the repo when you add it to the .gitignore , this will have no effect on it.特别是,如果在将文件添加到.gitignore时文件已经在 repo 中,则不会对其产生任何影响。

Since your README.md was already in the repo when you added it to .gitignore , it continued to be a tracked file.由于您的README.md在您将其添加到.gitignore时已经在存储库中,因此它仍然是一个跟踪文件。


Though that might not solve your actual issue: I'm having a hard time figuring out what your intent is, since this appears to be an XY problem - you're asking why the thing you tried didn't work, when we don't actually know what you were trying to do.虽然这可能无法解决您的实际问题:我很难弄清楚您的意图是什么,因为这似乎是一个 XY 问题-您在问为什么您尝试的方法不起作用,而我们却没有”实际上不知道您要做什么。 The two possibilities that come to mind are discussed below:下面讨论了想到的两种可能性:

  1. If your intent is to not have README.md in the repo, just git rm it, then commit/push/etc.如果您的意图是在 repo 中没有README.md ,只需git rm它,然后提交/推送/等。

  2. If you do want it in the repo, it shouldn't be listed in the .gitignore file.如果您确实希望在 repo 中使用它,则不应将列在.gitignore文件中。

  3. If you want to get rid of everything except a few files (and they're clean, in the sense that there are no uncommitted changes), you might find it easier to delete the lot then just check out the ones you want to keep.如果您想删除除少数文件之外的所有内容(并且它们是干净的,因为没有未提交的更改),您可能会发现删除这些文件更容易,然后只需查看您想要保留的文件即可。 Something like git rm . ; git checkout HEAD -- README.md类似git rm . ; git checkout HEAD -- README.md东西git rm . ; git checkout HEAD -- README.md git rm . ; git checkout HEAD -- README.md git rm . ; git checkout HEAD -- README.md . git rm . ; git checkout HEAD -- README.md

  4. If you only want to remove specific files, you can use wildcards to limit them.如果您只想删除特定文件,您可以使用通配符来限制它们。 For example, git rm --dry-run [0-9a-zA-QS-Z]* will not touch any files starting with R .例如, git rm --dry-run [0-9a-zA-QS-Z]*不会触及任何以R开头的文件。 You'll notice I've used --dry-run here since you'll probably want to check this option before allowing it to actually delete.你会注意到我在这里使用了--dry-run因为你可能想在允许它实际删除之前检查这个选项。 Once you're happy it will do the right thing, just run it again without that option.一旦您感到满意,它就会做正确的事情,只需在没有该选项的情况下再次运行它。

If it's something else you're trying to achieve, it'd be handy if you could expand on that.如果这是您想要实现的其他目标,如果您可以扩展它会很方便。

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

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