.gitignore is not working

2018-12-31 06:56发布

My .gitignore file seems to be being ignored by git - could the .gitignore file be corrupt? Which file format, locale or culture does git expect?

My .gitignore:

#this is a comment
debug.log
nbproject/

Output from git status:

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       debug.log
#       nbproject/
nothing added to commit but untracked files present (use "git add" to track)

I would like debug.log and nbproject/ not to appear in the untracked files list.

Where should I start looking to fix this?

标签: git gitignore
26条回答
大哥的爱人
2楼-- · 2018-12-31 07:14

Ok, so in my case the accepted solution did not work, and what worked is described here:

https://ericnelson.wordpress.com/2014/06/21/is-visual-studio-2013-ignoring-your-gitignore-file/

In short:

  • Close Visual Studio.
  • Navigate to your .git folder
  • Delete ms-persist.xml
  • Restart Visual Studio

Hope this helps somebody sometime

查看更多
永恒的永恒
3楼-- · 2018-12-31 07:15

In my case, it's because the files already exist in the repo and I'm trying to ignore it.

These are the things I did to fix the issue:

  • Copy the files to a temp folder
  • Remove them from my project folder.
  • Commit the changes which remove those files from the repo
  • Re-added those files to my project folder

By then, any changes I made on those files were ignored.

I think you can't ignore files that already exist on the repo.

查看更多
ら面具成の殇う
4楼-- · 2018-12-31 07:16

Another cause of this issue is blank spaces or tabs before the statement:

Example:

#Be aware of following:
 notWorkingIgnore.*
workingIgnore.*

And as pointed out by the comment bellow a trailing space can be an issue aswell

#Be aware of following:
notWorkingIgnore.* #<-Space
workingIgnore.*#<-Nospace
查看更多
回忆,回不去的记忆
5楼-- · 2018-12-31 07:16

Also check out the directory, where you put .gitignore It should be in root of your project:

./myproject/.gitignore

not in

./myproject/.git/.gitignore
查看更多
还给你的自由
6楼-- · 2018-12-31 07:18

In my case, the .gitignore wasn't working because I added a comment on the line containing the file to ignore:

/foo/bar/c.png # this image should be ignored

Placing the comment on a separate line thus fixed it:

# this image should be ignored:
/foo/bar/c.png
查看更多
唯独是你
7楼-- · 2018-12-31 07:19

As with the other solutions, commit first and be aware that you WILL lose any un-committed changes.

I had better results with this:

git rm -r --cached .
git reset HEAD --hard
git status

note that the status should have no modified files now.

查看更多
登录 后发表回答