T
I think we can better understand these states by creating a zero project. First I will create a folder called "project" and enter it:$ mkdir projeto
$ cd projeto/
For now she's empty. I will use https://git-scm.com/docs/git-init to boot it as a Git repository:$ git init
Initialized empty Git repository in C:/Users/usuario/projeto/.git/
Now I will start my project by adding a file (with the "hello world" content inside it), and then check the status of this file with https://git-scm.com/docs/git-status :$ echo "hello world" > hello.txt
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
See the file hello.txt is listed as "Untracked files." That is, Git is not "trackeando" this file. We can say that Git is not controlling the life cycle of this file (and I confess that I do not know if "life cycle" is the term technically correct).It's like Git said: "I saw you have a file here, but I'm not responsible for it".For Git to be responsible for this file, use https://git-scm.com/docs/git-add :$ git add hello.txt
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
Now the file is listed as "Changes to be committed, that is, he will be part of the next commit. It is then said that the file is "staged(or is in staging area, or no index, or no cache, https://www.reddit.com/r/git/comments/2szztw/is_the_index_the_same_as_the_staging_area/ ).Anyway, note that there is even an instruction to remove the file from the staging area (use "git rm --cached ..." to unstage).Instead of unstage the file, let's go commit it (I'm so sorry about that...)$ git commit -m"Primeiro commit"
[master (root-commit) ccc2a05] Primeiro commit
1 file changed, 1 insertion(+)
create mode 100644 hello.txt
$ git status
On branch master
nothing to commit, working tree clean
After the commit, git status says that there is nothing to show ("nothing to commit, working tree clean"), i.e. all files are up-to-date: your content hits with the last commit, there was no change). It can be said then that the file hello.txt There you are. unmodified (there are no modifications, compared with the last one commit).Then we will change the file by adding some text to its end:$ echo "Novo texto" >> hello.txt
$ cat hello.txt
hello world
Novo texto
Now the file has 2 lines, let's see what the git status shows:$ 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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
It shows that the file is ♪, that is, it was modified, compared to the last commit. But note that this change will not be in the next commit, since the file is listed as "Changes not staged for commit" and in the end still appears the message "no changes added to commit".I can see the modifications using https://git-scm.com/docs/git-diff :$ git diff
diff --git a/hello.txt b/hello.txt
index 3b18e51..7d34649 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
hello world
+Novo texto
The passage +Novo texto indicates that this line has been added (+). For the change to be available next commit, just use git add:$ git add hello.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
Notice that the file continues with status "♪" (after all, he is different from the last commit), but now it will be next commit ("Changes to be committed").That is, he is at the same time ♪ (different compared to the last) commit) and staged (for it is in staging area).The fact of being in staging area make it git diff do not show any more differences:$ git diff
(não mostra nada)
To know what has changed, you should use the option --cached, comparing the staging area (also called cache) with the last commit:$ git diff --cached
diff --git a/hello.txt b/hello.txt
index 3b18e51..7d34649 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
hello world
+Novo texto
By the way, https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgt--cached--merge-baseltcommitgt--ltpathgt82308203 - that is, compare the files that are staged with the last commit.In short:untracked: git knows nothing about the file. I mean, you know he exists, but you won't control his life cycle.staged: those who are in staging area/index/cache. Are the ones that will be next commit♪: those that were changed, compared to the last commitunmodified: those not changedFinally, it is worth remembering that the https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository says there are actually only two states: tracked and untracked. That is, or Git is controlling the life cycle of your file (tracked) or is not (untracked). Being a file in the state tracked may be staged, ♪ or unmodified.