Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

git commit Command

git commit Command

Git Basic OperationsGit Basic Operations


In the previous chapter we used the git add command to write content to the staging area.

The git commit command adds the staging area content to the local repository.

Commit the staging area to the local repository:

git commit -m [message]

[message] can be some remark information.

Commit specified files from the staging area to the repository area:

$ git commit [file1] [file2] ... -m [message]

-a The parameter setting means that after modifying a file you do not need to run the git add command, and can commit directly

$ git commit -a

Set user information when committing code

Before starting we need to set the commit user information, including username and email:


$ git config --global user.name 'runoob'
$ git config --global user.email test@runoob.com

If you remove the --global parameter, it is only valid for the current repository.

Commit the changes

Next we can add all changes to hello.php from the staging area content to the local repository.

In the following example, we use the -m option to provide a commit message on the command line.


$ git add hello.php
$ git status -s
A  README
A  hello.php
$ git commit -m '第一次版本提交'
[master (root-commit) d32cf1f] 第一次版本提交
 2 files changed, 4 insertions(+)
 create mode 100644 README
 create mode 100644 hello.php
 

Now we have recorded a snapshot. If we run git status again:


$ git status
# On branch master
nothing to commit (working directory clean)

The above output shows that since the last commit we have made no changes; it is a "working directory clean", which translates to a clean working directory.

If you do not set the -m option, Git will try to open an editor for you to fill in the commit message. If Git cannot find relevant information in its configuration, it will open vim by default. The screen will look like this:


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
# modified:   hello.php
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C

If you find the git add staging process too tedious, Git also allows you to skip this step with the -a option. The command format is as follows:


git commit -a

We first modify the hello.php file to the following content:


Then execute the following command:


$ git commit -am '修改 hello.php 文件'
[master 71ee2cb] 修改 hello.php 文件
 1 file changed, 1 insertion(+)

Git Basic OperationsGit Basic Operations