What is Tracked files and Untracked files in the context of GIT?
I’m new to Git. I wish to know what are tracked and untracked files?
I read “Pro Git”, but still couldn’t quite understand.
Can someone explain to me the difference between the two by providing an example?
- git diff files not in repository against files in repository
- Git untracked/unstaged files right after clone
- git commit modified and untracked content
- Git ignore and untrack file
- why git status shows “working directory clean” even after I add a new file
4 Solutions collect form web for “What is Tracked files and Untracked files in the context of GIT?”
A file is tracked if it is under version control.
As a small example, a C++ project would have
Makefile main.cpp interface.hpp worker.cpp
as source files; you’d put these under version control. During build,
main.o worker.o myapp
are generated; these do not belong under version control, so you do not use
git add on them. They remain untracked, because git does not care what happens to them. Until you add them to
.gitignore (the .o files are ignored by default), git does not known whether you want to add or ignore them, so it displays them with the
git status command until you make a decision.
Whether a file is tracked or not also depends on the version — suppose you autogenerate
worker.cpp and remove it from version control at a later version. The file is now untracked in that version. When you go back to a version where the file was still under version control, git will refuse to overwrite that file during checkout.
Tracked files are the one handled (version controlled) by Git, that were once added and committed. Untracked files are most of the time files you don’t want to be controlled, because for example they are generated by your compiler.
You add untracked files to the
.gitignore file, so that Git don’t ask you if you want to track them.
The Git Pro book chapter you mention tries to detail the notion of untracked file:
When you checkout a given SHA1, you get a “snapshot” of all versioned files.
Any file not referenced by this snapshot is untracked. It isn’t part of the Git tree:
See “git – how to tell if a file is git tracked (by shell exit code)?”
Any file that you want to ignore must be untracked (as explained in this GitHub help page).
Note that git will not ignore a file that was already tracked before a rule was added to this file to ignore it.
In such a case the file must be un-tracked, usually with
git rm --cached filename
From a purely technical perspective: A tracked file is simply a file that exists in the Git index. To say it’s a file “under version control” is misleading, because this suggests that it’s a file that made it into the repo–and that’s not necessary for a file to be tracked.
When you init a new Git repo, the index is empty and all files in your working directory are untracked. A file gets tracked when it’s added to the index–at which point a SHA-1 hash is created for it and an object entry is placed into the .Git\Objects folder. From that moment on, Git is able to compare the contents/name of the same file in the working directory in order to track changes, renames, and deletes. As long as the file exists in the index, it is tracked.