Yesterday, I uploaded a portion of organized wildlife image data to the Github repository.
However, when I checked the git log later, I found that the commit messages I submitted did not follow the commit conventions.
There were a total of 85 commits, and they had already been pushed to the remote Github repository.
I have previously modified individual commit messages, but this time there are too many to change individually.
After some research and experimentation, I have summarized a method for batch modifying Git commit messages.
git filter-branch
The main function of the git filter-branch command is to help rewrite the branch history of a Git project.
Here is a detailed usage of the command:
The git filter-branch command provides various options to modify the branch history. Some commonly used options are:
--msg-filter <command>
This is the filter for rewriting the commit messages.
The argument is evaluated in the shell with the original commit message on standard input;
its standard output is used as the new commit message.
It's important to note that git filter-branch modifies the commit history, and it can have significant implications if the repository is shared or if there are existing clones.
It is recommended to use this command with caution and communicate with other collaborators to ensure a smooth transition.
For more detailed information and examples, you can refer to the official Git documentation on git filter-branch: Git - git-filter-branch Documentation.
Solution
Since it is the first time using this command and to prevent any accidental contamination of the entire project, it is advisable to create a dev branch as a means of isolation before proceeding with the experiment.
After that, run git filter-branch to rewrite all commit messages starting with add to feat: add in dev branch.
And check the result with git log
Success!
The final step is just switching the git branch from dev to main and repeat the procedure above.
And run git push -f to push these changes to remote Github repository.