Write git commit message before 'git commit'

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

Have a look at the -t <file> flag with git commit

This lets you specify a file to use as the basis for the commit message. The editor is still invoked but at least you can use a commit message which you create in advance.

Alternatively, there is another workflow that you can use with git that might better suit your way of working:

With git you can work on a separate branch from the main line and make lots of small commits with their own messages. Although each of these commits by themselves may not solve the problem that you are working on, they do provide a way of saving the intermediate states of your work with the same sort of messages that you may have been updating in your commit message file.

Once you are ready to commit the sum of your work, you can use the rebase command and squash these commits together. Your editor will then be invoked with the all the individual messages that you used for the smaller commits which you can then edit together into a single message.

This is a lot easier than it sounds here, and is IMHO a more git-like approach.

Solution 2 - Git

As long as you haven't push your commit to others, you can do a git commit --amend. This will allow you to modify your commit, as well as your commit message.

I found this really help with the 'commit early and often', without get overwhelm by the number of trivial commits.

Solution 3 - Git

You could use these aliases:

git config --global alias.prepare '!${EDITOR:-vi} $(git rev-parse --git-dir)/.template'
git config --global alias.commitp '!git commit -F $(git rev-parse --git-dir)/.template'

Usage:

git prepare
EDITOR=nano git prepare # heeds standard EDITOR variable
git commitp

This keeps your commit message in .git/.template.

But rather than this, you should really just use a workflow where you commit atomic and small changes often, and use feature branches when necessary to group those changes. If you merge with git merge --no-ff $branch, you can use git log --first-parent later to ignore the branches.

Solution 4 - Git

You can use git gui and just leave it open while you work. Write the commit message for the bugfix you're about to do, then do the actual code changes, stage it, and commit it.

Solution 5 - Git

I believe the motivation for this question is to be able to write the description (commit message) before writing any code (while of course being able to modify it as the code is written). (Having used Perforce and a Perforce-like system before, I know it helps sometimes to be in that frame of mind, where you write a description of what you're going to do, before actually writing the code to do it.)

Apart from writing the message in a file and using the -t <file> (--template=<file>) or -F <file> (--file=<file>) flags to git commit, another approach is the following:

  1. Make an empty commit with git commit --allow-empty. This will, just like any git commit, bring up an editor where you can write the message. Write it and finish the commit.

  2. Make your code changes.

  3. Add the files you want to add with git add and then git commit --amend (or just git commit -a --amend if you don't want to pick out specific files with git add). This will make the earlier non-empty commit now no longer empty, and you can also edit the message to more closely match what you actually did (if you prefer).

(If you're working with others, remember not to git push while doing this: don't amend commits you've already pushed!)

Of course the advice to keep your commits as small and atomic as possible still applies, but this way lets you write the message before writing the code. (The git commit --amend approach has already been suggested in another answer; I'm only additionally pointing out that you can go all the way using git commit --allow-empty.)

Solution 6 - Git

Write it in a file; keep it updated as you work. Include the finalized version when actually committing.

If you're using a graphical frontend for git, then you'll have to specify which so someone can help with it specifically. In general, you can simply paste the message.

Using git from a command line, it will open your editor with a temp file and you can read the message into it (e.g. :r filename in vim).

Or you could use your shell to read that file as the value for the -m parameter:

# bash example, may work elsewhere
git commit -m "$(<filename)"

Solution 7 - Git

Built in, not as far as I know. If you're really desperate though, you could write it in the Terminal like COMMIT="Fix for bug #14453", COMMIT="$COMMIT and bug #4329" then commit like git commit -m "$COMMIT".

Solution 8 - Git

Use the --file <path> argument for the commit command.

git commit --file <absolute or relative path to file>

And replace the <absolute or relative path to file> with the path to the file.

 

Example for a relative file in the directory above the repository directory:
git commit --file ../commit-message.txt

 

You can work on the commit message in any text editor and leave it open. Just save the file and then commit with a constant commit command. It takes the message from the text file (.txt) without opening a editor.

Solution 9 - Git

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.

Solution 10 - Git

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.

Solution 11 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionackView Question on Stackoverflow
Solution 1 - GitAbizernView Answer on Stackoverflow
Solution 2 - GitfsetoView Answer on Stackoverflow
Solution 3 - Gituser1338062View Answer on Stackoverflow
Solution 4 - GitTylerView Answer on Stackoverflow
Solution 5 - GitShreevatsaRView Answer on Stackoverflow
Solution 6 - GitRoger PateView Answer on Stackoverflow
Solution 7 - Git46bitView Answer on Stackoverflow
Solution 8 - GitBinarianView Answer on Stackoverflow
Solution 9 - GitEsteckaView Answer on Stackoverflow
Solution 10 - Gituser1338062View Answer on Stackoverflow
Solution 11 - GitSimon CouvreurView Answer on Stackoverflow