我过去向 git 提交了一些错误的文件。之后,我排除并忽略了这些文件:
git rm --cached -r config.php
git reset config.php
echo "config.php" >> .gitignore
git update-index --assume-unchanged config.php
现在 git 提交效果很好。配置.php中的更改不会提交。
但是:如果我存储工作目录,例如切换分支,则配置.php将从本地存储库中恢复。
如何在不更改忽略(和从索引排除)文件的情况下使用 git stash
?
编辑的答案
你做了一个git rm --cached -r config.php
,但没有提交那个更改。
接下来你做了git reset config.php
,这清除了你之前所做的git rm
的效果。所以你现在又回到了你开始的地方。
然后,您将config.php
添加到.gitignore
。这不会有任何影响,因为config.php
现在仍在跟踪中。
最后,您做了一个git update-index --assume-unchanged config.php
,因此,如果您执行git status
/git diff
,则任何更改在本地都不可见。这些步骤给人一种错觉,即git rm
和.gitignore
工作正常,而实际上并非如此。
相反,这样做(-r
标志是多余的,这不是目录)
git rm --cached config.php
echo config.php >> .gitignore && git add .gitignore
git commit -m "removing config.php"
原始答案
但是:如果我存储工作目录,例如切换分支,则配置.php将从本地存储库中恢复。
好吧,您刚刚从当前分支中删除了 config.php(我们称之为 master
),但另一个分支other
仍在跟踪它。所以这就是你看到它的原因。它与git stash
无关.
您可能应该将分支master
合并到other
中,以便将提交引入分支other
。
我在 https://help.github.com/articles/remove-sensitive-data 找到了解决方案:
git filter-branch --force --index-filter
'git rm --cached --ignore-unmatch config.php'
--prune-empty --tag-name-filter cat -- --all
可能需要reset
将其从将来的提交中排除:
git reset config.php
从本地分支中删除 config.php 的所有信息后,我强制推送以更新远程存储库:
git push origin master --force
现在,我的藏匿区工作正常。