84

Is it possible to prepare a commit message prior to a commit, meaning that I will type my commit message before the actual commit or during the working dir so that I know what I am working on and I know what this branch or commit will be all about. When I say "before" I do not mean just couple secs before I enter the commit on the commandline. I literally mean right after a commit or at the starting of a branch so that the next commit will automatically inherit the message in the queue or what ever that might be called.

Naturally I can put these messages during the commit, to me there is a difference. And I can see the argument of well git is not meant for that as well. I am just curious about it.

I also know I can give my branches more meaningful names but I just want a bit space for such purpose.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
yarun can
  • 2,871
  • 5
  • 25
  • 27
  • 1
    A bit late to the party, but about branch descriptions; check this out https://stackoverflow.com/questions/2108405/branch-descriptions-in-git – jsageryd Mar 17 '18 at 18:30

6 Answers6

132

Git can take the commit message from a file using the -F or --file flags:

git commit -F message.txt

You can prepare your message in advance in a text file and use that file when you commit.

If you do this often, it makes sense to create an alias for it, for example:

done = commit -F message.txt

So that you can simply type git done to have it always use your text file.

If you make a mistake and commit too fast without updating the message file, not a problem, you can just do git commit --amend and fix the message in the commit.

UPDATE

The -e flag is useful too, as it lets you edit the message before committing:

git commit -eF message.txt
janos
  • 120,954
  • 29
  • 226
  • 236
  • That looks like what I need probably. I am accepting your answer. However if there are more ways to achieve it I am personally ready to hear. – yarun can Dec 07 '13 at 06:25
  • janos is it possible to create a note txt based on the branch? For instance I would like to be able to "git checkout -b mynewbranch" and then this command creates the branch plus the "mynewbranch.commit" text file? I tried somethign with aliases but not achieving it. Any solutions? – yarun can Dec 08 '13 at 08:19
  • 1
    @yaruncan That's really an entirely different question... But anyway, this alias could be what you're looking for or close enough: `newb = "!f() { git checkout -b $1 $2 && echo hello > $1.commit; }; f"` – janos Dec 08 '13 at 08:27
  • thanks. It was not really an entirely different question because that way I can have my text file to pipe into the commit message with every branch, where I can fill with the details of what the branch will be about. I will try your solution – yarun can Dec 08 '13 at 16:14
  • 1
    @yaruncan I just discovered something new: the `-e` is very useful to edit the message before the commit is performed. I updated my answer with this extra info. – janos Dec 14 '13 at 14:03
  • Great. Is there a way that the file given after -eF will be automatically deleted/ignored by Git? Or should I add a line to `.gitignore`? – iago-lito Dec 10 '15 at 08:46
  • 1
    Works fine, but lines starting with # are not ignored. – Radon8472 Feb 06 '18 at 22:36
  • Seems like # lines are not ignored, why is that?? – Danijel Apr 20 '21 at 08:35
17

If using the --file option for git commit you can pipe in the message through the standard input by using dash (-) instead of a file name.

echo "Classy commit message" | git commit --file -
Maic López Sáenz
  • 10,385
  • 4
  • 44
  • 57
  • Well thanks but this does not address the question, this is just another way of commiting a message, good to know however it is not a solution in anyway to my question unless you can show me how this can be used to solve my issue. – yarun can Dec 08 '13 at 07:24
  • 4
    Well, the point the message being piped is that the source of said message can be any program. It could be from simply printing the contents of a file to having them be compiled from any imaginable source. – Maic López Sáenz Dec 10 '13 at 06:32
  • This will have an undesired effect when the left hand side of the pipe contains multiple lines. – Richard A Quadling Dec 02 '19 at 12:05
2

If you're using VIM as Git's core editor, then when you run git commit, VIM will be opened and you'll be presented with a buffer containing the commented-out output of the git status command.

You can then use the VIM command :read COMMIT_MSG.txt which will insert the contents of the file COMMIT_MSG.txt (your pre-pared commit message) at the current cursor location.

This is really just an alternative to running git commit -eF COMMIT_MSG.txt, but I personally find it easier to remember the VIM command :read as opposed to having to remember yet another git command line argument. Personal preference, really.

sversch
  • 856
  • 8
  • 22
2

You can also get git's vim formatting (excluding the comment lines that usually list the included/excluded changes) by using this command:

vi -c 'set syntax=gitcommit'

I made an alias (non-git) and added it to my ~/.bashrc.

alias ecm="vi -c 'set syntax=gitcommit'"

Re-source your .bashrc to turn it on for your current session.

source ~/.bashrc

And then use it.

ecm message.txt

All this was done with Git Bash on Windows.

Chris
  • 3,400
  • 1
  • 27
  • 41
1

You could add a template file to your project's git configuration and its content will be used each time you start a new commit. This file might be added to .gitignore so that its existence will be only relevant for you while developing. Standing in the root of your project, you could for example do:

touch .gitmessage
echo "\n# commit message\n.gitmessage" >> .gitignore
git config commit.template .gitmessage

Hope this helps someone else in the future!

tebanep
  • 625
  • 1
  • 9
  • 11
  • 2
    Perfect! No alias needed. Review/edit at commit. Supports # comments. Git still adds changed file list at the end. Works with configured editor. Follows .gitX special file naming scheme. And.. you can plan ahead using comments and uncomment/edit the appropriate pieces for the current commit. – SensorSmith Dec 27 '22 at 17:52
0

I combined the answers of LopSae and janos to auto remove all lines beginning with a # from the input-textfile with command from this post.

Result is this command

cat .git/COMMIT_EDITMSG | sed '/^#/ d' | git commit -F -

And to make it shorter I suggest to add an alias.

EDIT Creating an alias for this command was more complex than exspeced. But after some tries and withs this tuturial I created an alias what works and supports custom parameters.

[alias]
  commitmsg  = "!f() { myarg=${@}; cat .git/COMMIT_EDITMSG | sed '/^#/ d' | git commit -F - $myarg;  }; f"

You can use it e.g. with parameter for modified date like this

commitmsg --date "2001-01-02 18:00:00"
Radon8472
  • 4,285
  • 1
  • 33
  • 41