664

I can push commits to GitHub via git (on the command line, not the Mac app).

When I push commits directly from the GitHub web interface (e.g. quickly fixing a typo), I have the chance to "comment" the commit, and GitHub gives me a commit title and a commit description. I find this very useful.

Still, when I git push from the local machine, git opens my default editor: so I write the commit comment, and then GitHub automatically divides it into title and "body". Is there a way to pretty comment commits from terminal too?

1

4 Answers 4

1276

There is also another straight and more clear way

git commit -m "Title" -m "Description....";
10
  • 69
    Well, a nice hack, but I prefer the answer below because it provides me a way to type a long description with newlines, with ease.
    – John Red
    Commented Mar 7, 2017 at 6:57
  • 41
    I do as well ^ But I feel if you're doing commit after commit you don't want to open the VIM every time. Can you add \n into the description doing it this way? E.g git commit -m "Title" -m "Description....\nNew line....\nAnother new line
    – James111
    Commented Mar 13, 2018 at 22:00
  • 5
    why not @James111
    – zzlalani
    Commented Mar 19, 2018 at 7:08
  • 5
    Adding \n in the description to add a line break doesn't work. Refer this StackOverflow post. Commented Apr 5, 2021 at 20:34
  • 6
    @James111 If you're going to use \n you need to put it inside of $'' like this: $'test\ntext'
    – PQCraft
    Commented Oct 2, 2021 at 17:19
635

Use the git commit command without any flags. The configured editor will open (Vim in this case):

enter image description here

To start typing press the INSERT key on your keyboard, then in insert mode create a better commit with description how do you want. For example:

enter image description here

Once you have written all that you need, to returns to git, first you should exit insert mode, for that press ESC. Now close the Vim editor with save changes by typing on the keyboard :wq (w - write, q - quit):

enter image description here

and press ENTER.

On GitHub this commit will looks like this:

enter image description here

As a commit editor you can use VS Code:

git config --global core.editor "code --wait"

From VS Code docs website: VS Code as Git editor

Gif demonstration: enter image description here

2
  • I think theres a little change while saving your commit just press ctrl+x it will prompt you to save by pressing y and then hit enter and git push should do the job Commented Oct 18, 2019 at 4:39
  • 2
    without any flags - well, there are some flags you do can use, such as --all --no-verify and more.. (Haven't check but probably all except -m)
    – Mosh Feu
    Commented Dec 16, 2019 at 17:50
86
git commit -a -m "Your commit message here"

will quickly commit all changes with the commit message. Git commit "title" and "description" (as you call them) are nothing more than just the first line, and the rest of the lines in the commit message, usually separated by a blank line, by convention. So using this command will just commit the "title" and no description.

If you want to commit a longer message, you can do that, but it depends on which shell you use.

In bash the quick way would be:

git commit -a -m $'Commit title\n\nRest of commit message...'
2
  • 19
    With a flag, git will stage all modified files and that is not what is usually desired. I would suggest removing a flag from your example. Commented May 24, 2021 at 4:22
  • @HarisMuzaffar I don't know about you, but that IS the behaviour that's "usually desired" in my workflow and I just learned about this flag because of this answer. :P Commented Jun 16, 2023 at 21:00
4

In case you want to improve the commit message with header and body after you created the commit, you can reword it. This approach is more useful because you know what the code does only after you wrote it.

git rebase -i origin/master

Then, your commits will appear:

pick e152ce2 Update framework
pick ffcf91e Some magic
pick fa672e1 Update comments

Select the commit you want to reword and save.

pick e152ce2 Update framework
reword ffcf91e Some magic
pick fa672e1 Update comments

Now, you have the opportunity to add header and body, where the first line will be the header.

Create perpetuum mobile

Redesign laws of physics with a pinch of imagination. Open a wormhole in 23 dimensions. Add protection to avoid high instability.
1
  • 7
    Your approach certainly works, but only in quite specific case. You can't do this if you already pushed your commits (without some dark magic :) ), and more importantly, it doesn't trully fit the OP's described situation.
    – Tin
    Commented Aug 7, 2018 at 9:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.