Easy Tutorial
❮ Git Workspace Index Repo Git Install Setup ❯

git commit Command

Git Basic Operations


In the previous sections, we used the git add command to add content to the staging area.

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

To commit the staging area to the local repository:

git commit -m [message]

[message] can be some notes.

To commit specified files from the staging area to the repository:

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

The -a parameter allows you to commit modified files without executing the git add command:

$ git commit -a

Setting User Information for Code Submission

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

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

If the --global parameter is omitted, it only takes effect for the current repository.

Committing Changes

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

In the following example, we use the -m option to provide a commit comment from the command line:

$ git add hello.php
$ git status -s
A  README
A  hello.php
$ git commit -m 'First version submission'
[master (root-commit) d32cf1f] First version submission
 2 files changed, 4 insertions(+)
 create mode 100644 README
 create mode 100644 hello.php

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

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

The above output indicates that we have made no changes since the last commit, and it is a "clean working directory."

If you do not set the -m option, Git will attempt to open an editor for you to fill in the commit message. If Git cannot find relevant information in its configuration, it will default to opening vim. 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 find the git add process too cumbersome, 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:

<?php
echo 'tutorialpro.org:www.tutorialpro.org';
echo 'tutorialpro.org:www.tutorialpro.org';
?>

Then execute the following command:

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

Git Basic Operations

❮ Git Workspace Index Repo Git Install Setup ❯