git add command

Git Basic Operations


git addThe command can add the file's modifications to the staging area.

By runninggit addWith the command, you can tell Git which file changes should be included in the next commit.

Add one or more files to the staging area:

git add [file1] [file2] ...

Add the specified directory to the staging area, including subdirectories:

git add [dir]

Add all files in the current directory to the staging area:

git add .

In the following example, we add two files:

$ touch README                # 创建文件
$ touch hello.php             # 创建文件
$ ls
README        hello.php
$ git status -s
?? README
?? hello.php
$ 

The git status command is used to view the current status of the project.

Next, we execute the git add command to add files:

$ git add README hello.php 

Now if we run git status again, we can see that these two files have been added.

$ git status -s
A  README
A  hello.php
$ 

In a new project, it's common to add all files. We can usegit add .the command to add all files in the current project.

Now we modify the README file:

$ vim README

Add the following content to the README:# Example Git Test, then save and exit.

Then run git status:

$ git status -s
AM README
A  hello.php

AMThis status means the file has been modified again after we added it to the cache. After modifying, we rungit add .command to add it to the cache:

$ git add .
$ git status -s
A  README
A  hello.php

After files are modified, we generally need to perform a git add operation to save historical versions.



Git Basic Operations

other extensions