Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top