git only tracking a single directory without any files

Viewed 52

git status shows:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        common/

nothing added to commit but untracked files present (use "git add" to track)

The untracked changes just a single directory itself, no file. The directory attributes changed? The diff command git diff common/ show nothing

How did this happen?

2 Answers

There are in fact multiple untracked files within that directory. Use git status -uall to show all of them.

Normally, git status collects them up and removes their names and shows just the directory-leading-to-them, so as not to print out too many names: it's pretty common to have a directory with several dozen or hundreds of untracked files if you forgot to list all such files in your .gitignore, and this makes the git status output too long.

"Untracked files" refers to files that exist only in your working copy, and have no corresponding changes in the git repository. In other words, this directory is not changed, it's newly created.

In most cases, git doesn't actually care about directories - they're not tracked as items, and you can't commit an empty directory. However, a newly created directory might contain many files, and make the output of git status far too long, so the directory name is listed as a summary.

If you use git add common, all the files currently in that directory will be staged for inclusion in your next commit, and tracked from then on. If you add common/ to your .gitignore file, the entire directory will be excluded from future git status checks.

Related