make - remove untracked files git

What is Tracked files and Untracked files in the context of GIT? (3)

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?

Thanks, Nayan

A file is tracked if it is under version control.

As a small example, a C++ project would have


as source files; you'd put these under version control. During build,


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.

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.

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.