3

Is there any way to censor certain worDs from git commit messages from the commit history? Not from files or actual code, but from the commit messages themselves.

nowi
  • 437
  • 4
  • 13

1 Answers1

3

You can use filter-repo tool's message callback to replace words in your commit messages

git-filter-repo --message-callback 'return re.sub(b"word",b"<redacted>",message)'

This will replace word with <redacted> in all your commit messages.

filter-repo tool is not bundled with git, so you need to install it separately.

Using filter-branch as mentioned in this answer:

git filter-branch --msg-filter 'sed "s/word/<redacted>/g"' -- --all
Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
  • 1
    I had a feeling you were thinking about filter-repo. I presented it here: https://stackoverflow.com/a/58251653/6309 – VonC Apr 20 '20 at 05:55
  • Anything that can be done only with what is bundled with git? – nowi Apr 21 '20 at 07:00
  • @nowi [`filter-branch`](https://git-scm.com/docs/git-filter-branch) is an option but it is not safe as mentioned in the docs under **WARNING** section [here](https://git-scm.com/docs/git-filter-branch#_description) . Also see VonC's answer [here](https://stackoverflow.com/a/58251653/6309) – Saurabh P Bhandari Apr 21 '20 at 07:14
  • @nowi if you are worried about installation, one easy option is to use `pip3`. Just do `pip3 install git-filter-repo` as mentioned [here](https://github.com/newren/git-filter-repo/blob/master/INSTALL.md#installation-via-pip) – Saurabh P Bhandari Apr 21 '20 at 07:18
  • @nowi See warning section [here](https://git-scm.com/docs/git-filter-branch#_warning) in git docs which also mentions about `git-filter-repo` – Saurabh P Bhandari Apr 21 '20 at 07:26
  • I do not have pyhton setup for `pip3` commands. Could you help me write a command using `filter-branch` safely? What exactly does that specific `git-filter-repo` command end up boiling to that I can use? – nowi Apr 21 '20 at 08:12
  • Is it case sensitive? – nowi Apr 21 '20 at 08:34
  • I will wait @VonC 's reply before doing it. – nowi Apr 21 '20 at 08:35
  • @nowi I believe it is generally case sensitive if you are referring to `sed ` – Saurabh P Bhandari Apr 21 '20 at 08:41
  • 1
    @nowi The sed command is case sensitive. The command looks safe, but I would recommend executing it on a fresh clone, for testing. – VonC Apr 21 '20 at 09:12
  • Isn't working for me. It first complained about msg-filter and then I realized it was missing the `--`. Now it is complaining that `fatal: bad revision 's/word//g''` – nowi Apr 22 '20 at 05:35
  • @nowi It should work now, I missed the `--` for `msg-filter` option – Saurabh P Bhandari Apr 22 '20 at 05:43
  • @nowi , If you are running this command in Windows CMD, it won't work because `sed` is not available by default for Windows except if you have installed GNU tools for Windows like CoreUtils, you can run it in git bash though – Saurabh P Bhandari Apr 22 '20 at 05:48