我过去将一些错误的文件提交给了 git。之后我排除并忽略了这些文件:
git rm --cached -r config.php
git reset config.php
echo "config.php" >> .gitignore
git update-index --assume-unchanged config.php
现在 git commit 工作得很好。 config.php 中的更改未提交。
但是:如果我 stash 工作目录,例如切换分支,那么 config.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 差异
。这些步骤给人一种错觉,即 git rm
和 .gitignore
可以正常工作,但实际上却没有。
相反,这样做(-r
标志是多余的,这不是目录)
git rm --cached config.php
echo config.php >> .gitignore && git add .gitignore
git commit -m "removing config.php"
原始答案
But: If I stash the working directory for example to switch the branch, than config.php is restored from the local repos.
嗯,您刚刚从当前分支(我们称它为 master
)删除了 config.php,但另一个分支 other
仍在跟踪它。所以这就是你看到它的原因。它与 git stash
无关。
您可能应该将分支 master
merge 到 other
中,以将提交带到分支 other
中。
关于git stash 恢复被忽略的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19981419/