Bearded Magnum

yet another git command: git-filter-branch

one comment

While browsing the Git Community Book, I came across another great Git command: git-filter-branch. This command is a good way to edit commits en masse. The command gives you access to commit information and let you do whatever you want with them.

Here is my use case: every new feature I code for ODE has a JIRA id. It’s a good practice to mention this identifier in your commit messages, so that people can quickly know which issue this commit refers to, and eventually consult the JIRA page to get all the information related to it.  Some tooling might even be built around this simple practice: for instance JIRA will index all the commits refering to a given issue.

Back to our case, so as I develop this new feature on my local Git branch, it happens that this JIRA id is rarely in my commit messages. Either because I forget, either because at the time of the commit I’m thinking to squash it later, etc. So when it comes to push these commits, I need to rebase my branch in interactive mode and manually edit each commit to prepend the JIRA id. Quite painful actually when you have a dozen of commits.

git-filter-branch makes that job in one single command:

$git-filter-branch --msg-filter "sed -e 's/.*/ODE-415: &/'" HEAD~8..

The argument is evaluated in the shell with the original commit message on standard input; its standard output is used as the new commit message. Nice..

No need to say that git-filter-branch should be use with care. The original refs, if different from the rewritten ones, will be stored in the namespace refs/original/.

Written by Alexis

February 9th, 2009 at 4:13 pm

Posted in Uncategorized