git 命令commit
The git commit
command will save all staged changes, along with a brief description from the user, in a “commit” to the local repository.
git commit
命令将所有分阶段的更改以及用户的简短描述保存在“提交”到本地存储库中。
Commits are at the heart of Git usage. You can think of a commit as a snapshot of your project, where a new version of that project is created in the current repository. Two important features of commits are:
提交是Git使用的核心。 您可以将提交视为项目的快照,在当前存储库中创建该项目的新版本。 提交的两个重要特征是:
you can recall the commited changes at a later date, or revert the project to that version (see Git checkout)
您可以稍后撤回已提交的更改,或将项目还原到该版本( 请参阅Git checkout )
There are a number of options that you can include with git commit
. However, this guide will only cover the two most common options. For an extensive list of options, please consult the Git documentation.
git commit
可以包含许多选项。 但是,本指南仅涵盖两个最常见的选项。 有关选项的详细列表,请查阅Git文档 。
The most common option used with git commit
is the -m
option. The -m
stands for message. When calling git commit
, it is required to include a message. The message should be a short description of the changes being committed. The message should be at the end of the command and it must be wrapped in quotations " "
.
与git commit
一起使用的最常见选项是-m
选项。 -m
代表消息。 调用git commit
,需要包含一条消息。 该消息应该是对所做更改的简短描述。 该消息应该在命令的末尾,它必须被包裹在报价" "
。
An example of how to use the -m
option:
有关如何使用-m
选项的示例:
git commit -m "My message"
The output in your terminal should look something like this:
终端中的输出应如下所示:
[master 13vc6b2] My message
1 file changed, 1 insertion(+)
NOTE: If the -m
is not included with the git commit
command, you will be prompted to add a message in your default text editor - see ‘Using detailed commit messages’ below.
注意:如果-m
不包含在git commit
命令中,则将提示您在默认文本编辑器中添加一条消息-请参阅下面的“使用详细的提交消息”。
Another popular option is the -a
option. The -a
stands for all. This option automatically stages all modified files to be committed. If new files are added the -a
option will not stage those new files. Only files that the Git repository is aware of will be committed.
另一个流行的选项是-a
选项。 -a
代表所有。 此选项将自动暂存所有已提交的修改文件。 如果添加了新文件,则-a
选项将不会暂存这些新文件。 仅Git存储库知道的文件将被提交。
For example:
例如:
Let’s say that you have a README.md
file that has already been committed to your repository. If you make changes to this file, you can use the -a
option in your commit command to stage and add the changes to your repository. However, what if you also added a new file called index.html
? The -a
option will not stage the index.html
as it does not currently exist in the repository. When new files have been added, the git add
command should be invoked in order to stage the files before they can be committed to the repository.
假设您有一个已经提交到存储库的README.md
文件。 如果对此文件进行更改,则可以在commit命令中使用-a
选项暂存并将更改添加到存储库。 但是,如果还添加了一个名为index.html
的新文件怎么办? -a
选项不会index.html
因为它当前在存储库中不存在。 添加新文件后,应调用git add
命令以git add
文件,然后再将它们提交到存储库。
An example of how to use the -a
option:
有关如何使用-a
选项的示例:
git commit -am “My new changes”
The output in your terminal should look something like this:
终端中的输出应如下所示:
[master 22gc8v1] My new message
1 file changed, 1 insertion(+)
Although git commit -m "commit message"
works just fine, it can be useful to provide more detailed and systmatic information.
尽管git commit -m "commit message"
正常工作,但提供更详细和系统的信息可能很有用。
If you commit without using the -m
option, git will open your default text editor with a new file, which will include a commented-out list of all the files/changes that are staged in the commit. You then write your detailed commit message (the first line will be treated as the subject line) and the commit will be performed when you save/close the file.
如果您提交时未使用-m
选项,则git将使用新文件打开默认的文本编辑器,该文件将包括提交中暂存的所有文件/更改的注释列表。 然后,您编写详细的提交消息(第一行将作为主题行),并且在保存/关闭文件时将执行提交。
Bear in mind:
记住:
The --amend
option allows you to change your last commit. Let’s say you just committed and you made a mistake in your commit log message. You can conveniently modify the most recent commit using the command:
--amend
选项使您可以更改最后的提交。 假设您刚提交,而在提交日志消息中犯了一个错误。 您可以使用以下命令方便地修改最近的提交:
git commit --amend -m "an updated commit message"
If you forget to include a file in the commit:
如果您忘记在提交中包含文件:
git add FORGOTTEN-FILE-NAME
git commit --amend -m "an updated commit message"
# If you don't need to change the commit message, use the --no-edit option
git add FORGOTTEN-FILE-NAME
git commit --amend --no-edit
Premature commits happen all the time in the course of your day-to-day development. It’s easy to forget to stage a file or how to correctly format your commit message. The --amend
flag is a convenient way to fix these minor mistakes. This command will replace the old commit message with the updated one specified in the command.
在您的日常开发过程中,过早的提交一直发生。 很容易忘记暂存文件或如何正确格式化提交消息。 --amend
标志是解决这些小错误的便捷方法。 此命令将用命令中指定的更新的提交消息替换旧的提交消息。
Amended commits are actually entirely new commits and the previous commit will no longer be on your current branch. When you’re working with others, you should try to avoid amending commits if the last commit is already pushed into the repository.
修改过的提交实际上是全新的提交,而先前的提交将不再位于您的当前分支上。 与他人一起工作时,如果最后一次提交已被推送到存储库中,则应尽量避免修改提交。
With --amend
, one of the useful flag you could use is --author
which enables you to change the author of the last commit you’ve made. Imagine a situation you haven’t properly set up your name or email in git configurations but you already made a commit. With --author
flag you can simply change them without resetting the last commit.
随着--amend
,您可以使用有用的标志之一是--author
,使您能够改变最后提交您所做的作者。 假设您没有在git配置中正确设置您的姓名或电子邮件,但您已经提交了。 使用--author
标志,您可以简单地更改它们而无需重置最后的提交。
git commit --amend --author="John Doe <johndoe@email.com>"
The -v
or --verbose
option is used without the -m
option. The -v
option can be useful when you wish to edit a Git commit message in your default editor while being able to see the changes you made for the commit. The command opens your default text editor with a commit message template as well as a copy of the changes you made for this commit. The changes, or diff, will not be included in the commit message, but they provide a nice way to reference your changes when you’re describing them in your commit message.
使用-v
或--verbose
选项时不使用-m
选项。 当希望在默认编辑器中编辑Git提交消息,同时又能看到对提交所做的更改时, -v
选项很有用。 该命令使用提交消息模板以及为此提交所做的更改的副本打开默认的文本编辑器。 更改或差异将不包含在提交消息中,但是当您在提交消息中描述更改时,它们提供了一种引用更改的好方法。
This is an awesome feature of rebase
that can be used in the interactive
mode. To squash the last n commits into one, run the following command:
这是rebase
一个很棒的功能,可以在interactive
模式下使用。 要将最后n个提交压缩为一个,请运行以下命令:
git rebase -i HEAD~n
That will open up a text-editor with something similar to the following:
这将打开一个文本编辑器,其内容类似于以下内容:
pick commit_1
pick commit_2
pick commit_3
...
pick commit_n
# Bunch of comments
Leave the first commit alone, and change the rest of the pick
s to squash
. Save and exit the editor.
不用理会第一次提交,然后将其余的pick
改为squash
。 保存并退出编辑器。
So if you wanted to squash the last three commits, you’ll first run git rebase -i HEAD~3
and then you’ll want to edit your commits to look something like this:
因此,如果您想压缩最后三个提交,则首先运行git rebase -i HEAD~3
,然后您将要编辑提交以使其类似于以下内容:
pick dd661ba Commit 1
squash 71f5fee Commit 2
squash f4b4bf1 Commit 3
If you’ve already pushed to a remote before squashing your commits, you’ll have to push to the remote again, with the -f
flag, otherwise git will throw an error at you.
如果在压缩提交之前已经将其推送到远程,则必须使用-f
标志再次推送到远程,否则git会向您抛出错误。
It is strongly suggested that you read the information in the opened file as there are many things you can do.
强烈建议您阅读打开的文件中的信息,因为您可以做很多事情。
翻译自: https://www.freecodecamp.org/news/git-commit-command-explained/
git 命令commit