404

I just executed a command $ git commit and it opens a new editor. But I'm trying to close that new commit editor. How to do this? I'm using Git for Windows.

5
  • 5
    not a complete answer, but one way to avoid the editor is to use the "-m" option. E.g. after $ git add <your_changes>, then do $ git commit -m "my changes; I'm avoiding the editor!" Commented Feb 24, 2020 at 4:41
  • @Quetzalcoatl How do you enter new lines in -m ? Commented Jul 23, 2021 at 14:48
  • @KorayTugay: it's a good question but i don't know. generally, i would discourage long commit messages (e.g. with new lines) and encourage one sentence messages. brief and crisp commit messages tend to be more insightful here Commented Jul 26, 2021 at 20:47
  • @Koray Tugay after first double quotation marks, you press Enter and commit command does not end Commented Jan 1, 2023 at 3:44
  • Actually, that depends on your shell. On Unix-like systems, most shells behave like you describe, but on Windows, you probably need to sacrifice to Azathoth and/or install some third-party tools from a sunken continent. Commented Nov 6, 2023 at 7:17

15 Answers 15

446

Save the file in the editor. If it's Emacs: CTRLX CTRLS to save then CTRLX CTRLC to quit or if it's vi: :wq

Press esc first to get out from editing. (in windows/vi)

Sign up to request clarification or add additional context in comments.

6 Comments

The user needs to close the editor in addition to saving the file, so for Emacs it's actually Ctrl-X Ctrl-C.
Updated... I knew that, but like most emacs commands, they're so ingrained in me that I forget what they are... I just do them.
If you use emacsclient, you can close it with C-x #
On my window, I am surprised Ctrlx-x command is enough to abort commit.
what is emacs? The OP asks for windows... just ESC does not help, the answer is incomprehensible, don't know why it was marked as answer
|
343

Had troubles as well. On Linux I used Ctrl+X (and Y to confirm) and then I was back on the shell ready to pull/push.

On Windows GIT Bash Ctrl+X would do nothing and found out it works quite like vi/vim. Press i to enter inline insert mode. Type the description at the very top, press esc to exit insert mode, then type :x! (now the cursor is at the bottom) and hit enter to save and exit.

If typing :q! instead, will exit the editor without saving (and commit will be aborted)

5 Comments

this is the answer for me for windows
esc :x! - This isn't just undiscoverable its as if someone was playing hide and seek with functionality.
Work perfectly on windows Git Bash, this is the answer for me.
Nothing here is specific to Linux or WIndows or Mac. It sounds like you had configured nano (?) as your editor on Linux, and had not configured an editor on Windows, which caused git to choose its default, which is vi
^ for Mac users.
119

After writing commit message, just press Esc Button and then write :wq or :wq! and then Enter to close the Unix file.

3 Comments

this solution is the one that work on Git Gui bash for window
! This reminded me of the University days, when we did some Unix vi editing things, after a long wrote some :wq commands
:x is a shortcut for :wq in Vim.
31

Better yet, configure the editor to something you are comfortable with (gedit as an example):

git config --global core.editor "gedit" 

You can read the current configuration like this:

git config core.editor 

You can also add the commit message from the command line.

git commit -m "blablabla" 

and the editor will not be opened in the first place.

1 Comment

git config --global core.editor "code --wait" for VSCode. Source
27

After git commit command, you entered to the editor, so first hit i then start typing. After committing your message hit Ctrl+c then :wq.

2 Comments

You should edit your commit message. Then follow above. Solution works for windows.
Nothing here is specific to Windows. This is how you save and exit if your editor is vi or a derivative.
23

In Mac, Press shift+Z shift+Z (capital Z twice).

3 Comments

This is not Mac-specific, but editor-specific. In vi, this will abort without saving the file.
From :help ZZ: Write current file, if modified, and close the current window (same as ":x"). If there are several windows for the current file, only the current window is closed.
this worked for me on a Windows laptop in the Visual Studio Code console when I was trying to amend a git commit message
5

