如何使用 Git GUI 忽略文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2318988/
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
How do I .ignore files with Git GUI?
提问by Darkmage
I have created a .gitignore file in the folder with the .git folder, but I still get the files I'm trying to ignore when doing a rescan of the repository.
我在带有 .git 文件夹的文件夹中创建了一个 .gitignore 文件,但是在重新扫描存储库时我仍然得到我试图忽略的文件。
This is the content of the file.
这是文件的内容。
# Ignored files
*.suo
*.user
bin
obj
*.pdb
*.cache
*_svn
*.svn
*.suo
*.user
*.build-res
TestResults
_ReSharper*
What am I doing wrong? where is the file suppose to be located?
我究竟做错了什么?文件应该位于哪里?
采纳答案by VonC
You need to make sure the file you are still seeing are not currently committed or staged.
您需要确保您仍然看到的文件当前未提交或暂存。
If you remove them (git rm
or git rm --cached
), and then add them as private file, then they will be ignored.
如果您删除它们(git rm
或git rm --cached
),然后将它们添加为私有文件,则它们将被忽略。
回答by AzP
Your file looks good. Just make sure you don't have any kind of whitespace at the beginning of each line, it took me 4 hours to find why my .gitignore wasn't working correctly; the reason was of course a cut'n'paste issue where I had a vertical line of spaces at the beginning of the file.
你的文件看起来不错。只要确保每行开头没有任何类型的空格,我花了 4 个小时才找到为什么我的 .gitignore 无法正常工作;原因当然是一个cut'n'paste问题,我在文件开头有一行垂直的空格。
You should be careful with git commit -a
before making sure that your .gitignore file really cleans out everything you don't want. Either mark each file (or use wildcards) with git add *.cpp *.h
or - like I prefer - develop your .gitignore as you progress, and always check with git status
before commiting.
git commit -a
在确保您的 .gitignore 文件确实清除了您不想要的所有内容之前,您应该小心。要么用git add *.cpp *.h
or标记每个文件(或使用通配符)——就像我喜欢的那样——随着你的进展开发你的 .gitignore,并git status
在提交之前始终检查。
If you want to double-check that your .gitignore really works, try
如果您想仔细检查您的 .gitignore 是否真的有效,请尝试
git ls-files --others -i --exclude-standard
This should list all the files that you are currently ignoring.
这应该列出您当前忽略的所有文件。
To clean out the files you've already added (probably using git add .
, a mistake we all make in the beginning =]) you can do like @VonCsaid:
either run
要清除您已经添加的文件(可能使用git add .
,我们在开始时都会犯的一个错误 =]),您可以像@VonC所说的那样:要么运行
git rm <filename>
or
或者
git rm --cached <filename>
Another option of cleaning out all the files you've unintentionally added is to totally clean your repository, and then re-add everything again. If you want to clear out everything in the staging area, you can run
清除您无意中添加的所有文件的另一种选择是完全清除您的存储库,然后再次重新添加所有内容。如果要清除暂存区中的所有内容,可以运行
git --rm cached .
But remember to not run git add .
Until you've made sure that git status only lists the files you really want in the repository.
但是请记住不要运行,git add .
直到您确定 git status 仅列出存储库中您真正想要的文件。
Another very useful thing with git is that it doesn't need paths to files when using wildcards. If you list your ignored files and see that you want to remove all *.suo
and *.log
files, just run
git 的另一个非常有用的地方是它在使用通配符时不需要文件路径。如果你列出你忽略的文件,看看您要删除所有*.suo
与*.log
文件,只需要运行
git rm --cached *.suo *.log
and git takes care of finding all the files in your repository that matches that signature, no matter where in the tree they are.
git 负责查找存储库中与该签名匹配的所有文件,无论它们在树中的哪个位置。
回答by Dave
I tried multiple ways to get this to work and it was finally pretty simple:
我尝试了多种方法来让它工作,最后它非常简单:
You must commit the .gitignore first!
你必须先提交 .gitignore !
Also, if you use TortoiseGIT - GitEx Commit Tool, there is an option to edit ignored files.
此外,如果您使用 TortoiseGIT - GitEx 提交工具,则可以选择编辑被忽略的文件。
回答by Stewie Griffin
You are locating it right. .gitignore should be at the same folder, where is .git folder located. File inside also looks correct. However, I dont have a comment line at the top..
你定位对了。.gitignore 应该在同一个文件夹中,.git 文件夹所在的位置。里面的文件看起来也正确。但是,我在顶部没有评论行..
回答by Zane
When I look at this, I would think you forgot to commit your ignore file.
当我看到这个时,我会认为你忘记提交你的忽略文件。
回答by Bob Ramsey
None of the --rm cached solutions worked for me. The file needs to be encoded as ASCII/ANSI. I changed the line endings as well, but that alone didn't fix it. I can't find the url that had these instructions(sorry, original poster!) but this worked for me:
没有一个 --rm 缓存解决方案对我有用。该文件需要编码为 ASCII/ANSI。我也更改了行尾,但仅此而已并不能解决问题。我找不到包含这些说明的网址(对不起,原始海报!)但这对我有用:
- Install Notepad++ if you don't have it already. Textpad should also work, but I use Notepad++.
- Open your .gitignore with Notepad++.
- Click on View | Show Symbol | Show All Characters. You will probably see CR LF at the end of each line.
- Do a search and replace(ctrl+h), making sure that Extended is checked(lower let hand corner). Search for \r\n and replace with \n. You'll see all the CR LF change to just LF.
- Now click on Encoding | Encode in ANSI.
- Save the file and exit.
- 如果您还没有 Notepad++,请安装它。Textpad 也应该可以工作,但我使用 Notepad++。
- 用 Notepad++ 打开你的 .gitignore。
- 点击查看 | 显示符号 | 显示所有字符。您可能会在每行末尾看到 CR LF。
- 进行搜索和替换(ctrl+h),确保选中扩展(左下角)。搜索 \r\n 并替换为 \n。您将看到所有 CR LF 更改为仅 LF。
- 现在点击编码 | 以 ANSI 编码。
- 保存文件并退出。
From the command prompt in windows or the git command prompt, whichever you like, when you do git status it should not show files in .gitignore.
从 Windows 中的命令提示符或 git 命令提示符,无论您喜欢哪个,当您执行 git status 时,它不应显示 .gitignore 中的文件。