git .gitignore 不工作:应该被忽略的文件仍然被提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2900816/
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
.gitignore not working: files that should be ignored still get committed
提问by John Grey
I'm trying to create a new git repository from existing folder. I've created a .gitignore
file in the root of the folder. But if I say
我正在尝试从现有文件夹创建一个新的 git 存储库。我在.gitignore
文件夹的根目录中创建了一个文件。但如果我说
git add *
git commit
git push
files that should be ignored still get committed to the remote repository. I'm on Windows.
Also I've bought a license for SmartGIT. It also seems to ignore .gitignore
. I have to manually select which new files to commit.
应该忽略的文件仍然提交到远程存储库。我在 Windows 上。我还购买了 SmartGIT 的许可证。它似乎也无视了.gitignore
。我必须手动选择要提交的新文件。
回答by Jakub Nar?bski
Try "git add .
" instead.
试试“ git add .
”。
Also, it works for me (on Linux):
此外,它对我有用(在 Linux 上):
$ git init $ echo foo > .gitignore $ echo foo > foo $ echo bar > bar $ git add -v * The following paths are ignored by one of your .gitignore files: foo Use -f if you really want to add them. fatal: no files added
回答by Shadowbob
Your file must still be tracked, you can see by doing git status
that will show you that your file is modified even if it's in .gitignore
You need to do this:
你的文件仍然必须被跟踪,你可以看到这样做git status
会告诉你你的文件被修改了,即使它在 .gitignore
你需要这样做:
git update-index --assume-unchanged [path_to_file]
回答by Randal Schwartz
Are your files already tracked? .gitignore only silences comments about untracked files, but won't stop a tracked file from being tracked.
您的文件是否已被跟踪?.gitignore 只会使有关未跟踪文件的评论静音,但不会停止跟踪已跟踪文件。
回答by Christian Delahousse
I've had issues with .gitignore also. I checked out the linked answers listed about, which fixed half the issue.
我也有 .gitignore 的问题。我查看了列出的链接答案,解决了一半的问题。
What really got gitignore working full for me was adding a comment on the first line of the file. Git wasn't parsing the exclude situated on the first line.
真正让 gitignore 对我来说有效的是在文件的第一行添加评论。Git 没有解析位于第一行的 exclude。
Cheers
干杯
回答by Valentin Jacquemin
A trick I faced on windows is that using echo
(as per Jakub Nar?bski answer) you have to be careful with spaces.
我在 Windows 上遇到的一个技巧是,使用echo
(根据 Jakub Nar?bski 的回答)你必须小心空格。
As you can see below any space before the redirection operator does have an effect on the actual ignore.
正如您在重定向运算符确实对实际忽略有影响之前的任何空格所见。
C:\test>dir /B
TOBEIGNORED
C:\test>echo TOBEIGNORED > .gitignore
C:\test>git status
[...]
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# TOBEIGNORED
C:\test>echo TOBEIGNORED> .gitignore
C:\test>git status
[...]
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
nothing added to commit but untracked files present (use "git add" to track)
回答by Shen Wang
You could have created a UTF-8 encoded text file. Try saving it as ANSI encoded. In git bash, you can verify by using vi -b.
您可以创建一个 UTF-8 编码的文本文件。尝试将其保存为 ANSI 编码。在 git bash 中,您可以使用 vi -b 进行验证。
回答by Scott A
Comment line as the first line of the file is critical! I spent considerable time trying to exclude files only to find that GIT was ignoring the first line in the ignore file.
注释行作为文件的第一行很关键!我花了大量时间试图排除文件,却发现 GIT 忽略了忽略文件中的第一行。
回答by jaguarondi
I'm using git version 1.7.12.3 on MacOSX and the first line of .gitignore is also not taken into account. Just add a comment as first line.
我在 MacOSX 上使用 git 版本 1.7.12.3,也没有考虑 .gitignore 的第一行。只需添加注释作为第一行。
回答by takeshin
Probably your exclude file mask is inacurate.
可能您的排除文件掩码不准确。