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

倾然丶 夕夏残阳落幕 提交于 2019-12-03 02:19:42

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.

VonC

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!