How to replace a string on all commit history

1 min read | by Jordi Prats

If we need to change the existing commits we can use the git filter-branch command to, for example, replace strings, among many other changes we can do

If we want to replace some typos, for example replace calender for calendar, we can use the following command:

$ git filter-branch -f --msg-filter \
  'sed "s/calender/calendar/g"' \
  --tag-name-filter cat -- --all
Rewrite 7fa29a4fc2ea62e8f766f77c467d1e3df92b2677 (3/3) (0 seconds passed, remaining 0 predicted)    
WARNING: Ref 'refs/heads/demo_branch' is unchanged
Ref 'refs/heads/master' was rewritten

Since we are going to change the resulting commit change, the entire repo history will be changed from that point. To push the changes to a remote repository we will have to use the --force flag:

$ git push -f

Posted on 15/06/2022

Categories