Reputation: 457
When you modify a file in your working directory, git tells you to use "git add" to stage.
When you add a new file to your working directory, git tells you to use "git add" to start tracking.
I am a bit confused about these 2 concepts because i assumed tracking a file for changes is different from staging it for commit
Upvotes: 33
Views: 10575
Reputation: 363
Let me explain Git's Three Tree Architecture, which includes a working directory, a staging index, and a repository:
When we move a file from the working directory to the staging index, git begins tracking the changes, indicating that the file is staged (in the staging index), and because it is also tracking the changes, we can say that the file is tracked.
There is a distinction between the file tracked in the staging index and the file tracked in the repository. When changes are committed to the repository, they become part of the repository history, whereas changes in the staging index have no effect on the git history.
Upvotes: 1
Reputation: 5241
Git essentially has 4 main statuses for the files in your local repo:
git add <file>
, it becomes:git commit
, it becomes:git add
As you can see, a git add
will track untracked files, and stage any file.
Also: You can untrack an uncommited file with git rm --cached filename
and unstage a staged file with git reset HEAD <file>
Upvotes: 53
Reputation: 14061
Both of the git add
steps you identify do essentially the same thing, they simply have different explanations because of their arrival route.
The git add
simply tells git that the file provided is a file that you desire to have, in its exact current form (its content), within its source control repository. At that point git will take a snapshot of the file (and it keeps a note in its index) so that it is ready for when you have all your changes to your files ready and added (i.e marshalled together in the staging area), for your git commit
(with appropriate message ;-).
Once git has been told about that file (e.g. @avh's -N
option) it will notice (track) changes to the file under the guise of various commands (such as git status
). Thus, later, you have to explicitly tell git when you no longer want a file to be tracked (git rm <file>
), and you can continue editing a file (locally) after you have add
ed the version that will be in the commit. Almost obviously (or perhaps not), you can git add
a file many times before you commit the final version.
Upvotes: 1
Reputation: 265211
Git has a concept known as 'the index'. To create a new commit, you fill the index with the contents you'd like to have in the next commit. That means that you have to explicitly tell Git which changes you want to appear in the next commit by using git add
. (git add -p
to add only single hunks)
It doesn't make a difference to Git whether you only update a file (»stage changes«) or if you add the full content of a new file (»start tracking a file«) – both times, all that Git's index sees is the addition of new changes
Upvotes: 19
Reputation: 2655
When you add a file to start tracking, it also stages its contents.
If you want to add a file for tracking without staging it, you can use
git add -N
Upvotes: 11