Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sffej/3c3e8d033ca9b7e053982d7a7eabd97a to your computer and use it in GitHub Desktop.

Select an option

Save sffej/3c3e8d033ca9b7e053982d7a7eabd97a to your computer and use it in GitHub Desktop.
Git: Add prefix to a range of commit messages

Original Link

Imagine that you cloned an open source project to contribute something. You implemented a bugfix through a series of atomic commits on a private branch. Just when you’re about to create a Pull Request to submit your changes, you discover in the contributor’s guide that you’re supposed to prefix each commit with the bug tracking number.

Rewriting the commit message of the last commit is easy:

git commit --amend

Rewriting a range of commits is easy too, once you know how. That’s a job for the filter-branch command, like this:

git filter-branch --msg-filter 'printf "THE_PREFIX " && cat' sha1..HEAD

The --msg-filter flag is to rewrite the commit messages. The argument within the single quotes is the shell commands to execute to manipulate the message, received from stdin. In this example we use the printf command to print the prefix and a space character, and then the cat command to print the rest of the commit message. (Note that printf is like the less portable echo -n.)

The last argument is the range of commits. This is sweet and perfect in this use case of working on a feature branch that started from a commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment