简体   繁体   English

在 'git commit' 之前写入 git commit 消息

[英]Write git commit message before 'git commit'

I'm learning Git coming from Perforce.我正在学习来自 Perforce 的 Git。

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.我真的很喜欢 perforce 的工作流程,您可以随时编辑更改列表描述,然后在准备好后签入。 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 吗?

Have a look at the -t <file> flag with git commit使用git commit查看-t <file>标志

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:或者,还有另一个工作流程可以与 git 一起使用,它可能更适合您的工作方式:

With git you can work on a separate branch from the main line and make lots of small commits with their own messages.使用 git,您可以在与主线不同的分支上工作,并使用自己的消息进行大量小提交。 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.一旦你准备好提交您的工作的总和,可以用rebase命令和squash这些提交在一起。 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.这比听起来容易得多,恕我直言,这是一种更像 git 的方法。

As long as you haven't push your commit to others, you can do a git commit --amend .只要您没有push您的提交push送给其他人,您就可以执行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.我发现这对“尽早和经常提交”真的很有帮助,而不会被琐碎的提交数量所淹没。

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 .这会将您的提交消息保存在.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.如果您与git merge --no-ff $branch ,则可以稍后使用git log --first-parent来忽略分支。

You can use git gui and just leave it open while you work.您可以使用git gui并在工作时将其保持打开状态。 Write the commit message for the bugfix you're about to do, then do the actual code changes, stage it, and commit it.为您将要进行的错误修复编写提交消息,然后进行实际的代码更改、暂存并提交。

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.) (之前使用过 Perforce 和类似 Perforce 的系统,我知道有时在这种心态下会有所帮助,在实际编写代码之前,先写下你将要做的事情的描述。)

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:除了将消息写入文件并使用-t <file> ( --template=<file> ) 或-F <file> ( --file=<file> ) 标志到git commit ,另一种方法如下:

  1. Make an empty commit with git commit --allow-empty .使用git commit --allow-empty进行空提交。 This will, just like any git commit , bring up an editor where you can write the message.就像任何git commit ,这将打开一个编辑器,您可以在其中编写消息。 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 ).使用git add要添加的文件,然后使用git commit --amend (如果不想使用git add挑选特定文件,则只需使用git commit -a --amend )。 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!) (如果您正在与其他人一起工作,请记住在执行此操作时不要git push :不要修改您已经推送的提交!)

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 .) (在另一个答案中已经建议了git commit --amend方法;我只是另外指出,您可以一直使用git commit --allow-empty 。)

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.如果您为 git 使用图形前端,那么您必须指定哪个,以便有人可以专门帮助它。 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 (eg :r filename in vim).从命令行使用 git,它将打开带有临时文件的编辑器,您可以将消息读入其中(例如:vim 中的 :r 文件名)。

Or you could use your shell to read that file as the value for the -m parameter:或者您可以使用 shell 读取该文件作为 -m 参数的值:

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

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" .如果你真的很绝望,你可以像COMMIT="Fix for bug #14453"COMMIT="$COMMIT and bug #4329"一样在终端中编写它,然后像git commit -m "$COMMIT"一样git commit -m "$COMMIT"

Use the --file <path> argument for the commit command.commit命令使用--file <path>参数。

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.它在不打开编辑器的情况下从文本文件 (.txt) 中获取消息。

An alternative to the -t <file> answer, if you plan to use it every single time, is to set :如果您打算每次都使用它,那么-t <file>答案的替代方法是设置:
git config commit.template <file> . git config commit.template <file>
This will implicitely use -t on every commit.这将在每次提交时隐式使用-t

Another simple option is to write a git commit with no changes, and amend that:另一个简单的选择是编写一个没有更改的 git commit,并修改它:

$ 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:这具有 git commit 的所有好处:

  • You will get "backups" in reflog in case you make a mistake.如果您犯了错误,您将在 reflog 中获得“备份”。
  • You can add files to index and --amend them to the commit as usual.您可以像往常一样将文件添加到索引并--amend它们到提交。
  • 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.为了补充 MatrixFrog 的答案,GitKraken GUI ( https://support.gitkraken.com/working-with-commits/commits/ ) 提供了类似的功能。 It allows to draft the commit message inside the GUI before/while implementing the actual changes.它允许在实施实际更改之前/同时在 GUI 内起草提交消息。

In addition, it allows to set a template to structure the commit body, eg:此外,它还允许设置模板来构建提交主体,例如:


changes:变化:

foo

-- ——

new tests:新测试:

bar酒吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM