Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rewrite history by running external tool on each commit

Tags:

git

c#

We have a repo with several thousands of commits that uses a really old code convention we want to replace with the default Microsoft one. The easiest is to run a code formatting tool on the latest commit only, but this way we lose "blame" and the history gets harder to follow. Can this be achieved by running the code formatting tool (in our case https://github.com/dotnet/codeformatter) on each commit, thus preserving change history, and what version of filter-branch should we use?

Edit: turns out the tool I was about to use was crashing on some of the commits, so that's why filter-branch did not work for me and did not apply any changes to the commits

like image 246
Vladimir Vasilev Avatar asked Feb 13 '18 09:02

Vladimir Vasilev


2 Answers

You can do this with the tree filter:

git filter-branch --tree-filter "CodeFormatter.exe"
like image 183
Hendrik M Halkow Avatar answered Sep 23 '22 08:09

Hendrik M Halkow


git rebase --interactive --exec CodeFormatter.exe --root

I.e. perform an interactive rebase executing the given command on every commit.

Shorten the command to just

git rebase -i -x CodeFormatter.exe --root
like image 20
phd Avatar answered Sep 23 '22 08:09

phd