Question

I committed in the past some wrong files to git. After that I excluded and ignored these files:

git rm --cached -r config.php
git reset config.php
echo "config.php" >> .gitignore
git update-index --assume-unchanged config.php

Now git commit works great. Changes in config.php are not committed.

But: If I stash the working directory for example to switch the branch, than config.php is restored from the local repos.

How can I use git stash without changing ignored (and from index excluded) files?

Was it helpful?

Solution 2

I found a solution at 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

May be a reset is necessary to exclude it from future commits:

git reset config.php

After all information of config.php are removed from local branches, I made a forced push to update remote repos:

git push origin master --force

Now, my stash area works fine.

OTHER TIPS

Edited Answer

You did a git rm --cached -r config.php, but didn't commit that change.

Next you did git reset config.php, which cleared the effect of git rm you had done previously. So you are right now back to where you started from.

Then, you added config.php to .gitignore. That won't have any affect since the config.php is still tracked right now.

Finally, you did a git update-index --assume-unchanged config.php due to which any changes are not visible locally if you do a git status / git diff. These steps give the illusion that git rm and .gitignore worked fine when they didn't.

Instead, do this (the -r flag was redundant, this is not a directory)

git rm --cached config.php
echo config.php >> .gitignore && git add .gitignore
git commit -m "removing config.php"

Original Answer

But: If I stash the working directory for example to switch the branch, than config.php is restored from the local repos.

Well, You just removed config.php from the current branch ( lets call it master), but the another branch other is still tracking it. So that's why you see it. Its got nothing to do with git stash.

You should probably merge the branch master into other to bring the commit into the branch other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top