One difference between Git and other version control systems such as SVN is the concept of a staging area.
Let's look at the noun first.
Workspace (Working Directory)
It is the directory you can see on your computer. For example, my learngit
folder is a workspace:
Repository
The workspace has a hidden directory .git
, which is not a workspace, but a Git repository.
There are a lot of things in the Git repository, the most important of which is the staging area called stage (or index), the first branch that Git automatically created for us master
, and master
a pointer to it called HEAD
.
We will talk about the concept of branch sum HEAD
later.
As mentioned earlier, when we add files to the Git repository, it is performed in two steps:
The first step is to git add
add the file to it, which is actually to add the file modification to the temporary storage area;
The second step is to git commit
commit the changes, which actually commits all the contents of the staging area to the current branch.
Because when we created a Git repository, Git automatically created the only master
branch for us, so now, git commit
it's time to commit changes to the master
branch.
You can simply understand that the file modifications that need to be submitted are all placed in the staging area, and then all the changes in the staging area are submitted at one time.
As the saying goes, practice brings true knowledge. Now, let's practice again, first readme.txt
make a modification, such as adding a line:
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Then, create a new text file in the workspace LICENSE
(write whatever content you want).
git status
Check the status first with :
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: readme.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# LICENSE
no changes added to commit (use "git add" and/or "git commit -a")
Git tells us very clearly that it has readme.txt
been modified and LICENSE
has never been added, so its state is Untracked
.
Now, use the command twice git add
, add the sum readme.txt
and check again:LICENSE
git status
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: LICENSE
# modified: readme.txt
#
Now, the state of the staging area becomes like this:
Therefore, the git add
command actually puts all the changes to be submitted into the staging area (Stage), and then, execution git commit
can submit all the changes in the staging area to the branch at one time.
$ git commit -m "understand how stage works"
[master 27c9860] understand how stage works
2 files changed, 675 insertions(+)
create mode 100644 LICENSE
Once committed, the workspace is "clean" if you haven't made any changes to it:
$ git status
# On branch master
nothing to commit (working directory clean)
Now that the repository looks like this, there is nothing in the staging area:
summary
The temporary storage area is a very important concept of Git. If you understand the temporary storage area, you will understand what Git does with many operations.