Concept of git tracking and git staging
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.
- how to get a file from the staging area?
- Retrieving an old version of a file using Git
- How to select repository for staging view in Eclipse Luna 4.4?
- What's the best way to set up Git staging and production environments with separate databases?
- git repository to staging site to live site
- Unable to apply manually edited hunks in git
I am a bit confused about these 2 concepts because i assumed tracking a file for changes is different from staging it for commit
4 Solutions collect form web for “Concept of git tracking and git staging”
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
Git essentially has 4 main statuses for the files in your local repo:
- untracked: The file is new, Git knows nothing about it. If you
git add <file>, it becomes:
- staged: Now Git knows the file (tracked), but also made it part of the next commit batch (called the index). If you
git commit, it becomes:
- unchanged: The file has not changed since its last commit. If you modify it, it becomes:
- unstaged: Modified but not part of the next commit yet. You can stage it again with
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>
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
Both of the
git add steps you identify do essentially the same thing, they simply have different explanations because of their arrival route.
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
added 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.