简体   繁体   中英

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.

I am a bit confused about these 2 concepts because i assumed tracking a file for changes is different from staging it for commit

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 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>

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

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.

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 (ie marshalled together in the staging area), for your git commit (with appropriate message ;-).

Once git has been told about that file (eg @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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM