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

Re: get commit ID from a tree object ID

From
KKKonstantin Khomoutov <kostix@bswap.ru>
Date
Mar 17, 2018, 13:01 UTC
Message-ID
<20180317130128.mr2sfsrncykkubwo@tigra>
In-Reply-To
<CANT8FXR3pMgbwVWmwssDYhgv6eMFw4+Uz5xARGwFVVU1cMVmWw@mail.gmail.com>
On Sat, Mar 17, 2018 at 01:17:12PM +0100, Michal Novotny wrote:
Show 19 quoted lines
> let's say I have made an annotated tag on a certain treeish:
> 
> $ git tag -a -m msg tagname HEAD:
> 
> Now, I can try to see the content of the tag:
> 
> $ git tag -v tagname
> object 42a1c36553a50ceae2f75ffc4b1446c6c393eae7
> type tree
> tag tagname
> tagger clime <clime@redhat.com> 1521288727 +0100
> 
> msg
> error: no signature found
> 
> 
> Can I use that object ID 42a1c36553a50ceae2f75ffc4b1446c6c393eae7 to
> get back to a particular commit from which the tag was created? The
> reason is that I would eventually like to checkout that commit.

In general, you can't, and that's because there can be any number of commits referencing that tree. A typical case is a tree object representing a subdirectory of your project which changes rarily, if ever - in this case, each commit which includes the same state of this subdirectory will refer the same tree object.

Another point to consider is that the commit itself only refers to a single tree object -- that one which records the state of the top-level project directory, and it usually refers to other three objects which may, in turn, refer to others and so on - all the way down.

So actually a generic approach to what you need is a full scan of all the commits in the repository with recursive traversing of the hierarchy of trees of each of them (via `git ls-tree`) and looking for the SHA-1 name of the reference tree object. As you can see, this is not going to be fast on repos of realistic size.

Previous: Michal NovotnyNext: Jeff King
Message 2 of 6 in “get commit ID from a tree object ID”
  1. Michal NovotnyMar 17, 2018
  2. Konstantin KhomoutovMar 17, 2018
  3. Jeff KingMar 17, 2018
  4. Junio C HamanoMar 17, 2018
  5. Stefan BellerMar 26, 2018
  6. Jonathan NiederMar 26, 2018

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.