【发布时间】:2025-12-22 09:25:09
【问题描述】:
我找到了一种方法,git 不会要求您存储,而是默默地删除您认为在 .gitignore 中安全的文件。 即使您自初始提交以来拥有相同的忽略文件也是如此。
当您处于已删除但在 .gitignore 中列出的上述文件的提交中,然后您签出另一个存在的提交时,就会出现问题:
git init
mkdir ignored/
echo stuff > ignored/file
echo otherstuff > otherfile
git add -A
# Opps added my ignored folders files
# Forgeting to rm --cache
echo ignored/ > .gitignore ; git add .gitignore
git commit -m 'accidentally add ignored/file'
git status
touch dummyfile; git add dummyfile
# Remembered to rm --cache
git rm --cache -rf ignored/file #This file is now vulnerable to clobber
git commit -m 'add stuff'
echo somechange >> ignored/file
## Wait for it..
git checkout HEAD~
## somechange has been silently clobbered!!
# Please paste first paragraph, observe and then past the second.
# Note both commits have the correct ignore file and are not immune!
(在将上述代码粘贴到终端之前,cd 到一个空文件夹)
有没有办法阻止这种无声的破坏?
【问题讨论】:
标签: git