How to replace a string in a whole Git history?

后端 未结 3 1916
萌比男神i
萌比男神i 2021-01-05 04:05

I have one of my passwords commited in probably few files in my Git repo. Is there some way to replace this password with some other string in whole history automatically so

3条回答
  •  一个人的身影
    2021-01-05 04:41

    git filter-repo --replace-text

    Git 2.25 man git-filter-branch already clearly recommends using git filter-repo instead of git filter-tree, so here we go.

    Install https://superuser.com/questions/1563034/how-do-you-install-git-filter-repo/1589985#1589985

    python3 -m pip install --user git-filter-repo
    

    and then use:

    echo 'my_password==>xxxxxxxx' > replace.txt
    git filter-repo --replace-text replace.txt
    

    or equivalent with Bash magic:

    git filter-repo --replace-text <(echo 'my_password==>xxxxxxxx')
    

    Tested with this simple test repository: https://github.com/cirosantilli/test-git-filter-repository and replacement strings:

    d1==>asdf
    d2==>qwer
    

    The above acts on all branches by default (so invasive!!!), to act only on selected branches use: git filter-repo: can it be used on a specific branch? e.g.:

    --refs HEAD
    --refs refs/heads/master
    

    The option --replace-text option is documented at: https://github.com/newren/git-filter-repo/blob/7b3e714b94a6e5b9f478cb981c7f560ef3f36506/Documentation/git-filter-repo.txt#L155

    --replace-text ::

    A file with expressions that, if found, will be replaced. By default, each expression is treated as literal text, but regex: and glob: prefixes are supported. You can end the line with ==> and some replacement text to choose a replacement choice other than the default of ***REMOVED***.

    Of course, once you've pushed a password publicly, it is always too late, and you will have to change the password, so I wouldn't even bother with the replace in this case: Remove sensitive files and their commits from Git history

    This seems to be the same question: How to substitute text from files in git history?

    Tested on git-filter-repo ac039ecc095d.

提交回复
热议问题