我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。


当前回答

在我的案例中,我在几个目录中有几个.lock 文件,我需要删除。

git rm -r --cached **/*.lock

这样做,在我所在的“根”下进入每个文件夹,并排除所有符合模式的文件。

其他回答

如果您创建了 gitignore 文件,使用命令如 echo node_modules >>.gitignore,它不会工作。

窗口终端存储文件在 UCS-2 LE BOM 和 git 似乎不接受它。

您可以通过在笔记本中打开并使用 UTF-8 编码重新存储。

此分類上一篇

它现在工作。

我认为他们需要纠正这一点,因为做echo“filetoignore” >>.gitignore实际上看起来非常有用

在我的案例中,我在几个目录中有几个.lock 文件,我需要删除。

git rm -r --cached **/*.lock

这样做,在我所在的“根”下进入每个文件夹,并排除所有符合模式的文件。

git update-index 为我做工作:

git update-index --assume-unchanged <file>

注意: 这个解决方案实际上是独立的.gitignore 因为 gitignore 仅适用于未追踪的文件。


更新,更好的选择

因为这个答案被发布,一个新的选项已经创建,应该是优先的. 你应该使用 --skip-worktree 这是为修改的跟踪文件,用户不想再承诺,并保持 --assume-unchanged 性能,以防止 git 检查大跟踪文件的状态. 查看 https://stackoverflow.com/a/13631525/717372 更多详细信息...

git update-index --skip-worktree <file>

取消

git update-index --no-skip-worktree <file>

這不再是最新 Git 的問題(v2.17.1 在寫作時)。

.gitignore 文件最终忽略了跟踪但删除的文件. 您可以通过运行下列脚本来为自己测试此文件. 最终 git 状态声明应该报告“没有任何承诺”。

# Create an empty repository
mkdir gitignore-test
cd gitignore-test
git init

# Create a file and commit it
echo "hello" > file
git add file
git commit -m initial

# Add the file to gitignore and commit
echo "file" > .gitignore
git add .gitignore
git commit -m gitignore

# Remove the file and commit
git rm file
git commit -m "removed file"

# Reintroduce the file and check status.
# .gitignore is now respected - status reports "nothing to commit".
echo "hello" > file
git status

把它移动出来,承诺,然后把它移动回来。

这对我来说在过去已经工作了,但可能有更“迷人的”方式来实现这一点。