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

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

Applying .gitignore to committed files

git

提问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 .gitignoreto match the ignored files, you can do git ls-files -ci --exclude-standardto 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 -rflag in xargsprevents git rmfrom running on an empty result and printing out its usage message, but may only be supported by GNU findutils. Other versions of xargsmay or may not have a similar option.)

(该-r标志xargs防止git rm在空结果上运行并打印出其使用消息,但可能仅受 GNU findutils 支持。其他版本xargs可能有也可能没有类似的选项。)

Now you can just type git apply-gitignorein your repo, and it'll do the work for you!

现在您只需输入git apply-gitignore您的 repo,它就会为您完成工作!

回答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

确保您的实际存储库是最新版本

  1. Edit .gitignoreas you wish
  2. git rm -r --cached .
  3. git add .
  1. .gitignore随心所欲编辑
  2. git rm -r --cached .
  3. 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:

按着这些次序:

  1. Add path to gitignorefile

  2. Run this command

    git rm -r --cached foldername
    
  3. commit changes as usually.

  1. 添加gitignore文件路径

  2. 运行这个命令

    git rm -r --cached foldername
    
  3. 像往常一样提交更改。