git将文件列为未跟踪时应忽略它们。 - 为什么?

时间:2014-05-05 10:06:07

标签: git

我有这个结构:

thumb/.gitignore

.gitignore里面我有:

#ignore everything
*
#except gitignore
!.gitignore

我在/ thumb文件夹中添加了一个文件。 它看起来没那么好!

我有:

git rm -r --cached .
git add .
git commit -a -m "gitignore SHOULD work"

重复这个过程,git仍然会考虑该文件!

我没有得到什么?它可能与其他"鞋帮" gitignore指令,我的意思是,我使用这很多因为git不理解文件夹......(所以我被告知)。

请建议。

更新

以下是所要求的详细命令:

1 - git ls-files

git ls(git ls是一个未找到的命令。我做过" git ls-files"我希望它是一样的。我不知道它们中的任何一个。 )

$ git ls-files 
.DS_Store 
.gitignore 
test 
test2 
thumb_5363d384b0cff.gif

2- git status。

$ git status . 
# On branch dev 
# Your branch is ahead of 'hub/dev' by 1 commit. 
# (use "git push" to publish your local commits) 
# 
nothing to commit, working directory clean 

3- git rm -r --cached。

$ git rm -r --cached . 
rm 'public_html/assets/images/cropkimages/thumb/.DS_Store' 
rm 'public_html/assets/images/cropkimages/thumb/.gitignore' 
rm 'public_html/assets/images/cropkimages/thumb/test' 
rm 'public_html/assets/images/cropkimages/thumb/test2' 
rm 'public_html/assets/images/cropkimages/thumb/thumb_5363d384b0cff.gif' 

4- git status。

$ git status . 
# On branch dev 
# Your branch is ahead of 'hub/dev' by 1 commit. 
# (use "git push" to publish your local commits) 
# 
# Changes to be committed: 
# (use "git reset HEAD <file>..." to unstage) 
# 
# deleted: .DS_Store 
# deleted: .gitignore 
# deleted: test 
# deleted: test2 
# deleted: thumb_5363d384b0cff.gif 
#
# 
# Untracked files: 
# (use "git add <file>..." to include in what will be committed) 
# 
# .DS_Store 
# .gitignore 
# test 
# test2 
# thumb_5363d384b0cff.gif

3 个答案:

答案 0 :(得分:2)

.gitignore规则不适用于已经跟踪的文件。

如果你的thumb/里面的文件已经在git中版本化了,而你想摆脱它们,你应该:

# assuming you are inside thumb/ :
git rm -r --cached .
# check if gitignore is still tracked, maybe you will have to run :
git add .gitignore
# do not try to add the other files, just commit the new result :
git commit -m 'Removed wrongly tracked files'

然后 .gitignore规则应该适用。


请进入thumb/目录,然后粘贴以下命令集的输出:

git ls
git status .
git rm -r --cached .
git status .

答案 1 :(得分:1)

在LeGEC回答之后,我已经输入了命令,并且我注意到列出的文件不应该被标记为未跟踪。

偶然的机会,我找到了这篇文章: https://stackoverflow.com/a/9115498/378170

我这样做了:

1)从所有.gitignore文件内容中删除了所有额外空格;

2)再次重新执行正确的命令。

一切正常。 所以它与额外的空间有关。

答案 2 :(得分:0)

您可能在尝试删除其他文件之前提交了.gitignore文件。因此,当您执行git rm -r --cached时,您还会删除.gitignore文件。

要保留.gitignore文件但删除其他文件,您需要做的是:

# Assuming you are in thumb/
git rm -r --cached .
git reset .gitignore
git add .
git commit -m "Remove wrongly tracked files"

现在它应该可以工作了。

相关问题