git add Command
The git add command adds the file to the staging area.
Add one or more files to the staging area:
git add [file1] [file2] ...
Add a specified directory to the staging area, including subdirectories:
git add [dir]
Add all files in the current directory to the staging area:
git add .
The following example adds two files:
$ touch README # Create file
$ touch hello.php # Create file
$ 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 execute git status again, we can see that these two files have been added.
$ git status -s
A README
A hello.php
$
In new projects, it is common to add all files, which we can do with the git add . command to add all files in the current project.
Now, let's modify the README file:
$ vim README
Add the following content to README: # tutorialpro Git Test, then save and exit.
Execute git status again:
$ git status -s
AM README
A hello.php
The AM
status means the file has been modified after it was added to the cache. After modifying, we execute the git add .
command to add it to the cache:
$ git add .
$ git status -s
A README
A hello.php
After modifying a file, we usually need to perform a git add operation to save the historical version.