Mcm
Mcm

Reputation: 119

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

Upvotes: 1

Views: 754

Answers (2)

Andrei Prigorshnev
Andrei Prigorshnev

Reputation: 608

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)

Upvotes: 1

torek
torek

Reputation: 489628

No part of any commit can ever be changed, so in that sense, the answer is no.

The git pull command consists, in essence, of running git fetch followed by either git merge or git rebase. The rebase command works by copying existing commits to new (and different, and presumably improved) commits, so in this sense, there is a way to get something like what you want. But the rebase that git pull runs only copies your commits, not the fetched ones; you seem to want to copy the fetched commits to new-and-improved commits, with different hash IDs.

To do that, you must separate your operation. Stop using git pull. Run:

  • git fetch, to obtain new commits; then
  • your chosen method of copying those to new-and-improved commits; then
  • git merge, if desired, or whatever other command is desired (perhaps none).

As a general rule, whenever you are trying to do something fancy, the answer to the question about how to make git pull do it is just "stop using git pull". 😀 Think about what git pull does, and break that down into the parts that are useful and the parts that aren't: usually the fetch is useful, and sometimes the second command is useful, but usually you want to stick something in between the two.

Upvotes: 1

Related Questions