[Solved] Aborting "git commit --amend"

In the past, I would make a change and do a "git commit -a --amend to add it to the previous commit. It opens Pe as I set “lpe” as editor in git’s config.
If I made no change in the commit message and just quit Pe, the commit would be aborted, because there was no change.

I got used to this behaviour, but with some update (I suppose) this changed, and now quitting Pe without change does not abort anymore, but does the amend.

Any idea if there’s a way to get the old behaviour back?

Perhaps you can script this based on thr status code git status gives on exit.

Sorry, I don’t understand. You mean I should do a “git status” and call a “git commit --amend” based on its exit?

I don’t see how this will work. I sometimes want to see the status without committing, and also do a “git commit --amend” without looking at the status before…

If your problem is that you commit needlessly and type this out it won’t work indeed, but in that case I’m not sure there is an easy answer. :slight_smile:

It’s most of the time not needlessly. Sometimes, I “amend” and while editing the commit message I realise that this would better be its own commit. In the past, I would just quit Pe and discard the changed commit message. This would abort the amend-commit. Now it doesn’t.

I hoped that there was some git.config switch I haven’t found yet to revert to the old behaviour. Like there was with “pager.branch false”, that avoids piping the “git branch” output to “less”. Another recent change in git behaviour.

Has Pe an auto-save feature enabled?

I don’t think Pe has an auto-save.
Also, even if I made no changes in the commit message, quitting will make the amend-commit.
It used to abort - I even had to make some change in a #-comment line if I wanted to amend-commit something and didn’t have changes to the commit message…

Some documentation from 2016 on stackoverflow claims emptying the commit message will make git abort the commit, perhaps this works (just alt a; delete and save)

Iirc git watches for the file handle to be closed

That works, thanks!

Aborting commit due to empty commit message.

git reflog is also worth reading about. It tracks changes to branches, including git commit --amend. It will show the previous SHA1 before the amend happened, and you can check that out (in a detached HEAD state) and then either make that a new branch or delete or rename the other branch with the “bad amend” and then name the detached HEAD branch the old branch name. Then things will be like before the amend.

Sure, this is a bit more complicated but allows you to get out of seemingly “unfixable” situations. This applies to other branch modification commands too.

Remember Git branches just point to a tree of commits.

1 Like

I feel for you, if anything this behavior smells like a bug and a bad one too, i mean abort != apply, it shouldn’t just silently commit changes without the user’s confirmation, and it adds yet another complication to the already complicated git options, maybe you should report this to the git folks?

This seems strange to me, as far as I know the only way to abort is leaving the commit message empty. Otherwise you would be forced to change the commit message each time you fix even a simple typo in your code with an amend?

Why? You asked git to change a commit and it does just that. It has always worked like this. So, not a bug. Maybe it is confusing, but all git features are, so, that doesn’t make it a bug?

1 Like

You’re only half right, because you forgot the fact the you also
asked it to abort the change, therefor git should not go ahead and silently validate the change on your behalf when you clearly aborted the change, and that’s exactly what humdinger is complaining about, and if it has always behaved this way it still a bug or a bad default.

1 Like

To abort the change you have to empty the commit message. This works and has always worked.

What humdinger had, for some reason, is a way to abort without doing that. As far as I know this is not how it works for Git, so I have no idea how it happened to work.

1 Like

I think in some versions git would invole the editor directly, and if the editor returns with an error code also abort the commit.

I don’t see anything about that in the “git commit” documentation: Git - git-commit Documentation

The only possibly relevant option is -t to provide a template. In the case a template is used:

-t --template=
When editing the commit message, start the editor with the contents in the given file. The commit.template configuration variable is often used to give this option implicitly to the command. This mechanism can be used by projects that want to guide participants with some hints on what to write in the message in what order. If the user exits the editor without editing the message, the commit is aborted. This has no effect when a message is given by other means, e.g. with the -m or -F options.

So, maybe humdinger you had ‘commit.template’ enabled in your configuration? But I’m not sure if that would affect --amend. Or maybe it used to and that was considered a bug?

Nope, no templates. Below is what has been my git config for pretty much ever. I only recently added the [pager] part, because another change in behaviour made it necessary for the branch command, and the [color *].

[alias]
	st = status -s
	re = remote -v
	ci = commit -a
	co = checkout
	br = branch
[core]
	editor = lpe
	untrackedCache = true
	checkStat = minimal
[credential]
	helper = cache --timeout=36000
[push]
	default = simple
[user]
	email = hum@g.com
	name = Humdinger
[init]
	defaultBranch = master
[pager]
	branch = false
	diff = false
[color "diff"]
    old = yellow
    new = green
	meta = cyan
[color "status"]
    changed = green
	untracked = yellow