git 将 .gitignore 应用于提交的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7527982/
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
Applying .gitignore to committed files
提问by user880954
I have committed loads of files that I now want to ignore. How can I tell git to now ignore these files from future commits?
我已经提交了我现在想忽略的大量文件。我如何告诉 git 现在忽略将来提交中的这些文件?
EDIT: I do want to remove them from the repository too. They are files created after ever build or for user-specific tooling support.
编辑:我也想从存储库中删除它们。它们是在构建之后创建或用于用户特定工具支持的文件。
回答by Jonathan Callen
After editing .gitignore
to match the ignored files, you can do git ls-files -ci --exclude-standard
to see the files that are included in the exclude lists; you can then do
编辑.gitignore
以匹配忽略的文件后,您可以git ls-files -ci --exclude-standard
查看排除列表中包含的文件;然后你可以做
//On Linux:
git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached
//On Windows:
for /F "tokens=*" %a in ('git ls-files -ci --exclude-standard') do @git rm --cached "%a"
//On mac
alias apply-gitignore="git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached"
to remove them from the repository (without deleting them from disk).
从存储库中删除它们(而不从磁盘中删除它们)。
Edit: You can also add this as an alias in your .gitconfig file so you can run it anytime you like. Just add the following line under the [alias] section (modify as needed for Windows or Mac):
编辑:您还可以将其添加为 .gitconfig 文件中的别名,以便您可以随时运行它。只需在 [alias] 部分下添加以下行(根据 Windows 或 Mac 的需要进行修改):
apply-gitignore = !git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached
(The -r
flag in xargs
prevents git rm
from running on an empty result and printing out its usage message, but may only be supported by GNU findutils. Other versions of xargs
may or may not have a similar option.)
(该-r
标志xargs
防止git rm
在空结果上运行并打印出其使用消息,但可能仅受 GNU findutils 支持。其他版本xargs
可能有也可能没有类似的选项。)
Now you can just type git apply-gitignore
in your repo, and it'll do the work for you!
现在您只需输入git apply-gitignore
您的 repo,它就会为您完成工作!
回答by Matt Ball
- Edit
.gitignore
to match the file you want to ignore git rm --cached /path/to/file
- 编辑
.gitignore
以匹配您要忽略的文件 git rm --cached /path/to/file
See also:
也可以看看:
回答by ben.tiberius.avery
to leave the file in the repo but ignore future changes to it:
将文件保留在 repo 中,但忽略未来对其的更改:
git update-index --assume-unchanged <file>
and to undo this:
并撤消此操作:
git update-index --no-assume-unchanged <file>
to find out which files have been set this way:
找出以这种方式设置的文件:
git ls-files -v|grep '^h'
credit for the original answer to http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/
归功于http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/的原始答案
回答by Théo Attali
Be sure that your actual repo is the lastest version
确保您的实际存储库是最新版本
- Edit
.gitignore
as you wish git rm -r --cached .
git add .
.gitignore
随心所欲编辑git rm -r --cached .
git add .
then commit as usual
然后照常提交
回答by Chris Pfohl
Old question, but some of us are in git-posh
(powershell). This is the solution for that:
老问题,但我们中的一些人在git-posh
(powershell)中。这是解决方案:
git ls-files -ci --exclude-standard | foreach { git rm --cached $_ }
回答by Kalyan Chakravarthi V
Follow these steps:
按着这些次序:
Add path to
gitignore
fileRun this command
git rm -r --cached foldername
commit changes as usually.
添加
gitignore
文件路径运行这个命令
git rm -r --cached foldername
像往常一样提交更改。