How to Batch Update Git Commit Messages

By  on  

At Mozilla we're mostly strict about receiving contributions via git/GitHub.  Aside from requiring tests and enforcing code quality, one basic of submitting a commit (or several) is that the commit message begins with "bug #######", which is a reference to its Bugzilla bug.  The message requirement makes sense but I hate going back to contributors and asking them to do "the paperwork" of changing their commit message to match our standard, so I generally try to make the update for them.

Recently MDN received a contribution which upgraded CKEditor and did so via approximately 60 commits, none of which referenced the bug number for the feature update.  I badly wanted to do the commit message updates for this contributor, but I wanted to do so in an automated way -- a batch update to save me the time updating each message one by one.  Welcome to filter-branch and --msg-filter!

Prepending to Commit Messages

To prepend text to every commit message in a given range, you'd execute a message like:

git filter-branch --msg-filter 'echo "bug ###### - \c" && cat' master..HEAD

The combination of filter-branch and --msg-filter will allow you to recurse through every commit message.  The echo piece allows you to build the new string.  The last piece is the range whose commit messages should be targeted -- in this case, I'm prepending the text to every commit in the feature branch.

You can also sed to achieve this:

git filter-branch -f --msg-filter 'sed "s/^/bug ###### - /"' master..HEAD

Appending to Commit Messages

The case for appending to commit messages could be where you want to add the reviewer name(s) to the message.  Appending is roughly the same:

git filter-branch -f --msg-filter 'cat && echo "[Reviewer Walsh]"' master..HEAD

The difference here is the position of the cat command.  Everything else can be the same.

I have a love/hate relationship with git -- I love git because it's so easy for 90% of my needs but hate it when I need the other 10%.  Hopefully these examples help you if you run into this need in the future!

Recent Features

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    Vertically Centering with Flexbox

    Vertically centering sibling child contents is a task we've long needed on the web but has always seemed way more difficult than it should be.  We initially used tables to accomplish the task, then moved on to CSS and JavaScript tricks because table layout was horribly...

  • By
    CSS Vertical Center with Flexbox

    I'm 31 years old and feel like I've been in the web development game for centuries.  We knew forever that layouts in CSS were a nightmare and we all considered flexbox our savior.  Whether it turns out that way remains to be seen but flexbox does easily...

Discussion

  1. Colly

    I like to keep https://gist.github.com/carmat/8728901 up to date as much as possible. Helpful for shorthand commands to open/create things remotely. I know there are other pre-built tools out there, but this works for me

  2. Peter
    git filter-branch -f --msg-filter 'sed "s/^/bug ###### - /"' master..HEAD
    

    This doesn’t work for multiline commit messages.
    It will prepend to every single line since sed works on line level.

  3. You can fix the sed example like this:

    git filter-branch -f --msg-filter 'sed "1 s/^/bug ###### - /"' master..HEAD
    

    (Note the “1” there to limit sed to the first line)

  4. …and you can ignore the -f if you choose; that’s just a leak from me running this various times.

  5. Patrick

    How can you parameterize the prepended label in a git alias?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!