gitgit-trackgit-untracked

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


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?


Solution

  • 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.