30

I am using Git Bash and am trying to figure out what is happening when I type 'git commit -a'.

Looks like VIM opens up to edit my commit message but how do I save and actually complete this commit? I type in the editor and hit enter but it just creates another line.

FYI: I am using VM Fusion on my mac so some of my keys might be a little different

1

4 Answers 4

42

this is a question about VIM and not git itself (if i'm not mistaken)

VIM uses multiple modes, to exit insert mode hit ESC or ^C. to save and exit the file use ZZ, :x or :wq

just to be complete: git commit -a will first add all tracked and changed files' contents to the index and then creates a commit (after specifying the commit message inside vim – or your editor of choice)

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

2 Comments

right, I was just trying to save a step and understand how to work in vim - thanks
You just fixed an hour of headaches for me.. lol Thanks. the ESC did the trick ;)
26

Just leave VIM (command mode -> :wq).

-a flag works like:

git add -u
git commit ...

-m flag specifies commit message. That message is mandatory so if you don't pass that (git commit -m "My message") the default text editor will be opened (you can change it in git configuration) and you'll have to write a commit message, then save and quit the editor.

4 Comments

git commit -a does not work like git add ., it works like git add -u
"then save and quit the editor" NOTE: Exiting the editor saves the commit message.
-1 "just leave vim" may be the most unhelpful instruction ever. You might as well tell the Minotaur to "just leave the maze"
Can you explain what 'git add -u' does? I'm new to git and am extremely confused on what that does.
14

To save your commit you save and exit the file as you normally would in vim.

:wq

If you change your mind and want to abort you exit without saving.

:q!

You don't have to use VIM though, you can set the editor, for example the following would change the editor git uses to textmate.

git config --global core.editor textmate

Comments

3

As others have said, Vim is being opened so you an add a message with information about the changes you're committing. It seems the default editor on your system is Vim. Git is set up so that it will include whatever message you type and save in Vim, after you close Vim with :wq, :x, or some other command.

In addition to method Andrew Myers suggests of changing the editor being called by Git, you can include a simple message on the command line with something like this:

git commit -am 'This is my message for the commit. . . '

If you do it that way Git will not bother you by opening a text editor at all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.