git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Is a given file known to git?

From
Shawn O. Pearce <spearce@spearce.org>
Date
Mar 11, 2008, 07:24 UTC
Message-ID
<20080311072412.GP8410@spearce.org>
In-Reply-To
<47D6317D.7030700@melosgmbh.de>
Christoph Duelli <duelli@melosgmbh.de> wrote:
Show 8 quoted lines
> Given a repository and a path p to a file in it:
> Is it possible (how?) to detect (in a bash script) if the file pointed 
> to by p is "known" to git?
> Something along the line:
> if `git knows p?
> then
> ...
> fi

Depends on your definition of "known to git". You can look at the current index, which may have files newly added but not yet committed:

	if test $(git ls-files "$p" | wc -l) -gt 0
	then
	...
	fi
you can look at a specific committed state too:
	if git cat-file -e "$commit:$p" 2>/dev/null
	then
	...
	fi

where $commit can be any tree-ish, so a tag, a branch, a commit, the symbolic-ref HEAD... etc.

-- 
Shawn.
Previous: Christoph DuelliNext: Johannes Sixt
Message 2 of 7 in “Is a given file known to git?”
  1. Christoph DuelliMar 11, 2008
  2. Shawn O. PearceMar 11, 2008
  3. Johannes SixtMar 11, 2008
  4. Christoph DuelliMar 11, 2008
  5. Johannes SixtMar 11, 2008
  6. Christoph DuelliMar 11, 2008
  7. Junio C HamanoMar 11, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.