The "git commit" command has, like most git commands, a large number of options that you can pass to modify the way the command is performed. I'll just be showing you two now, but don't forget that if you want to know what options you have, you can type "git help ".

When I type "git help commit", I'm shown the manual page for the "commit" command. As I scroll down, you'll see there are a ton of options that can be passed when you do a commit. I'll just be demonstrating the "-m" and "-a" options, because they are arguably the most commonly used options. I use them just about every time I use Git.

I'll press the "q" key to quit out of the Git manual.

Writing the Commit Message In-line with the "-m" Option.

There is a faster way to write your commit message when performing a commit. The "-m" option allows you to write the message in-line with the "commit" command, instead of first opening the message interface, writing the message, and saving and quiting the message interface.

I'll add a few files to the "sites/all" directory that I typically include in each Drupal site I build. These include:

  • sites/all/modules/contrib/README.txt
  • sites/all/modules/custom/README.txt
  • sites/all/modules/features/README.txt
  • sites/all/libraries/README.txt
  • sites/all/drush/README.txt

Now, when I type "git stats" we'll see the new directories. (Note: like before, we don't see the individual files. When a new directory is detected, the directory will be listed instead of each file inside the directory. This is because there could be hundreds of files, and you'll typically want to add all files in a new directory. If you want to select only specific files inside the directory, you have to know the explicit paths, and type them in yourself. Once a single file is added explicitly, however, the rest will be listed here individually, since you've already expressed that you want track individual files as opposed to the entire directory.)

Okay, so let's use the "-m" option to add our commit message in-line with the commit command.

Before, we simply typed "git commit" and were taken to the commit message interface to add our message, which is actually very useful when you need to add a long or multi-lined message. If instead you're just going to add a brief message, you can simply type "git commit -m ", and you won't have to use VI at all for your commit message.

I'll type "git add ." to add all of the new files to the staging area. (To verify they are staged, I'll type "git status".)
Now, I'll type "git commit -m "Created drush, libraries, and separate module directories"" and press Enter.

When that's done, we get a message like before that shows the commit message, files changed and line insertions.

This can make committing code much faster than having to write your commit messages in VI. And if we run "git status", we'll see the message "nothing to commit, working directory clean", which tells us that the new files are under version control.

Automatically Staging Modified Files with the "-a" Option.

When you edit an existing file it will not be committed with the staged files unless you first manually add it to the list of staged files. To demonstrate, I'll update the "sites/all/modules/README.txt" file with my own information about the "contrib", "custom" and "features" directories that I created.

I'll type "vi sites/all/modules/README.txt" and press Enter.

In this file, you'll actually notice the second paragraph mentions that it's okay to organize modules into subdirectories like "contrib" and "custom". I'll actually delete everything in this file and write my own information. (I'll delete everything after my cursor by typing "dG" (d Shift-G).)

Now, I'll press "i" to enter insert mode, and type

Do not place modules directly in this directory (sites/all/modules).

Instead, place:

contributed modules (from drupal.org) in "sites/all/modules/contrib",
custom modules in "sites/all/modules/custom" and
Feature modules (created with the Features module) in "sites/all/modules/features".

Then, I'll press the escape key, type ":wq" and press Enter.

Now, when I type "git status" we'll see the "README.txt" file under the "Changes not staged for commit" heading, and the message at the bottom that says "no changes added to commit (use "git add" and/or "git commit -a")". So, let's do that, we'll use the "-a" option with the "git commit" command.

First, I'll show you what would happen if we tried a standard commit without the "-a" option.

I'll type "git commit" and press Enter.

When I do that, I get the same message that says "no changes added to commit" because the modified file wasn't staged when I tried to commit it. I could use "git add" and then "git commit", but let's use the "-a" option to do it all in one command.

I'll type "git commit -a" and press Enter.

Now, I'm taken to the message interface where I can type my message. I'm actually going to cancel this because I want to show you that you can use the command "git commit -am " to stage all modified files and write your commit message all at once.

So, since I'm in VI, I'll type ":q" and press Enter. When I do that, you'll see the message "Aborting commit due to empty commit message." so, nothing was committed, and I can verify that by typing "git status".

Note: This technique automatically stages all modified files. Meaning that if I deleted 10 files, and made edits to another 10, all 20 files would be committed at once when I passed the "-a" option.

Using the "-m" and "-a" Options at the Same Time

Now that you know how to add your message with the "-m" option and how to stage all modified files with the "-a" option, its useful to know that you can combine these options and simply type "-am" to do both at the same time.

I'll type "git commit -am "Updated the sites/all/modules/README.txt file with info about sub-directories."" and press Enter.

When I do that, I see the commit message, that 1 file was changed and that there were 7 lines inserted and 16 lines deleted.