Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concept of git tracking and git staging

Tags:

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

like image 742
Jeele Yah Avatar asked Sep 27 '11 06:09

Jeele Yah


People also ask

What is staging with git?

A staging step in git allows you to continue making changes to the working directory, and when you decide you wanna interact with version control, it allows you to record changes in small commits.

What does it mean to track a file git?

Tracked files are files that were in the last snapshot, as well as any newly staged files; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about.

What is the difference between staging and committing in git?

If a particular version of a file is in the Git directory, it's considered committed. If it has been modified and was added to the staging area, it is staged.

What are the three stages of Git?

Every project under the distributed version control system Git, goes through three stages — Modified, Staged, and Committed.


2 Answers

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>

like image 189
ejoubaud Avatar answered Oct 04 '22 10:10

ejoubaud


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

like image 38
knittl Avatar answered Oct 04 '22 08:10

knittl