什么是 GIT 上下文中的跟踪文件和未跟踪文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9663507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:10:25  来源:igfitidea点击:

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

gitgit-trackgit-untracked

提问by Nayan Soni

I'm new to Git. I wish to know what are trackedand untrackedfiles? I read "Pro Git", but still couldn't quite understand.

我是 Git 的新手。我想知道什么是跟踪文件和未跟踪文件?我读了“Pro Git”,但还是不太明白。

Can someone explain to me the difference between the two by providing an example?

有人可以通过提供一个例子向我解释两者之间的区别吗?

采纳答案by Simon Richter

A file is tracked if it is under version control.

如果文件处于版本控制之下,则会对其进行跟踪。

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

作为一个小例子,一个 C++ 项目将有

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 addon 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 statuscommand until you make a decision.

产生;这些不属于版本控制,所以你不要使用git add它们。他们仍然没有被跟踪,因为 git 并不关心他们会发生什么。在您将它们添加到.gitignore(默认情况下忽略 .o 文件)之前,git 不知道您是要添加还是忽略它们,因此它会使用git status命令显示它们,直到您做出决定。

Whether a file is tracked or not also depends on the version -- suppose you autogenerate worker.cppand 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.

文件是否被跟踪还取决于版本——假设您worker.cpp在更高版本中自动生成并从版本控制中删除它。该文件现在在该版本中未跟踪。当您返回文件仍受版本控制的版本时,git 将拒绝在结帐期间覆盖该文件。

回答by VonC

The Git Pro book chapteryou mention tries to detail the notion of untracked file:

您提到的Git Pro 书籍章节试图详细说明未跟踪文件的概念:

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)?"

当您签出给定的 SHA1 时,您将获得所有版本化文件的“快照”。
此快照未引用的任何文件都不会被跟踪。它不是 Git 树的一部分:
请参阅“ git - 如何判断文件是否被 git 跟踪(通过 shell 退出代码)?

Any file that you want to ignore must be untracked (as explained in this GitHub help page).

您要忽略的任何文件都必须未被跟踪(如本GitHub 帮助页面中所述)。

Note that git will not ignore a file that was already tracked before a rule was added to the .gitignorefile to ignore it.
In such a case the file must be un-tracked, usually with git rm --cached filename

请注意,在将规则添加到.gitignore文件以忽略它之前,git 不会忽略已跟踪的文件。
在这种情况下,必须取消跟踪文件,通常使用git rm --cached filename

enter image description here

在此处输入图片说明

回答by CharlesB

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.

跟踪文件是由 Git 处理(版本控制)的文件,它们曾经被添加和提交。大多数情况下,未跟踪的文件是您不想被控制的文件,因为例如它们是由您的编译器生成的。

You add untracked files to the .gitignorefile, so that Git don't ask you if you want to track them.

您将未跟踪的文件添加到.gitignore文件中,这样 Git 就不会询问您是否要跟踪它们。

回答by Jazimov

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.

从纯技术角度来看:被跟踪文件只是存在于 Git 索引中的文件。说它是一个“受版本控制”的文件是一种误导,因为这表明它是一个将其放入存储库的文件 - 而这对于跟踪文件来说不是必需的。

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.

当您初始化一个新的 Git 存储库时,索引为空并且您工作目录中的所有文件都未跟踪。将文件添加到索引时会对其进行跟踪——此时会为其创建 SHA-1 哈希,并将对象条目放入 .Git\Objects 文件夹中。从那一刻起,Git 能够比较工作目录中同一文件的内容/名称,以跟踪更改、重命名和删除。只要文件存在于索引中,它就会被跟踪。

回答by Hasham

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

请记住,工作目录中的每个文件都可以处于以下两种状态之一:已跟踪或未跟踪。简而言之,跟踪文件是 Git 知道的文件。未跟踪的文件就是其他所有文件? - 工作目录中不在上次快照中且不在暂存区中的任何文件。跟踪文件是上次快照中的文件;它们可以是未修改的、修改的或暂存的