将 git .gitignore 规则应用于现有存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3258638/
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 08:45:42  来源:igfitidea点击:

Apply git .gitignore rules to an existing repository

git

提问by Smarl Teeksoe

I started using git with an xcode project and have recently discovered that I can use .gitignore and .gitattributes files to ignore the noise from the compiler and system. Now that I have the .gitignore and .gitattributes files in place, how can I "apply" the new ignore rules and get rid of the crud from version control?

我开始在一个 xcode 项目中使用 git,最近发现我可以使用 .gitignore 和 .gitattributes 文件来忽略来自编译器和系统的噪音。 既然我已经有了 .gitignore 和 .gitattributes 文件,我该如何“应用”新的忽略规则并从版本控制中摆脱掉渣滓?

My .gitignore file is:

我的 .gitignore 文件是:

# xcode noise
*.modelv3
*.pbxuser
*.perspective
*.perspectivev3
*.pyc
*~.nib/
build/*

# Textmate - if you build your xcode projects with it
*.tm_build_errors

# old skool
.svn

# osx noise
.DS_Store
profile

And my .gitattributes file is:

我的 .gitattributes 文件是:

*.pbxproj -crlf -diff -merge

Thanks!

谢谢!

采纳答案by Neal L

use git rm --cachedfor files and git rm -r --cachedfor the build/directory

使用git rm --cached文件和文件git rm -r --cachedbuild/目录

回答by Chris Johnsen

Here is one way to “untrack” any files that are would otherwise be ignored under the current set of exclude patterns:

这是一种“取消跟踪”在当前排除模式集下会被忽略的任何文件的方法:

(GIT_INDEX_FILE=some-non-existent-file \
 git ls-files --exclude-standard --others --directory --ignored -z) |
xargs -0 git rm --cached -r --ignore-unmatch --

This leaves the files in your working directory but removes them from the index.

这会将文件保留在您的工作目录中,但会将它们从索引中删除。

The trick used here is to provide a non-existent index file to git ls-filesso that it thinks there are no tracked files. The shell code above asks for all the files that would be ignored if the index were empty and then removes them from the actual index with git rm.

这里使用的技巧是向git ls-files提供一个不存在的索引文件,以便它认为没有跟踪文件。如果索引为空,上面的 shell 代码会询问所有将被忽略的文件,然后使用git rm将它们从实际索引中删除。

After the files have been “untracked”, use git statusto verify that nothing important was removed (if so adjust your exclude patterns and use git reset -- pathto restore the removed index entry). Then make a new commit that leaves out the “crud”.

在文件被“取消跟踪”后,使用git status来验证没有删除任何重要的内容(如果是这样,请调整您的排除模式并用于git reset -- path恢复已删除的索引条目)。然后进行一个新的提交,忽略“crud”。

The “crud” will still be in any old commits. You can use git filter-branchto produce clean versions of the old commits if you really need a clean history (n.b. using git filter-branchwill “rewrite history”, so it should not be undertaken lightly if you have any collaborators that have pulled any of your historical commits after the “crud” was firstintroduced).

“crud”仍将在任何旧的提交中。如果你真的需要一个干净的历史,你可以使用git filter-branch来生成旧提交的干净版本(nb 使用git filter-branch将“重写历史”,所以如果你有任何合作者已经拉取了在首次引入“crud”之后您的任何历史提交)。

回答by Sven Koschnicke

If you are already tracking files you want to ignore, you have to remove them with

如果您已经在跟踪要忽略的文件,则必须使用以下命令删除它们

git rm --cached <file>

Git won't ignore files which are already tracked (i.e. you added them with git add ).

Git 不会忽略已跟踪的文件(即您使用 git add 添加它们)。

回答by Abizern

If you want to remove the files completely, then here's a handy reference from Github.

如果你想完全删除文件,那么这里有一个来自 Github方便参考

Be warned that:

请注意:

  1. This will rewrite your history, so it's probably only worth doing before you publish your repo.
  2. Remember that the old shas will still be in the object database, but the guide I've linked to will show you how to deal with those as well.
  1. 这将重写您的历史记录,因此可能只有在您发布回购之前才值得这样做。
  2. 请记住,旧的 shas 仍将在对象数据库中,但我链接到的指南也将向您展示如何处理它们。

回答by Jed Schneider

mv the_file_i_want_to_ignore the_file_i_want_to_ignore.back

mv the_file_i_want_to_ignore the_file_i_want_to_ignore.back

git rm the_file_i_want_to_ignore

git rm the_file_i_want_to_ignore

mv the_file_i_want_to_ignore.back the_file_i_want_to_ignore

mv the_file_i_want_to_ignore.back the_file_i_want_to_ignore

git status

git status

git commit -m 'ignore a bunch of stuff i should have ignored before'

git commit -m 'ignore a bunch of stuff i should have ignored before'

this a bit of a manual workflow but its how i've done this in the past.

这有点手动工作流程,但我过去是这样做的。

回答by Alex Bliskovsky

You shouldn't have to 'apply' the changes. simply put the .gitignore file in your home directory (if you want it to be global) or in your project root (if you want it to be unique only to that project).

您不应该“应用”更改。只需将 .gitignore 文件放在您的主目录(如果您希望它是全局的)或您的项目根目录中(如果您希望它仅对该项目是唯一的)。

Edit:It occurs to me that you might be having this problem because you've added the files you don't want to track already. Here's what the gitignore man page has to say about that:

编辑:我想到您可能遇到此问题,因为您已经添加了不想跟踪的文件。以下是 gitignore 手册页对此的说明:

Note that all the gitignore files really concern only files that are not already tracked by git; in order to ignore uncommitted changes in already tracked files, please refer to the git update-index --assume-unchanged documentation.

请注意,所有 gitignore 文件实际上只涉及 git 尚未跟踪的文件;为了忽略已跟踪文件中未提交的更改,请参阅 git update-index --assume-unchanged 文档。