Git add .

I want to understand what my git repository is going to add to my repository if
Do `git add .`
Then commit
Then push
I ask this question because when I look at my `git status` is see three different categories:

Changes to be committed:
Changes not staged for commit:
Untracked files:

What do these categories mean?

```
$ git status
On branch Search
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: src/main/java/MongoDbTxt.java

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: src/main/java/MongoDbTxt.java
modified: Imp/settings.gradle

Untracked files:
(use "git add <file>..." to include in what will be committed)
build.gradle
build/
src/main/resources/


jen@ru MINGW64 /c/ru (Search)
$
```

Last edited on
Changes to be committed:
Changes not staged for commit:
Untracked files:

What do these categories mean?

first you need to understand that git repository consists of (in this order):
1. Working tree
2. index
3. Local repository
4. remote repository


Working tree is where your working tree is, that is your code and project files on your local computer.

index is where you add or "stage" your changes from working tree to index, index resides also on your local computer

Local repository is also on your local computer and it's a place where you commit staged files, from index, that is you commit them to local repository.

finally remote repository is as the name implies remote git repo such as Github.

OK, now to the answer:
when you do git add you move your changes from working tree to index.
when you do git commit you mark your index as ready for push, that is you "move" it to local repository.
Finally when you do git push you copy commits (commited code from index to local repository) from local repository to remove repository.

note: that index does not contain any files (unlike, working tree, local repo and remote repo), I use "move" word here to simplify explanation

then if you continue to do some work in your code(working tree) these new changes are not staged or comitted yet, so when you do git status you see them as Changes not staged for commit.

note that staging means adding files to index, and commiting means adding files from index to local repo, and push means copy from local repo to remote repo.

now if you do git add . and then continue to work on code and do git status you'll see new changes as Changes not staged for commit and Changes to be committed (which are changes which you git add previously, prior to making new further changes)

working tree clean means you commited all your local changes to local repository (or remote repo is there are not commits to be pushed)

Untracked files
untracked files are files or code in your working tree which you did not git add *initially* to track them (that is to be able to add them to index in the future, and then commit and push) you use git add. to track files which means that git tracks changes on them, you only git add them once, subsequent git add means adding them to index (not start tracking them)!

Hopefully this gets you a better picture of how git works, if you have more questions feeel free to ask.
Last edited on
Thank you for the explanation, you could not have explained it better!
Topic archived. No new replies allowed.