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?
What Are Untracked Files? Untracked files are the ones still not versioned—”tracked”—by Git. This is the state of new files you add to your repository. That basically means Git is aware the file exists, but still hasn't saved it in its internal database.
You have to add the untracked files of the repository by using the “git add” command and run the “git stash” command to save the untracked file and clean the current directory for working by removing the untracked file from the repository folder.
When you start a new repository, you typically want to add all existing files so that your changes will all be tracked from that point forward. So, the first command you'll typically type is "git add ." (the "." means, this directory. So, it will add everything in this directory.) I'll type "git add ." and press Enter.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With