Write git commit message before 'git commit'

Viewed 9939

I'm learning Git coming from Perforce.

As far as I can tell you must write the commit message in the same step as when you commit. Or am I missing how I might write the message earlier and have it hang around until I'm ready to commit.

I really liked the workflow in perforce where you can edit the changelist description at any time, and then checkin when you're ready. Personally, I like to open the description many times and document as I code, or as I think of noteworthy things to point out.

Possible with Git?

11 Answers

An alternative to the -t <file> answer, if you plan to use it every single time, is to set :
git config commit.template <file>.
This will implicitely use -t on every commit.

Another simple option is to write a git commit with no changes, and amend that:

$ git commit --allow-empty
# create message
$ git commit --allow-empty --amend
# edit message

You could create an alias:

$ git config --global alias.draft 'commit --allow-empty'
$ git draft
# create message
$ git draft --amend
# edit message

This has all the benefits of git commits:

  • You will get "backups" in reflog in case you make a mistake.
  • You can add files to index and --amend them to the commit as usual.
  • You can push the draft message to your feature branch.

To add to MatrixFrog's answer, the GitKraken GUI (https://support.gitkraken.com/working-with-commits/commits/) provides similar functionality. It allows to draft the commit message inside the GUI before/while implementing the actual changes.

In addition, it allows to set a template to structure the commit body, e.g.:


changes:

foo

--

new tests:

bar

Related