Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git filter-branch: possible to update commit messages to reference old commit IDs?

Tags:

git

I can successfully run git filter-branch on some code to e.g. merge another repo into a subdirectory [1]:

git filter-branch --index-filter '
  git ls-files -s |
  perl -pe "s{\t\"?}{$&helper/}" |
    GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info &&
  mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE
' remotes/helper/master

But now, I'd also love to expand each commit's message to include its former ID (SHA hash), since those commits may have had comments/conversation on GitHub which aren't getting transferred over.

E.g. add an extra line like this (where bob/helpers is hardcoded/known in advance):

[COPIED FROM bob/helpers@76c7c080b3bd2f93dc78e4864899d668a57cd9f9]

As far as I can tell, Git's msg-filter only gives me the original message, and Git's env-filter doesn't contain the commit ID (SHA has) as an input variable. Is this possible then, or no?

Thanks!

[1] Via this great article, with the Perl filter from this email thread to workaround a bug on Mac OS X.

like image 427
Aseem Kishore Avatar asked Apr 20 '12 15:04

Aseem Kishore


People also ask

How do you update an old commit message?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N . Don't amend pushed commits as it may potentially cause a lot of problems to your colleagues.

Does git filter branch rewrite history?

Lets you rewrite Git revision history by rewriting the branches mentioned in the <rev-list options>, applying custom filters on each revision. Those filters can modify each tree (e.g. removing a file or running a perl rewrite on all files) or information about each commit.

What does git filter branch do?

You use it via git filter-branch --subdirectory-filter name_of_subdir @ . This is useful for extracting the history of a folder into its own repository. Another useful filter is the tree filter, you can use it to do things like moving around, creating, or removing files.


1 Answers

--msg-filter would be where to do it, and the original commit ID is in the environment variable $GIT_COMMIT, so (untested) make the message filter simply: cat; echo "[COPIED FROM bob/helpers@$GIT_COMMIT]".

like image 71
torek Avatar answered Sep 21 '22 23:09

torek