Reputation: 14985
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?
Upvotes: 72
Views: 12057
Reputation: 12755
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:
--amend
them to the commit as usual.Upvotes: 2
Reputation: 388
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.
Upvotes: 0
Reputation: 1
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
Upvotes: -1
Reputation: 12446
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.
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.
Upvotes: 0
Reputation: 39113
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:
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.
Make your code changes.
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
.)
Upvotes: 5
Reputation: 12755
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.
Upvotes: 7
Reputation: 22136
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.
Upvotes: 4
Reputation: 150735
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.
Upvotes: 77
Reputation: 10478
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.
Upvotes: 11
Reputation:
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)"
Upvotes: 3
Reputation: 1537
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"
.
Upvotes: 1