git commit command

Git Basic Operations


In the previous chapter, we used the git add command to write content into 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 the specified files in the staging area to the repository:

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

-aParameter setting: after modifying a file, there is no need to execute the git add command, just commit directly.

$ git commit -a

Set the user information when committing code

Before starting, we need to set up the user information for commits, including username and email:

$ git config --global user.name 'example'
$ git config --global user.email [email protected]

If the --global parameter is removed, it only applies to the current repository.

Submitmodify

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

The following example uses 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 we have not made any changes since the last commit; it is "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, vim will open 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 <file>..." to unstage)
#
# modified:   hello.php
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C

If you think the process of git add to stage changes is too cumbersome, Git also allows you to use the -a option to skip this step. The command format is as follows:

git commit -a

First, let's modify the hello.php file to the following content:

<?php
echo 'Example:www.example.com';
echo 'Example:www.example.com';
?>

Then run the following command:

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


Git Basic Operations

other extensions