visit
The command used to stage any change in Git is git add
. The git add
command adds a modification to the staging area from the working directory. It informs Git that you wish to include changes to a specific file in the next commit. However, git add has little effect on the repository—changes are not truly recorded until you execute git commit.
git add
[project*] <------------ project*
^
| modify with editor
|
HEAD . project
| .
| .
| .
HEAD~1 .
| .
| .
| .
.
.git . working directory
(history) . (files we see)
You can specify a <file>
from which all changes will be staged. The syntax would be as follows:
git add <file>
git add <directory>
You can also use a .
to add all the changes from the present directory, such as the following:
git add .
git status
command is used to check the status of the files (untracked, modified, or deleted) in the present branch. It can be simply used as follows:
git status
In case, you have accidentally staged a file or directory and want to undo it or unstage it, then you can use the git reset
command.
git reset
[project*] ------------> project*
HEAD .
| .
| .
| .
HEAD~1 .
| .
| .
| .
.
.git . working directory
(history) . (files we see)
git reset HEAD example.html
If you remove files, they will appear as deleted in git status
, and you must use git add
to stage them. Another option is to use the git rm
command, which deletes and stages files in a single command:
git rm example.html
git rm -r myfolder
The git commit
command saves a snapshot of the current staged changes in the project. Committed snapshots are "secure" versions of a project that Git will never alter unless you specifically ask it to.
HEAD . project
| .
| .
| .
HEAD~1 .
| .
| .
| .
HEAD~2 .
| .
| .
| .
.
.git . working directory
(history) . (files we see)
git commit -m "commit message"
To summarize, git add
is the first command in a series of commands that instructs Git to "store" a snapshot of the current project state into the commit history. When used alone, git add
moves pending changes from the working directory to the staging area. The git status
command examines the repository's current state and can be used to confirm a git add
promotion. To undo a git add
, use the git reset
command. The git commit
command is then used to add a snapshot of the staging directory to the commit history of the repository.