我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。
当前回答
移动或复制文件到一个安全的位置,所以你不会失去它。
文件仍然会出现,如果你转到以前的任务之一,或另一个分支,它没有被删除. 但是,在所有未来的任务,你不会再看到文件. 如果文件在 Git 忽略,那么你可以将其返回文件夹, Git 不会看到它。
其他回答
這不再是最新 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
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>
.gitignore 将防止未跟踪的文件添加(没有添加 -f)到 Git 跟踪的文件集,但是, Git 将继续跟踪已跟踪的任何文件。
要停止跟踪文件,我们必须从指数中删除文件:
git rm --cached <file>
要重新删除文件夹和文件中的所有文件:
git rm -r --cached <folder>
将文件从主审查中删除,将在下一个任务中进行。
警告:虽然这不会从您的本地机器中删除物理文件,但它会在下一个 git 拖动时将从其他开发人员的机器中删除文件。
git ls-files -c --ignored --exclude-standard -z | xargs -0 git rm --cached
git commit -am "Remove ignored files"
这取消了被忽略的文件列表,从指数中删除它们,并执行了变更。
例如,在使用 CVS 时,这个问题不存在, CVS 将信息存储为基于文件的变更列表。
这两篇文章对我很有帮助:
git assume-unchanged vs skip-worktree 以及如何用 Git 忽略跟踪文件的变化
git update-index --skip-worktree <file>
从此,此文件中的所有本地变更将被忽视,不会去远程。如果文件在远程中被更改,冲突将发生,当 git pull. Stash 不会工作。
git update-index --no-skip-worktree <file>
git stash
git pull
文件内容将被远程内容取代. 将您的更改从安全地点调整到文件,然后再次执行:
git update-index --skip-worktree <file>