git stash 恢复被忽略的文件

标签 git gitignore git-stash

我过去将一些错误的文件提交给了 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/

相关文章:

git - 为什么 git stash 会删除我的 node_modules 文件夹?

xcode - 不能忽略 UserInterfaceState.xcuserstate

git - 如果我将更改存储在一个分支中然后删除该分支会发生什么

Git 存储单个未跟踪的文件?

git - 查找错误来源 : git push rejected: error: failed to push some refs

git - 使用gitversion修复错误后如何自动增加补丁版本

git - 如何停止跟踪git中目录的内容?

git - 中止 git merge 并返回到未提交的工作

git - 文件被列为前缀为 ../in git

git - ubuntu git init 权限被拒绝