git-filter-repo
This topic describes how to use git-filter-repo to modify commit history.
Introduction
git-filter-repo is a tool officially recommended for modifying commit history. For more information, see https://github.com/newren/git-filter-repo/tree/main/contrib/filter-repo-demos.
Prerequisites
To use the git-filter-repo tool, you need the following:
git version 2.22.0 or later
Some features require git version 2.24.0 or later
Python 3.5 or later
Download
git clone https://github.com/newren/git-filter-repo.gitInstall
cd git-filter-repo/
sudo cp git-filter-repo /usr/local/binModifying a commit email or message creates a new commit and updates the branch to point to it. Before you proceed, create a new branch to save the current branch state. For example, run `git branch tmp`.
Modify a commit email
Parsing Execution Methods
The tool's execution pattern is as follows. For example, to modify an email address:
The command git-filter-repo --email-callback 'BODY' generates a Python callback function.
def email_callback:
BODYProcedure
For example, to change the email suffix from alibaba-inc.com to example.com for the last two commits on the master branch, run the following command:
git-filter-repo --email-callback 'return email.replace(b"alibaba-inc.com", b"example.com")' --force --refs master~2..masterThe `--force` flag is added because git-filter-repo can only modify a freshly cloned repository by default. You must add the `--force` flag to modify an existing repository. The result is as follows:
commit 372f7ca59fe3dc99efc0d106f1951594d7a8aca1 (origin/master, origin/HEAD)
Author: xxx <xxx@alibaba-inc.com>
Date: Wed Feb 19 20:27:51 2020 +0800
add 1
commit 869519cb1736b5d17afd04c2d1be9ff863502cdc
Author: xxx
Date: Mon Feb 17 23:05:35 2020 +0800
misha-MacBookPro:Codeup-Demo-123 teambition$ git-filter-repo --email-callback 'return email.replace(b"xxx@alibaba-inc.com", b"example.com")' --force --refs master~2..master
Parsed 2 commits
New history written in 0.05 seconds...
HEAD is now at de93869 add 3
Completely finished after 0.08 seconds.
misha-MacBookPro:Codeup-Demo-123 teambition$ git log
commit de9386925dc09a27d3f6e44f6c2c00d3f54b6f19 (HEAD -> master)
Author: xxx <xxx@example.com>
Date: Wed Feb 19 20:29:47 2020 +0800
add 3
commit e90060d7131ab7dbad6a715d5fba21126fa058c9
Author: xxx <xxx@example.com>
Date: Wed Feb 19 20:29:29 2020 +0800
add 2
commit f5ee710107e10b3197221b0102d5feac8d3ff706
Author: xxx <xxx@alibaba-inc.com>
Date: Wed Feb 19 20:29:12 2020 +0800
add 1
commit 372f7ca59fe3dc99efc0d106f1951594d7a8aca1 (origin/master, origin/HEAD)
Author: xxx <xxx@alibaba-inc.com>
Date: Wed Feb 19 20:27:51 2020 +0800
add 1
commit 869519cb1736b5d17afd04c2d1be9ff863502cdcThe email suffix for the commits on the master branch has changed from alibaba-inc.com to example.com. Note that because the email address was modified, the corresponding commit IDs have also changed.
To modify the email for all commits on the current branch, remove master~2..master and run the following command:
git-filter-repo --email-callback 'return email.replace(b"alibaba-inc.com", b"example.com")' --force --refs masterModify a commit message
The method is similar to modifying a commit email. Use the --message-callback command. For example, to change "3" to "hi" in the message of the last commit on the master branch:
git-filter-repo --message-callback 'return message.replace(b"3", b"hi")' --force --refs master~1..masterAfter you run the command, the message of the last commit changes from "3" to "hi".
misha-MacBookPro:Codeup-Demo-123 teambition$ git-filter-repo --message-callback 'return message.replace(b"3", b"hi")' --force --refs master~1..master
Parsed 1 commits
New history written in 0.09 seconds...
HEAD is now at 759ca4c add hi
Completely finished after 0.13 seconds.
misha-MacBookPro:Codeup-Demo-123 teambition$ git log
commit 759ca4c0a28e7cb6f5cd4f148b8f896573c7aa87 (HEAD -> master)
Author: xxx <xxx@example.com>
Date: Wed Feb 19 20:29:47 2020 +0800
add hi
commit e90060d7131ab7dbad6a715d5fba21126fa058c9
Author: xxx <xxx@example.com>
Date: Wed Feb 19 20:29:29 2020 +0800
add 2
commit f5ee710107e10b3197221b0102d5feac8d3ff706
Author: xxx <xxx@alibaba-inc.com>
Date: Wed Feb 19 20:29:12 2020 +0800
add 1
commit 372f7ca59fe3dc99efc0d106f1951594d7a8aca1 (origin/master, origin/HEAD)
Author: xxx <xxx@alibaba-inc.com>
Date: Wed Feb 19 20:27:51 2020 +0800
add 1Advanced modification
For example, to add the prefix "bugfix: " to the message of the last commit on the master branch, run the following command:
git-filter-repo --message-callback '
message=b"bugfix: " + message
return message
' --refs master~1..master --forceThe result is as follows. The prefix "bugfix: " has been added to the message:
HEAD is now at c103a52 bugfix: add hi
Completely finished after 0.07 seconds.
misha-MacBookPro:Codeup-Demo-123 teambition$ git log
commit c103a527a6f90e5498af72a35e2705566b280e8b (HEAD -> master)
Author: xxx <example.com>
Date: Wed Feb 19 20:29:47 2020 +0800
bugfix: add hi
commit e90060d7131ab7dbad6a715d5fba21126fa058c9
Author: xxx <example.com>
Date: Wed Feb 19 20:29:29 2020 +0800
add 2
commit f5ee710107e10b3197221b0102d5feac8d3ff706
Author: xxx <xxx@alibaba-inc.com>
Date: Wed Feb 19 20:29:12 2020 +0800
add 1In summary, whether you modify a commit email or a commit message, you can write a corresponding callback function in Python to define the modification rules.
For more information about advanced features, see https://htmlpreview.github.io/?https://github.com/newren/git-filter-repo/blob/docs/html/git-filter-repo.html.