Skip to content

Instantly share code, notes, and snippets.

@mattantonelli
Last active May 31, 2024 14:20
Show Gist options
  • Save mattantonelli/4e587359360d2e83fcd84e95fdfa5aac to your computer and use it in GitHub Desktop.
Save mattantonelli/4e587359360d2e83fcd84e95fdfa5aac to your computer and use it in GitHub Desktop.
A Guide to Good Commit Messages

Good Commit Messages

A good commit message helps both you and your collaborators better understand your code. A series of good messages will create a well-documented history as your code evolves. This also makes it easier for someone to pick up where you left off.

Jump to:

  1. Summary
  2. Description
  3. Full message
  4. Branch
  5. Pull request
  6. References

The first line of a commit message is the summary AKA the title. This should be a summary of the commit which is 50 characters or less.

Bad

redirect

Good

Redirect user to the requested page after login

After the summary, you should leave one blank line, then write a more detailed description of your commit. The description should be no longer than 72 columns in width. It should provide a detailed explanation of each change you made in the commit. Formatting your description with the bulleted list markdown is a great way visibility.

If your commit is resolving an issue on Github, this is a good place to reference the issue number. For example, if you are resolving issue 42, you could add Resolves #42. If you copy this line into your pull request later, it will automatically close the issue when your PR is merged.

If your commit is lengthy (though you should strive for it to be short!) you can add helpful headers to separate the bullet points in your description. Headers like New features, Clean up, and TODO are useful.

Bad

redirect on login

Good

* Store requested path in a session variable
* Redirect to the stored location after successfully logging in the user
* Resolves #42

Good (with headers)

New features

* Store requested path in a session variable
* Redirect to the stored location after successfully logging in the user
* Resolves #42

Clean up

* Fixed indentation in the router

TODO

* Connect the back-end FTP capacitor

Here we combine the header and descriptions from earlier to give you an idea of the full commit message.

Bad

redirect

redirect on login

Good

Redirect user to the requested page after login

* Store requested path in a session variable
* Redirect to the stored location after successfully logging in the user
* Resolves #42

Good (with headers)

Redirect user to the requested page after login

New features

* Store requested path in a session variable
* Redirect to the stored location after successfully logging in the user
* Resolves #42

Clean up

* Fixed indentation in the router

TODO

* Connect the back-end FTP capacitor

Our commits have to go somewhere, and that's where the branch comes in. Whenever we want to add a new feature to our project, we create a new branch (usually off of master.) For example, a good branch name for our previous commit would be fix-login-redirect.

All commits on this feature branch should be related to fixing the login redirect issue. If we want to work on another feature, we can go back to our base and make another branch. For example, if we also notice an issue with the registration form, we would switch back to master and make another branch fix-registration-form.

When we are finished making commits on our feature branch, we need to open a pull request to have our changes merged in. The pull request should detail the changes we've made across various commits in our branch. Remembering everything we did on our branch can be difficult, but since we've been making good commit messages, our work is already done! All we need to do is copy our good commit messages into the PR, and add extra information where necessary. Add some nice markdown to format the pull request and we're good to go.

Remember we can reference and auto-close issues by adding a line like Resolves #42. We can also provide a link to an individual commit by referencing its 7 character SHA (e.g. 48e15af). In both cases, Github will automatically create links to the issue/commit for us.

Below are examples of well-formatted pull request using our commits from earlier. Don't forget to also add a descriptive title!

Example

##### Redirect user to the requested page after login 48e15af

* Store requested path in a session variable
* Redirect to the stored location after successfully logging in the user
* Resolves #42

Redirect user to the requested page after login 48e15af
  • Store requested path in a session variable
  • Redirect to the stored location after successfully logging in the user
  • Resolves #42

Example with headers

#### Redirect user to the requested page after login 48e15af
##### New features

* Store requested path in a session variable
* Redirect to the stored location after successfully logging in the user
* Resolves #42

##### Clean up

* Fixed indentation in the router

##### TODO

* Connect the back-end FTP capacitor

Redirect user to the requested page after login 48e15af

New features
  • Store requested path in a session variable
  • Redirect to the stored location after successfully logging in the user
  • Resolves #42
Clean up
  • Fixed indentation in the router
TODO
  • Connect the back-end FTP capacitor

https://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message

@schie
Copy link

schie commented May 19, 2016

👍

@patientsoxfan
Copy link

Nice job Matt!! Thanks for your help, putting it in Word. 🎊

@JustinMcNamara74
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment