Reputation: 2263
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:
git status fileName
: shows nothing unless file is tracked and modifiedgit check-ignore fileName
: shows if a file is ignored, but shows nothing if untracked or trackedgit check-ignore --no-index fileName
: ignores the index, and so won't show whether file is trackedIs 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
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