Is there a way I could change content of a file through out all commits in git?
I am stuck in situation, where one of the config file in my project has a signing password in it and its been committed on to git since the start (Almost 800+) commits so far. I want to move the password from that config file and use it separately from now on. But my concern is the older commits still have that content and I want to update in all those. The best way I could come up with is to move it to different repo altogether and start the project from fresh . But I am curious if git has some thing which I can use to achieve the same without creating new repo excluding the file which has password. Please help.
This was added as support to tell that this is not the duplication post of this . I want to remove the content from file not delete the file from git. Hope it helps to identify it as unique.
3 Solutions collect form web for “Is there a way I could change content of a file through out all commits in git?”
Sure, have a look at
git filter-branch, it is made for situations like this. Which filter arguemnt you need depends on what you want to do exactly. If you want to remove the whole config file, you might want to use
--index-filter, if you just want to remove the line with the password, you might want to use
--tree-filter and in any way you might also want to use
--tag-filter cat to get your tags moved to the rewritten commits too, otherwise they stay on the old commits.
You can use git-filter-branch, but it might be slow or inconvenient. For you it’s easier and faster to use bfg-repo-cleaner tool. It’s not distributed with git, but really stellar and super fast compared to
java -jar bfg-1.12.14.jar --delete-files <file> --private <git repo>
It will automatically remove file from all branches. Tags will be reapplied to proper commits. And for several thousand commits, it will probably work few seconds 😉
--private option is there to avoid adding information about old commit ids to rewritten commit messages.
Don’t sweat the git history. You need to invalidate and cycle the secret, even if you re-wrote the history to hide it. The secret is out, you can’t take it back.
Focus on getting the new secret out of source control and move on.