Fritz Fritz - 1 year ago 83
Git Question

How do you undo your Add Line Break in Git Commit Message?

I know how to add multiple lines already in my git commit message.

I am using:

git commit -m "
>Text here...
>[Accidental Enter Key]

However, what if I mistakenly pressed the Enter button and I want to go back to the previous line? Is there such a way?

Kaz Kaz
Answer Source

GNU Readline editing is wacky at times. In the situation when you've accidentally hit Enter in the middle of a quoted string in Bash, just hit Ctrl-C. The part of the command line before the last Enter is preserved in the history. You just lose the very last line: anything you typed since the last Enter. After this Ctrl-C, you can recall the line and edit it. This time around, you can move the cursor back through the quote: you can back-space the unwanted multi-line parts to join them into a single line.

$ echo "abc
> oops hit Enter, didn't mean to
> oops, I did it again!^C

Now up arrow:

$ echo "abc
oops hit Enter, didn't mean to_  <- cursor is here (lost the last line)
 `- no > characters here now, and you can move the cursor left
    to just before the "oops" and hit backspace to merge it with
    the "abc.

It's not clear why Readline is so inconsistent in this way: why there is the > mode for continuing the line, but then normal editing when you recall that; maybe there is a way to configure away this unfriendly > stupidity. I think it exists for the sake of newbies: the > prompt is a loud and clear signal indicating "hey, you have an unbalanced quote or something, and so I'm prompting you for more input". Still, that's no reason not to allow the user to at least backspace past the >.

In any case, in this situation you can always just complete the commit with the unintended message and then immediately do a

$ git commit --amend -m "corect message"   # oops
$ git commit --amend -m "correc message"   # darn
$ git commit --amend -m "correct message!"

Finally, get a keyboard which doesn't have a tiny backspace key, and a huge Enter key right under it! Those idiotic keyboards are the main reason you accidentally hit Enter: precisely when you want to backspace to fix something that is wrong. Imagine if gas pedals were right next to the brake!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download