Alternatives to Nano (might make your life easier):

On Windows, use notepad. In command prompt type:

git config core.editor notepad

On Ubuntu / Linux, use text editor (gedit). In terminal window type:

git config core.editor gedit

1 Comment

Gedit is a rather lackluster editor; you should probably find one you like, and install that instead, and configure it as your default editor.
5

You Just clicking the key.

first press ESC + enter and then press :x + enter

1 Comment

Another redundant vi answer.
3

I had this problem I received a ">" like prompt and I couldn't commit. I replace the " in the comment with ' and it works.

I hope this help someone!

1 Comment

Aha! I had gotten stuck with this ">" prompt too. From reading your comment I figured out that since I had used an apostrophe in a contraction in my comment, the terminal was waiting for me to close out the quote! Finally I'm free!
1

I encountered the similar issue just in case this helps you.

When you hit the command git commit --amend. It opens a default editor. Now, the question was how to close this. I have just resolved this so here it is if it helps:

press Ctrl + X

Press Y to select Yes

Press Ctrl + M + A (This command saves the commit message you are editing and brings you out of editor)

Try git log command to verify your changes

1 Comment

This seems to be advice for users of the nano editor.
0

Not sure the key combination that gets you there to the > prompt but it is not a bash prompt that I know. I usually get it by accident. Ctrl+C (or D) gets me back to the $ prompt.

1 Comment

It's the shell's secondary prompt, which is displayed if you are in a multi-line string (i.e. you typed an opening single or double quote, and have not yet typed the corresponding closing quote).
0

Note that if you're using Sublime as your commit editor, you need the -n -w flags, otherwise git keeps thinking your commit message is empty and aborting.

Comments

0

Somehow my terminal on Mac opens Joe editor when git commit. I had to press Ctrl + K H to get to the help, this informed me to press Ctrl + K + X to exit.

Comments

0

What exactly works depends on which editor you are in. Many of the answers here explain how to exit vi or vim (Esc followed by :wq at the prompt to save the file and exit), with some other editors thrown in; but none of them really discuss how you would know.

Git out of the box examines your VISUAL environment variable. If this is unset, it will default to vi on Unix-like platforms.

export VISUAL=nano 

to select nano instead. (It's not an editor I particularly like or recommend, even though it was designed specifically to be easy for beginners to understand and learn.) Obviously, if you prefer emacsclient or sublime or code, put that after the equals sign instead. No spaces on either side of the equals sign! Put this in your shell's startup file (.bash_profile or .zprofile or .profile or etc, depending on which shell you are using) to make this setting the default in new terminal instances.

Another common scenario is if you started typing in a multi-line string at the shell prompt.

bash$ git commit -m "here is my commit message > oops, I forgot the final quote > how do I fix this? > haaaaaaalp 

At this point, you can press ctrl-C to give up and start over. The > prompt is the shell's secondary prompt (it can be configured by changing the value of PS2, but most people don't) which is displayed when you are in the middle of a quoted string. If you want to commit with the message you already typed, simply add the missing final single or double quote:

bash$ git commit -m 'small changes > ' [main 0badd09] 736 files changed, 136756 insertions(+) bash$ _ 

Just to reiterate, Git and your editor are two different components (and so is your OS, your shell, your terminal, your programming language(s), etc). When you first get started, it's probably confusing and distracting to figure out which part controls which behavior. Down the line, this is how you end up with an extremely flexible and customizable and thus extremely efficient personal environment.

(On Windows, Git commonly comes bundled with Bash because at the time they started to figure things out, there was no sane native shell for Windows. These days, things are slightly different, to the extent that "sane" and Windows can be used in the same sentence without negations or complex qualifiers, but it probably doesn't make sense to go back and start over with a different baseline.)

Comments

0

On Windows, I had to press Ctrl+c first and then do the standard Vim editor actions like i to enter into insert mode and then Escape+ :wq to close the editor.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.