Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebasing a git history with empty commit messages

Tags:

git

I recently converted an svn repository with git svn. Unfortunately the svn history has a number of empty commit messages. This is a problem when I rebase and edit/reword a commit before the most recent commit without a commit message.

$ git rebase -i d01
[detached HEAD ff9839c] asdf
 2 files changed, 9 insertions(+), 0 deletions(-)
Aborting commit due to empty commit message.
Could not apply 054e890... 

$ git branch
* (no branch)
  master

$ git commit --amend
fatal: You are in the middle of a cherry-pick -- cannot amend.

In this example I made a commit message for the second most recent commit with an empty commit message and the rebasing stopped on the most recent commit with an empty commit message.

I would like to edit all of the commits with empty messages at once. Is there a way I can do that? Maybe I can change all commits with a empty commit message to have the commit message "empty" first?

like image 976
schmmd Avatar asked Dec 17 '11 03:12

schmmd


People also ask

Does rebase rewrite commit history?

Changing older or multiple commits. To modify older or multiple commits, you can use git rebase to combine a sequence of commits into a new base commit. In standard mode, git rebase allows you to literally rewrite history — automatically applying commits in your current working branch to the passed branch head.

How do I rebase a commit message?

On the command line, navigate to the repository that contains the commit you want to amend. Use the git rebase -i HEAD~n command to display a list of the last n commits in your default text editor. Replace pick with reword before each commit message you want to change.

How does rebasing affect the history of a repository?

Instead of blindly moving all of the commits to the new base, interactive rebasing gives you the opportunity to alter individual commits in the process. This lets you clean up history by removing, splitting, and altering an existing series of commits. It's like Git commit --amend on steroids.


3 Answers

To replace empty commit messages with some template, you can do something like this:

git filter-branch -f --msg-filter '
read msg
if [ -n "$msg" ] ; then
    echo "$msg"
else
    echo "The commit message was empty"
fi'
like image 97
manojlds Avatar answered Nov 01 '22 17:11

manojlds


As I commented before, this totally destroys comments if you happen to have newlines in them. Here's a perl script that does this without being destructive:

#!/usr/bin/perl

my $data = "";    
while(<STDIN>) {
    $data .= $_;
}

if($data =~ /^\s*$/) { $data="[Empty message]\n"; }
print "$data";

Then, just git filter-branch -f --msg-filter /path/to/perlfilter.pl

like image 39
lucasmo Avatar answered Nov 01 '22 18:11

lucasmo


If you have multiple lines you could also use cat:

git filter-branch -f --msg-filter \
 'msg=$(cat); if [ "$msg" = "" ]; then echo "Here was an empty commit message"; else echo "$msg"; fi'

This handles commit messages with multiple lines as well as messages that contain empty lines for spacing.

like image 30
cgrote Avatar answered Nov 01 '22 17:11

cgrote