在提交之前准备 git commit 消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20438293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Preparing a git commit message before committing?
提问by yarun can
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.
当然,我可以在提交期间放置这些消息,对我来说是有区别的。我可以看到 git 的论点也不是为了那个。我只是对此感到好奇。
I also know I can give my branches more meaningful names but I just want a bit space for such purpose.
我也知道我可以给我的分支更有意义的名字,但我只是想要一些空间来达到这个目的。
回答by janos
Git can take the commit message from a file using the -F
or --file
flags:
Git 可以使用-F
或--file
标志从文件中获取提交消息:
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.
这样您就可以简单地键入git done
以使其始终使用您的文本文件。
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.
如果你犯了一个错误并且提交太快而不更新消息文件,这不是问题,你可以git commit --amend
在提交中修复消息。
UPDATE
更新
The -e
flag is useful too, as it lets you edit the message before committing:
该-e
标志也很有用,因为它允许您在提交之前编辑消息:
git commit -eF message.txt
回答by LopSae
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.
如果使用该--file
选项,git commit
您可以使用破折号 ( -
) 而不是文件名通过标准输入管道输入消息。
echo "Classy commit message" | git commit --file -
回答by sversch
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.
如果您使用 VIM 作为 Git 的核心编辑器,那么当您运行 时git commit
,VIM 将被打开,您将看到一个包含注释掉的git status
命令输出的缓冲区。
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.
然后您可以使用 VIM 命令:read COMMIT_MSG.txt
,该命令将COMMIT_MSG.txt
在当前光标位置插入文件的内容(您准备好的提交消息)。
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.
这实际上只是 running 的替代方法git commit -eF COMMIT_MSG.txt
,但我个人发现记住 VIM 命令:read
比必须记住另一个 git 命令行参数更容易。个人喜好,真的。
回答by Radon8472
I combined the answers of LopSaeand janosto auto remove all lines beginning with a # from the input-textfile with command from this post.
我结合LopSae和janos的答案,使用这篇文章中的命令从输入文本文件中自动删除所有以 # 开头的行。
Result is this command
结果是这个命令
cat .git/COMMIT_EDITMSG | sed '/^#/ d' | git commit -F -
And to make it shorter I suggest to add an alias.
为了让它更短,我建议添加一个别名。
EDITCreating an alias for this command was more complex than exspeced. But after some tries and withs this tuturialI 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"
回答by Chris
You can also get git's vim formatting (excluding the comment lines that usually list the included/excluded changes) by using this command:
您还可以使用以下命令获取 git 的 vim 格式(不包括通常列出包含/排除更改的注释行):
vi -c 'set syntax=gitcommit'
I made an alias (non-git) and added it to my ~/.bashrc.
我创建了一个别名(非 git)并将其添加到我的 ~/.bashrc 中。
alias ecm="vi -c 'set syntax=gitcommit'"
Re-source your .bashrc to turn it on for your current session.
重新获取 .bashrc 以在当前会话中打开它。
source ~/.bashrc
And then use it.
然后使用它。
ecm message.txt
All this was done with Git Bash on Windows.
所有这些都是在 Windows 上使用 Git Bash 完成的。