Dorian
Dorian

Reputation: 23989

Git fix last commit: rebase it with previous one

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

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

Upvotes: 0

Views: 246

Answers (2)

Krzysztof Adamski
Krzysztof Adamski

Reputation: 2079

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.

Upvotes: 2

Karsten S.
Karsten S.

Reputation: 2391

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"

Upvotes: 0

Related Questions