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


当前回答

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

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

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

此分類上一篇

它现在工作。

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

其他回答

我用 git filter-branch 完成了这一点,我所使用的准确命令从 man 页面上取出:

警告:此将从您的整个历史中删除文件

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

这个命令将恢复整个命令历史,在每个命令之前执行 git rm 以便摆脱所指定的文件. 不要忘记在运行命令之前备份它,因为它将失去。

复制/复制(单线)的答案是:

git rm --cached -r .; git add .; git status; git commit -m "Ignore unwanted files"

这个命令不会改变.gitignore 文件的内容. 它只会忽略已承诺到 Git 存储库的文件,但现在我们已经添加到.gitignore。

命令 git 状态; 只需审查更改,可以下载。

最终,它将立即通过“不要错过不需要的文件”的消息进行更改。

如果您不希望承诺更改,请放下命令的最后一部分(git commit -m “不要错过不需要的文件”)

移动或复制文件到一个安全的位置,所以你不会失去它。

文件仍然会出现,如果你转到以前的任务之一,或另一个分支,它没有被删除. 但是,在所有未来的任务,你不会再看到文件. 如果文件在 Git 忽略,那么你可以将其返回文件夹, Git 不会看到它。

对于我来说,文件在历史上仍然可用,我首先需要清除添加删除文件的命令: https://gist.github.com/patik/b8a9dc5cd356f9f6f980

下面的例子结合了最后3个命令

git reset --soft HEAD~3
git commit -m "New message for the combined commit"

推破的承诺 如果承诺已推到远程:

git push origin +name-of-branch

這不再是最新 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