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

妖精的绣舞 提交于 2019-12-20 11:08:03

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


Remember that each file in your working directory can be in one of two states: tracked or untracked. In short, tracked files are files that Git knows about. Untracked files are everything else — any files in your working directory that were not in your last snapshot and are not in your staging area. Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged



来源:https://stackoverflow.com/questions/9663507/what-is-tracked-files-and-untracked-files-in-the-context-of-git

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