如何使用git忽略对跟踪文件的新更改?

时间:2014-05-15 08:38:11

标签: git gitignore ignore

当我运行git status时:

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   .gitignore
#       modified:   .project
#       modified:   reports/images/2014-03.png
#       modified:   reports/images/graph-2014-03.bmp
#       deleted:    reports/phpbmp/cache/0/02/027/0270/phpThumb_cache_portal.gep.co.za_src02707010e1e50bc80594
f31460d514c4_par80d8993b181666aca11d7be02b12fea7_dat1399293161.bmp
#       deleted:    reports/phpbmp/cache/0/02/027/0270/phpThumb_cache_portal.gep.co.za_src02707010e1e50bc80594
f31460d514c4_par80d8993b181666aca11d7be02b12fea7_dat1399293182.bmp
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       README.MD
#       reports/phpbmp/cache/9/
no changes added to commit (use "git add" and/or "git commit -a")

我意识到我不想跟踪以下文件的更改:

.project
reports/images/*.png
reports/images/*.bmp
reports/phpbmp/cache/*

所以我将这些文件添加到.gitignore

如何从Changes not staged for commit下的当前工作目录中删除文件?

2 个答案:

答案 0 :(得分:25)

如果我理解得好,你希望filename在git存储库中。但是,您不希望收到有关filename的新更改的通知。您可以使用以下命令执行此操作:

git update-index --assume-unchanged <filename>

如果您想再次开始跟踪更改,请运行以下命令:

git update-index --no-assume-unchanged <filename>

其他信息:编辑.gitignore只会忽略文件,因此不会将它们添加到git存储库中。但是,已经跟踪的文件不会被忽略。

答案 1 :(得分:1)

已跟踪文件,现在您要忽略这些文件。

git rm --cached filename 
echo "filename" >> .gitignore
git add .gitignore
git commit -m 'Removes filename file and adds it to gitignore.'