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?
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?
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.
git will refuse to overwrite that file during checkout.
–
Coburg 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 withgit rm --cached filename
.gitignore
file: I have edited the answer accordingly. –
Rhizobium 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.
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.
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
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
© 2022 - 2024 — McMap. All rights reserved.