Michael Durrant
Michael Durrant

Reputation: 96484

How can I search my git history for a string and change all occurrences?

For a password, for example...

I can find all the occurrences of "my_top_secret" with:

git grep my_top_secret $(git rev-list --all)

How can I change them all, e.g. with --filter-branch and --tree-filter to change my_top_secret to be not_top_secret_now

so I really remove the references from anyone who clones the repo after this point (or searches github).

Upvotes: 0

Views: 210

Answers (1)

Andrew C
Andrew C

Reputation: 14853

I don't have a repo I can test this with, but you would basically run a filter branch on all your commits, and then for each commit search for the string and replace any instances that are found. You'll probably need to fix the sed quoting.

git filter-branch --tree-filter 'git grep -n my_top_secret | xargs sed -i 's/my_top_secret/not_top_secret_now/g' ' -- --all

Upvotes: 2

Related Questions