git 如何从命令行同时使用“消息”和“描述”提交更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16122234/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:00:31  来源:igfitidea点击:

How to commit a change with both "message" and "description" from the command line?

gitgithub

提问by whatyouhide

I'm new to both gitand GitHub. I managed to set up everything locally on my Mac, so that now I can push commits to GitHub via git(on the command line, not the Mac app).

我是gitGitHub 和 GitHub 的新手。我设法在我的 Mac 上本地设置了所有内容,以便现在我可以通过git(在命令行上,而不是在 Mac 应用程序上)将提交推送到 GitHub 。

When I push commits directly from the GitHub web interface (e.g. quickly fixing a typo), I have the chance to "comment" the commit, and GitHub gives me a commit titleand a commit description. I find this very useful.

当我直接从 GitHub 网络界面推送提交时(例如快速修复错字),我有机会“评论”提交,GitHub 会给我一个提交标题和一个提交描述。我觉得这非常有用。

Still, when I git pushfrom the local machine, gitopens my default editor: so I write the commit comment, and then GitHub automatically divides it into title and "body". Is there a way to pretty comment commits from terminal too?

尽管如此,当我git push从本地机器git打开我的默认编辑器时:所以我写了提交评论,然后 GitHub 自动将其分为标题和“正文”。有没有办法从终端提交漂亮的评论?

回答by zzlalani

There is also another straight and more clear way

还有一种更直接更清晰的方式

git commit -m "Title" -m "Description ..........";

回答by Mikhail

Use the git commitcommand without any flags. The configured editor will open (Vim in this case):

使用git commit没有任何标志的命令。配置的编辑器将打开(在本例中为 Vim):

enter image description here

在此处输入图片说明

To start typing press the INSERTkey on your keyboard, then in insert mode create a better commit with description how do you want. For example:

要开始输入,请按INSERT键盘上的键,然后在插入模式下创建一个更好的提交,并带有您想要的描述。例如:

enter image description here

在此处输入图片说明

Once you have written all that you need, to returns to git, first you should exit insert mode, for that press ESC. Now close the Vim editor with save changes by typing on the keyboard :wq(w - write, q - quit):

一旦你写了所有你需要的东西,要返回到 git,首先你应该退出插入模式,然后按ESC。现在通过在键盘上输入:wq(w - write, q - quit)来关闭 Vim 编辑器并保存更改:

enter image description here

在此处输入图片说明

and press ENTER.

并按ENTER

On GitHub this commit will looks like this:

在 GitHub 上,此提交将如下所示:

enter image description here

在此处输入图片说明

As a commit editor you can use VS Code:

作为提交编辑器,您可以使用VS Code

git config --global core.editor "code --wait"

From VS Code docs website: VS Code as Git editor

来自 VS Code 文档网站:VS Code 作为 Git 编辑器

Gif demonstration: enter image description here

Gif 演示: 在此处输入图片说明

回答by Yuval Adam

git commit -a -m "Your commit message here"

will quickly commit all changes with the commit message. Git commit "title" and "description" (as you call them) are nothing more than just the first line, and the rest of the lines in the commit message, usually separated by a blank line, by convention. So using this command will just commit the "title" and no description.

将使用提交消息快速提交所有更改。Git 提交“标题”和“描述”(如您所称)只不过是第一行,提交消息中的其余行通常由一个空行分隔,按照惯例。所以使用这个命令只会提交“标题”而没有描述。

If you want to commit a longer message, you can do that, but it depends on which shell you use.

如果您想提交更长的消息,您可以这样做,但这取决于您使用的 shell

In bash the quick way would be:

在 bash 中,快速的方法是:

git commit -a -m $'Commit title\n\nRest of commit message...'

回答by tashuhka

In case you want to improve the commit message with header and body after you created the commit, you can rewordit. This approach is more useful because you know what the code does only after you wrote it.

如果您想在创建提交后改进带有标题和正文的提交消息,您可以改写它。这种方法更有用,因为您只有在编写代码之后才知道代码做了什么。

git rebase -i origin/master

Then, your commits will appear:

然后,您的提交将出现:

pick e152ce2 Update framework
pick ffcf91e Some magic
pick fa672e1 Update comments

Select the commit you want to reword and save.

选择要改写并保存的提交。

pick e152ce2 Update framework
reword ffcf91e Some magic
pick fa672e1 Update comments

Now, you have the opportunity to add header and body, where the first line will be the header.

现在,您有机会添加标题和正文,其中第一行将是标题。

Create perpetuum mobile

Redesign laws of physics with a pinch of imagination. Open a wormhole in 23 dimensions. Add protection to avoid high instability.