Dale Forester
Dale Forester

Reputation: 18917

How to tell if a file is git tracked (by shell exit code)?

Is there a way to tell if a file is being tracked by running some git command and checking its exit code?

In other words: is git tracking a file?

Upvotes: 490

Views: 182363

Answers (10)

Luca Davanzo
Luca Davanzo

Reputation: 21520

I suggest a custom alias in your .gitconfig.

You have two ways you could do it:

  1. With a git command:
    git config --global alias.check-file <command below>
  1. Editing ~/.gitconfig and adding this line in the alias section:
    [alias]
        check-file = "!f() { if [ $# -eq 0 ]; then echo 'Filename missing!'; else tracked=$(git ls-files ${1}); if [[ -z ${tracked} ]]; then echo 'File not tracked'; else echo 'File tracked'; fi; fi;  };  f"

Once you launch the command (1) or save the file (2), you can test it like so:

$ git check-file
$ Filename missing 

$ git check-file README.md
$ File tracked 

$ git check-file foo
$ File not tracked

Upvotes: 7

jthill
jthill

Reputation: 60295

if git rev-parse -q --verify :path/to/file >&-
then echo it\'s tracked
else echo it\'s not
fi

Upvotes: 2

Vineet Gupta
Vineet Gupta

Reputation: 120

I would prefer git log -1 --oneline to git ls-files because git log command doesn't traverse directory, so the output is similar irrespective of whether path being checked is a file or directory.

Here is sample output for git ls-files:

vinegupt@bhoscl88-04(/imsgit_local/work/vinegupt/ims_21.8/ims_oam/MT)$ git ls-files untracked-file # Untracked file shows no output
vinegupt@bhoscl88-04(/imsgit_local/work/vinegupt/ims_21.8/ims_oam/MT)$ git ls-files form/data.json # tracked file shows its name again
form/data.json
vinegupt@bhoscl88-04(/imsgit_local/work/vinegupt/ims_21.8/ims_oam/MT)$ git ls-files form # For directory entire directory is traveresed
form/Traffic_Params_2M_IS_cont_GVolte_EATF.xls
form/data.json

Sample output for git log -1 --oneline:

vinegupt@bhoscl88-04(/imsgit_local/work/vinegupt/ims_21.8/ims_oam/MT)$ git log -1 --oneline untracked-file # Untracked file shows no output
vinegupt@bhoscl88-04(/imsgit_local/work/vinegupt/ims_21.8/ims_oam/MT)$ git log -1 --oneline form/data.json # tracked file shows one-line output
e8e9e0f CONTAINERS-767 MT and release change for 21.8
vinegupt@bhoscl88-04(/imsgit_local/work/vinegupt/ims_21.8/ims_oam/MT)$ git log -1 --oneline form # Only directory info is given no traversal
e8e9e0f CONTAINERS-767 MT and release change for 21.8

Upvotes: -1

andreee
andreee

Reputation: 4679

Just my two cents:

git ls-files | grep -x relative/path

where relative/path can be easily determined by pressing tab within an auto-completion shell. Add an additional | wc -l to get a 1 or 0 output.

Upvotes: 5

hasen
hasen

Reputation: 166152

try:

git ls-files --error-unmatch <file name>

will exit with 1 if file is not tracked

Upvotes: 641

SuperNova
SuperNova

Reputation: 27466

using git log will give info about this. If the file is tracked in git the command shows some results(logs). Else it is empty.

For example if the file is git tracked,

root@user-ubuntu:~/project-repo-directory# git log src/../somefile.js
commit ad9180b772d5c64dcd79a6cbb9487bd2ef08cbfc
Author: User <[email protected]>
Date:   Mon Feb 20 07:45:04 2017 -0600

    fix eslint indentation errors
....
....

If the file is not git tracked,

root@user-ubuntu:~/project-repo-directory# git log src/../somefile.js
root@user-ubuntu:~/project-repo-directory#

Upvotes: 3

Jonas Sourlier
Jonas Sourlier

Reputation: 14435

If you don't want to clutter up your console with error messages, you can also run

git ls-files file_name

and then check the result. If git returns nothing, then the file is not tracked. If it's tracked, git will return the file path.

This comes in handy if you want to combine it in a script, for example PowerShell:

$gitResult = (git ls-files $_) | out-string
if ($gitResult.length -ne 0)
{
    ## do stuff with the tracked file
}

Upvotes: 114

Jay Walker
Jay Walker

Reputation: 399

I don't know of any git command that gives a "bad" exit code, but it seems like an easy way to do it would be to use a git command that gives no output for a file that isn't tracked, such as git-log or git-ls-files. That way you don't really have to do any parsing, you can run it through another simple utility like grep to see if there was any output.

For example,

git-ls-files test_file.c | grep .

will exit with a zero code if the file is tracked, but a exit code of one if the file is not tracked.

Upvotes: 7

stefanB
stefanB

Reputation: 79810

EDIT

If you need to use git from bash there is --porcelain option to git status:

--porcelain

Give the output in a stable, easy-to-parse format for scripts. Currently this is identical to --short output, but is guaranteed not to change in the future, making it safe for scripts.

Output looks like this:

> git status --porcelain
 M starthudson.sh
?? bla

Or if you do only one file at a time:

> git status --porcelain bla
?? bla

ORIGINAL

do:

git status

You will see report stating which files were updated and which ones are untracked.

You can see bla.sh is tracked and modified and newbla is not tracked:

# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   bla.sh
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       newbla
no changes added to commit (use "git add" and/or "git commit -a")

Upvotes: 22

JaredPar
JaredPar

Reputation: 754745

Try running git status on the file. It will print an error if it's not tracked by git

PS$> git status foo.txt
error: pathspec 'foo.txt' did not match any file(s) known to git.

Upvotes: 13

Related Questions