.gitignore在新的存储库中无法工作

3

我有一个全新的git仓库,刚刚检出。然后我更新了我的.gitignore文件,添加了以下内容:

wp-config.php

现在我的git status显示如下:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

我添加并提交了这个文件(本地)。然后我更新wp-config.php文件。

但是,现在,当我运行git status时,我得到以下结果:

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   wp-config.php

我已经阅读了这个问题:.gitignore is not working,但是其中的观点似乎并不适用于我正在使用的全新版本库。是否还有其他建议?我肯定不必将其提交到远程('origin/master')以在本地忽略它吧?

你是否碰巧强制添加了文件(使用 git add -f wp-config.php),因为这将覆盖 .gitignore - Willem Van Onsem
1
这是关于gitignore的最常见问题之一,出现在Stack Overflow上。Git不会忽略已经被跟踪的文件;你需要先停止跟踪它们。 - jub0bs
2个回答

4

看起来git已经在跟踪那个文件,所以.gitignore无法生效。

你可以使用以下命令停止git跟踪该文件:

git rm --cached wp-config.php

之后,您不应在git status输出中看到wp-config.php


1
如果文件已经被git跟踪,你需要在将其添加到.gitignore后将其删除。这将防止它被进一步跟踪:
git rm --cached wp-config.php

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接