How to Write Git Commits With a Commit Message

Git uses commits to store changes to a codebase in a way such that any individual commit can later be undone or replayed.

If you want to change the layout of your website, you would make the code change and create a commit to save the change. Later if you realized you accidentally broke other parts of the layout, you could easily revert the commit to get back to the original layout.

Git relies on commit messages to describe the change each commit is responsible for.

Whenever you ship a commit you should include a message. Here's how.


Writing Commit Messages

There are two popular ways of saving commits. Each will have you input your commit message in a different way.

AWS Made Easy
AWS Made Easy
How to learn AWS quickly and easily.
AWS Made Easy
LEARN MORE

I'd recommend one of these options:

  • git commit -m <commit message>
  • git commit

The -m Flag

If you decide to use the -m flag you can write your entire commit message out in this command and when you hit enter, your commit and message will be instantly saved.

It works like this:

git commit -m "Changing landing page layout"

I recommend this for most developers as it's very fast and can be significantly less to remember if you aren't familiar with Vim.

The only downside is that given you are typing a message in the command line, it can be annoying to have to jump back to earlier words and edit the message before submitting it. But that's really not that big of a deal.

The Default Commit Command

If you don't apply any flags to the command, git commit will launch Vim in your terminal to type a commit message.

Vim is a code editor that can be confusing if you aren't familiar with how it works.

At a high level, it uses shortcuts to do everything and as a result by default doesn't even let you use a mouse. You type to navigate around a document and do various tasks. I love it, but I totally get that it's not for everyone.

Since you navigate by typing in Vim, it relies on having different modes to determine whether each letter typed should navigate you or be added to the document as text.

At the very least, you will need to understand visual mode (allowing you to navigate a file) and insert mode (allowing you to insert new text in the file).

When using the default git commit command, you can enter your commit message through Vim using these steps:

  1. Immediately type i to enter Insert Mode which allows you to type and insert your commit message
  2. Hit esc once done to exit insert mode
  3. Type :wq to save your commit and exit Vim

Don't worry if this sounds complicated, you can definitely rely on the git commit -m option.

And boom, we've shipped our first commit!

Featured
Level up faster
Hey, I'm Nick Dill.

I help people become better software developers with daily tips, tricks, and advice.