Search code examples
gitgit-pull

rewrite commit message when pulling


I'm merging multiple repos to one. For that I add each of the origin repos with git add ..., and then I run git pull --no-edit --quiet --allow-unrelated-histories ...

My question is whether it is possible to rewrite the commit message at the same time, so as I can add a label to the commit subject or a trailer to the commit body.

EDIT: To clarify I want to rewrite all the commit messages with changes, not the merge message of the pull


Solution

  • You cannot change commit messages when pulling commits.

    It mustn't be possible by design. When you change a commit message, a hash of this commit changes. Two identical commits that differ only in commit messages will have different hashes. So git always consider such commits as two different commits.

    pull is supposed to incorporate changes from the remote repository into your local repository. If you rewrite commit messages during the pull process, it'll change the hashes of these commits. From a git perspective, you'll end up with another history in your local repo. It means that incorporation wasn't done, and you should pull again.

    So you cannot change commit messages when pulling commits. But you can rewrite commit messages after pulling. You can use git filter-branch with --msg-filter option to rewrite commit messages.

    I would advise you to do it in the next order:

    1. Pull commits from one repository
    2. Rewrite commits from this repository using git filter-branch. (This time, you have only links like "#nnn" in your history, so you can easily change them to "first-repo-url/nnn")
    3. Pull commits from the next repository and then rewrite those commits (This time, links from the previous repository already processed, so you can separate them from links from the current repository)