git 命令commit_Git Commit命令解释

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 )

  • if multiple commits edit different parts of the project, they will not overwrite each other even if the authors of the commit were unaware of each other. This is one of the benefits of using Git over a tool like Dropbox or Google Drive.

    如果多个提交编辑了项目的不同部分,则即使提交的作者彼此不知道,它们也不会互相覆盖。 这是通过Dropbox或Google Drive之类的工具使用Git的好处之一。

选件 (Options)

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文档

-m选项 (The -m Option)

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命令中,则将提示您在默认文本编辑器中添加一条消息-请参阅下面的“使用详细的提交消息”。

-a选项 (The -a Option)

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(+)

使用详细的提交消息 (Using detailed commit messages)

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:

记住:

  • Keep your commit message lines length less than 72 charcters as standard practice

    按照惯例,提交消息行的长度应少于72个字符
  • It is perfectly ok - and even recommended - to write multiline commit messages

    编写多行提交消息是完全可以的,甚至推荐这样做
  • You can also refer to other issues or pull requests in your commit message. GitHub allocated a number reference to all pull requests and issues, so for example if you want to refer to pull request #788 simply do so in either the subject-line or in the body text as appropriate

    您还可以参考其他问题或在提交消息中提取请求。 GitHub为所有请求请求和问题分配了编号引用,因此,例如,如果您要引用请求请求788,只需在主题行或正文中适当地引用

—修改选项 (The —amend Option)

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>"

-v或-verbose选项 (The -v or —verbose Option)

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选项很有用。 该命令使用提交消息模板以及为此提交所做的更改的副本打开默认的文本编辑器。 更改或差异将不包含在提交消息中,但是当您在提交消息中描述更改时,它们提供了一种引用更改的好方法。

如何将多个提交压缩为一个 (How to squash multiple commits into one)

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 picks 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.

强烈建议您阅读打开的文件中的信息,因为您可以做很多事情。

更多信息: (More Information:)

翻译自: https://www.freecodecamp.org/news/git-commit-command-explained/

git 命令commit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值