Setting default commit template in Git

Well-formed commit messages can be rather helpful in terms of tracking the history or generating the changelog automatically. Here we will configure a commit template to help writing commit messages in a good format. You can force it across the whole team.

Configure Git commit message template

Write you own commit template file, such as ~/.gitmessage. Below is a good example :

<type>: subject (try to keep under 50 characters)

Multi-line description of commit to explain
what, why and how this change is being made
(try to limit each line under 72 Characters)

Provide ticket numbers or links to other relevant resource
Example:
[Ticket: #53]

This template structure contains 3 sections:

  1. head, a subject line starting with a commit type, for example feat: add settings page (followed by a blank line).
  2. body, explains the commit.
  3. foot, optional, provides tickets or other information.

Setup it as the default commit template with below command:

# Config a custom commit template
$ git config --global commit.template ~/.gitmessage

When you run git commit command, an editor opens containing something like this.

Subject line (try to keep under 50 characters)

Multi-line description of commit to explain
what, why and how this change is being made
(try to limit each line under 72 Characters)

Provide links or ids to relevant issues or other resources
Example:
[Ticket: #53]
# Please enter the commit message for your changes. Lines starting
# with '' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   my-plugin.php
#

Then replace the 3 parts with your actual content.

Commit types

The commit types known to developers that you can set:

Commit Type Description
feat New feature.
fix Fix a bug.
refactor Refactor production code.
style Format issues like missing semi colons, etc, no code change.
docs Change documentation files.
test Add or refactor tests, no production code change.
chore Update grunt tasks, no production code change. Some examples of grunt tasks: configuration changes (.gitignore, .gitattributes), tool changes or something that would not go into the product.

You can put commit types in the template file to help to pick the proper commit type as Git commit template shows:

# <type>: (If applied, this commit will...) <subject> (Max 50 char)
# |<----  Using a Maximum Of 50 Characters  ---->|


# Explain why this change is being made
# |<----   Try To Limit Each Line to a Maximum Of 72 Characters   ---->|

# Provide links or keys to any relevant tickets, articles or other resources
# Example: Github issue #23

# --- COMMIT END ---
# Type can be
#    feat     (new feature)
#    fix      (bug fix)
#    refactor (refactoring production code)
#    style    (formatting, missing semi colons, etc; no code change)
#    docs     (changes to documentation)
#    test     (adding or refactoring tests; no production code change)
#    chore    (updating grunt tasks etc; no production code change)
# --------------------
# Remember to
#   - Capitalize the subject line
#   - Use the imperative mood in the subject line
#   - Do not end the subject line with a period
#   - Separate subject from body with a blank line
#   - Use the body to explain what and why vs. how
#   - Can use multiple lines with "-" for bullet points in body
# --------------------
# For updated template, visit:
# https://gist.github.com/adeekshith/cd4c95a064977cdc6c50
# Licence CC

Resources

Undoing add in Git

To undo git add action, run git restore --staged for a file or “git reset” for all files that have been added to index. git reset HEAD -- also works for unstaging a file, but take care to use git reset command for it may bring unexpected results if you do not use it correctly.

Unstage a file

If you add a file to staging area and want to undo the action, use below git restore command to unstage it:

# Syntax
# Undo add action for a file
#
# --, the following parameter will be parsed as a file path
$ git restore --staged -- <file-path>

# Examples:

# Undo add action for index.php
$ git restore --staged -- index.php

Unstage all files

If you want to undo add action for all the files that have been added to staging area, use git reset to do that:

$ git reset HEAD
# or
# If no commit is specified, it defaults to HEAD
$ git reset

If fact, git status will tell you how to unstage a file. Depending on your version of Git, you may see some content like this :

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   index.md

On older versions, it may prompt using git reset HEAD to unstage a file:

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:    index.php

git reset HEAD -- also work, git reset and git resotre has some overlapped function.

If you are confusing the three commands, revert, restore and reset, see their differences at the bottom of the post.

Resources

  • git restore

  • git reset

  • Reset, restore and revert

    There are three commands with similar names: git reset, git restore and git revert.

    • git-revert is about making a new commit that reverts the changes made by other commits.

    • git-restore is about restoring files in the working tree from either the index or another commit. This command does not update your branch. The command can also be used to restore files in the index from another commit.

    • git-reset is about updating your branch, moving the tip in order to add or remove commits from the branch. This operation changes the commit history.

    git reset can also be used to restore the index, overlapping with git restore.