git: changing an old commit message without creating conflicts

荒凉一梦 提交于 2019-12-03 09:45:00

Make a small script in your /bin directory (or any directory in your path) named git-reword-commit. (You can name it whatever you want as long as the name starts with git-. This will work regardless of whether there are merge commits or not.

#! /bin/bash
REV=$1
MESSAGE=$2
FILTER="test $(echo '$GIT_COMMIT') = $(git rev-parse $REV) && echo $MESSAGE || cat"
git filter-branch --msg-filter "$FILTER" -- --all

To use, execute

git reword-commit <commit> 'new message'

Warning: This will rewrite many commits, so the same issues that apply to rebase, apply here as well, i.e., you will need to force when you push, and other users will have to know about this.

Git puts your original refs (from before the filter-branch) in .git/refs/original. You can use the following aliases to undo/confirm any filter-branch command.

git config alias.undo-filter-branch '! DIR=$(git rev-parse --git-dir); cp -r $DIR/refs/original/refs/ .git/; rm -r $DIR/refs/original/'
git config alias.confirm-filter-branch '! DIR=$(git rev-parse --git-dir); rm -r $DIR/refs/original/'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!