git-filter-repo

更新时间:
复制 MD 格式

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.git

Install

cd git-filter-repo/
sudo cp git-filter-repo /usr/local/bin
Important

Modifying 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:
      BODY

Procedure

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..master

The `--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:

高的 - 2024-11-18T145814

The 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 master

Modify 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..master

After you run the command, the message of the last commit changes from "3" to "hi", as indicated by ① in the image below:

Advanced 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 --force

The result is as follows. As indicated by ②, the prefix "bugfix: " has been added to the message:

In 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.