1

I'm wondering why my git alias got stuck and how it caused a rebase.

Steps to reproduce:

git config --global alias.am "commit --amend"

I can see that the alias is added with:

git config --global -e

[alias]
    am = commit --amend --no-edit

Create new test repo:

> git init

> git am

Ctrl + c, Ctrl + c

Nothing happens so I have to close it manually. The expected result would be fatal: You have nothing to amend. in this case since there are no commits. But what happens is that git is now in rebase mode: (master|AM/REBASE)

When running in a repo with a commit, the expected result is to open my default git editor so I can edit the commit message but the same unexpected behaviour explained above happens.

The question

How come my git am alias gets stuck as it does, and how does it put me in a rebase situation?

What I have tried

I guess that my git alias is faulty in some way and that it causes it to start the git commit --amend but in a way so its unable to start the editor. And since a git commit --amend probably does a rebase in the background to do its thing it is left in that state when I force it to abort?

However, I have tried to add --no-edit to no avail so it seems not to be an editor error..

This issue caused me to accidentally do a git rebase --abort and lose some local non-staged changes.

System

I'm on Windows 10 using Git Bash.

I have tested using both emacs and notepad as my default git editors, both with the same result.

Alex Telon
  • 1,107
  • 1
  • 14
  • 30

2 Answers2

3

The issue was that git am already is a command.

In general when dealing with git alias, note that.

  • Git gives no warning when you try to overwrite an already existing keyword.

  • Git still saves the alias in the config file.

When writing a new git alias remember to test if the alias you intend to use already exists and hope that it's not added as a keyword in the future.

Alex Telon
  • 1,107
  • 1
  • 14
  • 30
  • 1
    `git am` is actually quite well known to people who have to handle patches (possibly sent via email). It's pretty much the opposite of `git format-patch`. – cmaster - reinstate monica Mar 20 '18 at 14:03
  • Yes you are probably right. I was thinking from my perspective which is using git at work with a shared private repo and in hobby-projects on github. Neither which which does not necessarily need a lot of patches. Though I should probably take a closer look at it for those temporary code-drop situations maybe. – Alex Telon Mar 20 '18 at 14:19
  • 1
    "nothing about git am being its own command" - actually I wasn't aware of AM and was pretty new to git in general when I posted the answer in '14. The `rebase --abort` solved the guy's problem so I didn't look further. I have asked the guy to unaccept my answer so I can delete it – Tim Mar 22 '18 at 11:18
  • No worries. Yeah that might be the best course of action. Another option is to edit your answer and removing mine? Duno what is the propper SO way of doing this. – Alex Telon Mar 22 '18 at 13:03
1

According to the document on alias.* in git-config,

To avoid confusion and troubles with script usage, aliases that hide existing Git commands are ignored.

am is an existing Git command, so alias.am is ignored. git am is waiting for some input but nothing is received, neither from stdin or a patch file. So it looks stuck.

When you press Ctrl+C to exit, the process of git am is interrupted as if it encounters a conflict. A temporary folder rebase-apply is created under .git. With this folder existing, Git knows it's in the status (master|AM/REBASE). You could either remove .git/rebase-apply or run git am --abort to get rid of it.

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
ElpieKay
  • 27,194
  • 6
  • 32
  • 53