438

I'm new to both git and GitHub. I managed to set up everything locally on my Mac, so that now 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?

aniskhan001
  • 6,602
  • 8
  • 32
  • 52
whatyouhide
  • 13,582
  • 7
  • 51
  • 69

4 Answers4

888

There is also another straight and more clear way

git commit -m "Title" -m "Description ..........";
zzlalani
  • 19,534
  • 16
  • 41
  • 72
  • 40
    Well, a nice hack, but I prefer [the answer below](http://stackoverflow.com/a/36427485/3810038) because it provides me a way to type a long description with newlines, with ease. – John Red Mar 07 '17 at 06:57
  • 20
    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 Mar 13 '18 at 22:00
  • 4
    why not @James111 – zzlalani Mar 19 '18 at 07:08
  • Adding `\n` in the description to add a line break doesn't work. Refer [this StackOverflow post](https://stackoverflow.com/questions/5064563/add-line-break-to-git-commit-m-from-the-command-line). – Dhruv Saraswat Apr 05 '21 at 20:34
398

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

Sergioet
  • 792
  • 8
  • 23
Mikhail
  • 8,543
  • 7
  • 23
  • 45
  • 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 – Mahesh Jamdade Oct 18 '19 at 04:39
  • 1
    `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 Dec 16 '19 at 17:50
62
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...'
Community
  • 1
  • 1
Yuval Adam
  • 149,388
  • 85
  • 287
  • 384
  • 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. – Haris Muzaffar May 24 '21 at 04:22
0

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.
tashuhka
  • 4,524
  • 3
  • 40
  • 62
  • 3
    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. – Vaz Aug 07 '18 at 09:14