Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commit reword without interactive rebase?

Tags:

git

Is it possible to run an automatic reword of a commit (identified by hash) without using interactive mode? What I aim for is a one-liner for rewording because I need to invoke from code.

UPDATE:

I modified it a little for inline use.

git filter-branch --msg-filter "ruby -e \"puts (ENV['GIT_COMMIT'] == '1ba2dd66581f6fbc03d1a6406a0ed61b6473c9ab' ? 'new msg' : STDIN.read)\"" HEAD

Would be nice though in pure bash (without ruby), but I had troubles with getting the STDIN to work (using read).

like image 522
Era Avatar asked Jan 08 '12 22:01

Era


1 Answers

git-filter-branch can do what you want. Specifically, you want the --msg-filter argument:

This is the filter for rewriting the commit messages. 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.

One wrinkle is that git-filter-branch wants to run on every commit in the branch, while you only want to rewrite one commit (and then effectively rebase the rest). So your message filter needs to have some conditional logic, to output either the new commit comment (for the one commit you actually do want to rewrite), or the original, unmodified commit message (for everything else).

This does the trick:

git filter-branch --msg-filter "ruby /path/to/filter.rb 843c2da5e07de914ea2319fa3295730ff8b3b0a1 'New message'" HEAD

git-filter-branch changes the current directory before it invokes your script, so you do need to fully qualify /path/to/filter.rb.

Here's filter.rb:

if ENV['GIT_COMMIT'] == ARGV[0]
  puts ARGV[1]
else
  puts STDIN.read
end

This could probably be rewritten more succinctly in shell script, perhaps without even putting the message-filter script in a separate file; but I'm running on Windows so I don't have anything that fancy. I do have Ruby, so I used that, and it does work.

like image 141
Joe White Avatar answered Oct 05 '22 04:10

Joe White