0

I don't know exactly how to describe it in Git terms but what I do is:

  • I make some changes, commit it to a "Add a search box" commit
  • I realized I had a parenthesis missing in my code, change it, commit it as "fix"
  • Then git rebase -i HEAD~2:

    pick 34ea25a Add a search box
    f 9c4b283 fix
    
  • Save the file

  • And yeah, I have a nice history

But I would like to automate that, so how can I use git rebase without having to open an editor?

Dorian
  • 22,759
  • 8
  • 120
  • 116

2 Answers2

2

First of all, if you know that you are going to squash your next commit when doing it, you can just use git commit --amend instead. Here's example workflow:

$ git commit -am "Add a search box"
$ vim file1.c    # fix something
$ git commit --amend file1.c

Git interactive mode, as the name suggest, is designed for interactive use. You can, however, do this using GIT_SEQUENCE_EDITOR environment variable or sequence.editor config option. Both works the same - you can set them to some script that will get a standard interactive rebase file as an input. It's output will be used for actual rebase. You can see some suggestion on how to use it in this question.

Community
  • 1
  • 1
Krzysztof Adamski
  • 2,039
  • 11
  • 14
  • I edited the question to explain that it's not a typo in the commit message but in the code source – Dorian Apr 09 '14 at 21:52
  • @Dorian: It doesn't matter. Commit ammending is not only about changing commit message. You can also change the commit itself. I've updated my answer to show my point. – Krzysztof Adamski Apr 09 '14 at 21:56
  • Awesome, that's exactly what I was looking for. And the rebase scripting will be useful for something else. Thanks a lot. – Dorian Apr 09 '14 at 22:07
0

My first solution is a simple one and works only if you have a clean branch, i.e. no untracked files, and only for the latest commit.

Add a shell script to a folder in your PATH like ~/bin and name it git-reword -- make it executable.

#!/bin/bash
if [ -z "$1" ]; then
  echo "Message missing"
  exit 0
fi
git reset --soft HEAD^1
git commit -am "$1"

In your repo you can now type

$ git reword "new message"
Karsten S.
  • 2,349
  • 16
  • 32