Apply git .gitignore rules to an existing repository
This question already has an answer here:
使用git rm --cached
获取文件,使用git rm -r --cached
获取build/
目录
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-files so 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.
After the files have been “untracked”, use git status to verify that nothing important was removed (if so adjust your exclude patterns and use git reset -- path
to restore the removed index entry). Then make a new commit that leaves out the “crud”.
The “crud” will still be in any old commits. You can use git filter-branch to produce clean versions of the old commits if you really need a clean history (nb using git filter-branch will “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 first introduced).
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 (ie you added them with git add ).
链接地址: http://www.djcxy.com/p/23392.html上一篇: .gitignore不起作用