Daniel Griscom
Daniel Griscom

Reputation: 2263

Get a file's complete git status, including ignored, untracked, unmodified, modified

Sometimes I'll have a file in a git-controlled repository, and I'll want to know how git understands the file. Is it tracked and modified? Tracked but unmodified? Untracked and ignored? Untracked and not ignored?

Problem: the commands I know don't give complete information:

Is there a git command (or series of commands) which will show everything git knows about a file (except perhaps any diff if modified)?

Upvotes: 0

Views: 81

Answers (1)

torek
torek

Reputation: 489065

Sometimes I'll have a file in a git-controlled repository, and I'll want to know how Git understands the file. Is it tracked and modified? Tracked but unmodified? Untracked and ignored? Untracked and not ignored?

This is fundamentally a bit tricky.

To test whether the file is tracked, see if it exists in the index. You may first wish to test whether the index contains any unmerged entries, or inspect the potential matches for their staging slots. As phd suggested in a comment, git ls-files is your plumbing command to use here, to tell whether the file is in Git's index. Run git ls-files --staged to see if the file is in the index, and if so, at which slots. If it is in the index (at all), it's tracked; if not, it's untracked.

Having decided whether the file is tracked or not, you now pass into two separate cases:

  • An untracked file can either exist in the working tree, or not, and that's pretty much it for the file itself. If it does exist in the working tree, it can be excluded, or not. Use git ls-files with the --others option to see if it exists, and add --exclude-standard to see if it's ignored.

  • A tracked file can either exist in the working tree, or not; if it doesn't exist in the working tree, it's considered "modified", and if it's modified as compared to the index version, git ls-files --modified will show it as modified. So use git ls-files to check its status here.

    A tracked file can also be marked --assume-unchanged and/or --skip-worktree. These show up as flag letters in git ls-files -v output.

Hence, git ls-files is the command to use for everything: you just have to use it more than once, and take further actions based on the first one's output.

(To script git ls-files, use the -z option. See my git flagged command for an example.)

Upvotes: 1

Related Questions