如何使用 Git GUI 忽略文件?

发布于 2024-08-22 13:52:12 字数 261 浏览 8 评论 0 原文

我在 .git 文件夹的文件夹中创建了一个 .gitignore 文件, 但在重新扫描存储库时我仍然收到我试图忽略的文件。

这是文件的内容。

# Ignored files
*.suo 
*.user 
bin 
obj 
*.pdb 
*.cache 
*_svn 
*.svn 
*.suo 
*.user 
*.build-res 
TestResults 
_ReSharper*

我做错了什么?该文件应该位于哪里?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

—━☆沉默づ 2024-08-29 13:52:12

您需要确保您仍然看到的文件当前尚未提交或暂存。

如果您删除它们(git rmgit rm --cached),然后将它们添加为私有文件,那么它们将被忽略。

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.

禾厶谷欠 2024-08-29 13:52:12

你的文件看起来不错。只要确保每行开头没有任何空格,我花了 4 个小时才找到我的 .gitignore 无法正常工作的原因;原因当然是剪切和粘贴问题,我在文件开头有一条垂直的空格线。

在确保您的 .gitignore 文件确实清除了您不想要的所有内容之前,您应该小心使用 git commit -a 。使用 git add *.cpp *.h 标记每个文件(或使用通配符),或者 - 就像我喜欢的那样 - 随着进度开发您的 .gitignore,并始终检查 git status提交之前的代码>。

如果您想仔细检查 .gitignore 是否确实有效,尝试

git ls-files --others -i --exclude-standard

这应该列出您当前忽略的所有文件。

要清除已经添加的文件(可能使用 git add .,这是我们一开始就犯的错误 =]),您可以像 @VonC 说:
要么运行 要么

git rm <filename>

清理

git rm --cached <filename>

您无意中添加的所有文件的另一个选项是完全清理您的存储库,然后再次重新添加所有内容。如果你想清除暂存区中的所有内容,你可以运行

git --rm cached .

但记住不要运行git add . 直到您确保 git status 仅列出存储库中您真正想要的文件。

git 的另一个非常有用的事情是,使用通配符时它不需要文件路径。如果您列出了忽略的文件并发现您想要删除所有 *.suo*.log 文件,只需运行

git rm --cached *.suo *.log

git 就会找到您的文件中的所有文件。与该签名匹配的存储库,无论它们位于树中的哪个位置。

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.

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.

If you want to double-check that your .gitignore really works, try

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 @VonC said:
either run

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.

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 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.

东风软 2024-08-29 13:52:12

我尝试了多种方法来使其发挥作用,最终非常简单:

您必须首先提交 .gitignore!

此外,如果您使用 TortoiseGIT - GitEx 提交工具,还有一个选项可以编辑忽略的文件。

I tried multiple ways to get this to work and it was finally pretty simple:

You must commit the .gitignore first!

Also, if you use TortoiseGIT - GitEx Commit Tool, there is an option to edit ignored files.

心碎的声音 2024-08-29 13:52:12

您的位置是正确的。 .gitignore 应该位于 .git 文件夹所在的同一文件夹中。里面的文件看起来也正确。但是,我在顶部没有评论行..

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..

云归处 2024-08-29 13:52:12

当我看到这个时,我认为您忘记提交忽略文件。

When I look at this, I would think you forgot to commit your ignore file.

会傲 2024-08-29 13:52:12

--rm 缓存解决方案都不适合我。该文件需要编码为 ASCII/ANSI。我也更改了行结尾,但仅此并没有解决问题。我找不到包含这些说明的网址(对不起,原始海报!)但这对我有用:

  1. 如果您还没有 Notepad++,请安装它。文本板应该
    也可以,但我使用 Notepad++。
  2. 使用 Notepad++ 打开 .gitignore。
  3. 单击查看 |显示符号 |显示所有字符。您可能会在每行末尾看到 CR LF。
  4. 进行搜索并替换(ctrl+h),确保选中“扩展”(左下角)。搜索 \r\n 并替换为 \n。您将看到所有 CR LF 都更改为 LF。
  5. 现在单击编码 |以 ANSI 编码。
  6. 保存文件并退出。

从 Windows 中的命令提示符或 git 命令提示符(无论您喜欢哪种),当您执行 git status 时,它不应显示 .gitignore 中的文件。

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:

  1. Install Notepad++ if you don't have it already. Textpad should
    also work, but I use Notepad++.
  2. Open your .gitignore with Notepad++.
  3. Click on View | Show Symbol | Show All Characters. You will probably see CR LF at the end of each line.
  4. 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.
  5. Now click on Encoding | Encode in ANSI.
  6. Save the file and exit.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文