Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 567
Dale Forester Avatar asked Mar 08 '10 22:03

Dale Forester


People also ask

Which git option is used to see whether a file is already tracked?

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

Which type of file is tracked by git?

Tracked files are files that were in the last snapshot, as well as any newly staged files; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about.

Which command provides the list of tracked files in git?

ls-tree will output all the versioned files.


2 Answers

try:

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

will exit with 1 if file is not tracked

like image 191
hasen Avatar answered Oct 20 '22 00:10

hasen


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 } 
like image 42
cheesus Avatar answered Oct 19 '22 23:10

cheesus