What are tracked files and untracked files in the context of Git?
Asked Answered
M

8

38

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?

Muriate answered 12/3, 2012 at 8:4 Comment(0)
U
24

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.

Unreserved answered 12/3, 2012 at 8:15 Comment(2)
I don't see why anyone would want to deal with untracked files for long, either they are tracked or they belong in gitignore....what is the purpose of untracked files that don't get mentioned in gitignore?Woodard
What do you mean by git will refuse to overwrite that file during checkout.Coburg
R
17

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 the .gitignore file to ignore it.
In such a case the file must be un-tracked, usually with git rm --cached filename

File Status Lifecycle

Rhizobium answered 12/3, 2012 at 8:12 Comment(3)
add untracked file will add it to staging area, the arrow should point to staged instead of unmodifiedDaye
what is the file you mention in "this file" phrase. is it .gitignore? Also @Daye is right, here is the correct image link : git-scm.com/book/en/v2/images/lifecycle.pngIota
@Iota The .gitignore file: I have edited the answer accordingly.Rhizobium
K
10

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.

Kyte answered 12/3, 2012 at 8:13 Comment(1)
It is entirely incorrect to say that a "tracked file" is one that was once added AND COMMITTED. A tracked file is simply a file that exists in the index tree--how it got there could have been due to a branch checkout or due to an add or due to a direct Git plumbing command.Anglo
A
8

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.

Anglo answered 27/4, 2017 at 21:39 Comment(0)
P
2

Tracked Files:

Tracked files are files that Git is aware of and actively manages. These are files that have been added to the Git repository using the git add command and committed with git commit. Git tracks changes to these files, allowing you to see the history of changes, switch between different versions (commits), and share these changes with others through pushes to remote repositories.

Untracked Files:

Untracked files are files that are not currently being managed by Git. These are typically files that exist in the working directory of your repository but have not been added to the Git repository using git add. Untracked files will not be included in commits, and Git will not keep a history of changes to these files until you explicitly add them to the repository using git add.

You can use the git status command to see a list of tracked and untracked files in your repository. Tracked files will appear under the "Changes to be committed" section, while untracked files will appear under the "Untracked files" section.

Pfeffer answered 20/10, 2023 at 9:41 Comment(0)
J
1

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

Janiuszck answered 12/11, 2019 at 22:0 Comment(1)
Do you mean that untracked files tend most of the times to be new files in the wirking tree?Sawdust
A
1
  • Tracked : Files which u maked them 'git add' or 'git commit' (i mean this files already is under control of git)
  • Untracked : Files which u not maked them 'git add' or 'git commit' (if u added or committed an X file - this means git tracking that X file)
  • When you first time adding files to an Working Directory, they are all untracked. To get Git to track them, you need to stage them(git add) or add them to the local repository(git commit)
Ameliaamelie answered 23/12, 2022 at 22:56 Comment(0)
S
0

Suppose you are working on a project and you decided to use a VCS to manage it better and you downloaded Git, and then you initialized a git repo inside your working directory, an empty repo is initialized (a folder with .git name) at this point any files residing inside your working directory are untracked that means the files are not yet managed by the version control, any operations on those untracked files is not recorded or maintained by the version control. Also you can check the untracked files in your working directory by the following command

git status

That said the version control is useless if the files remained untracked.

So to make the use of version control we have to make our files tracked that means in simple words the tracked files are those files which are under version control.

At this point you have a empty repo inside your working directory and few untracked files, now to make those untracked files as tracked files you have to run a command that is

git add .

At this point all the untracked files become tracked and sits into staged area, so from this point any modification in any file will be tracked by the git. As soon as you run the above command a file with the name index is created inside the git repo and it will contains the tracked files, so as long as the files sits in the index file it is a tracked file, also a SHA-1 hash is also created in Object folder

So any file which are staged or committed is a tracked file, otherwise untracked. So from that point any new file added to working directory is untracked until it is not added by using 'git add' commands, also there can be files which can formed automatically like if you compile a java program a class file is automatically created which is created as untracked so now two options are there either use the add command to make them tracked files or put such files into .gitignore so that it will be ignored by the git and not shown in the untracked files in 'git status'

Also if the file is committed then there will form two more SHA-1 hash inside the Object folder

So as long as the file exists inside the index it is a file which is tracked by the git, otherwise if the files are never added to the index or removed from the index then those files are called as untracked files and are not under any version control

Sulphuryl answered 20/6 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.