threads / discuss / 56356

git read-tree doesn't accept tree-ish

Subject: git read-tree doesn't accept tree-ish

## tl;dr

7 messages between Aug 23, 2021 and Aug 23, 2021.

replies: 6people: 4as markdown or json

Nikita Bobko· Aug 23, 2021, 17:31 UTC · lore

``` git init echo foo > foo git add --all git commit -m 'foo'

echo bar > foo git add --all git commit -m 'bar'

git read-tree HEAD~:foo
```
EXP: Read information about file `foo` into index
ACT: `fatal: failed to unpack tree object HEAD~:foo`

I expect it to work because manpage says that `git-read-tree` accepts `<tree-ish>`

Elijah Newren· Aug 23, 2021, 17:38 UTC · re: Nikita Bobko · lore

Re: git read-tree doesn't accept tree-ish

On Mon, Aug 23, 2021 at 10:32 AM Nikita Bobko <nikitabobko@gmail.com> wrote:
Show 18 quoted lines
>
> ```
> git init
> echo foo > foo
> git add --all
> git commit -m 'foo'
>
> echo bar > foo
> git add --all
> git commit -m 'bar'
>
> git read-tree HEAD~:foo
> ```
> EXP: Read information about file `foo` into index
> ACT: `fatal: failed to unpack tree object HEAD~:foo`
>
> I expect it to work because manpage says that `git-read-tree` accepts
> `<tree-ish>`

HEAD~:foo is not a tree in your example, it's a blob. Try running this: `git cat-file -t HEAD~:foo`, to see the type.

Jeff King· Aug 23, 2021, 18:53 UTC · re: Nikita Bobko · lore

Re: git read-tree doesn't accept tree-ish

On Mon, Aug 23, 2021 at 07:31:29PM +0200, Nikita Bobko wrote:
Show 17 quoted lines
> ```
> git init
> echo foo > foo
> git add --all
> git commit -m 'foo'
> 
> echo bar > foo
> git add --all
> git commit -m 'bar'
> 
> git read-tree HEAD~:foo
> ```
> EXP: Read information about file `foo` into index
> ACT: `fatal: failed to unpack tree object HEAD~:foo`
> 
> I expect it to work because manpage says that `git-read-tree` accepts
> `<tree-ish>`

In your example, "foo" is a blob, not a tree. And thus HEAD~:foo is not a tree-ish. Just "HEAD~" would be.

What are you trying to do?
-Peff
Nikita Bobko· Aug 23, 2021, 19:10 UTC · re: Jeff King · lore

Re: git read-tree doesn't accept tree-ish

Yes, now I see. Thanks!

I was trying to read state of a particular directory/file from a particular revision into my index but so far didn't manage to do it reliably.

I was using `git read-tree <hash>:<path>` and it doesn't work for files (because they are blobs not tree-ish as you elaborated it to me) and for directories, in my cases, it fails with not so helpful message: ``` error: Entry '<path>/<subpath>' overlaps with '<path>/<subpath>'. Cannot bind. ```

`git checkout <hash> -- <path>` also doesn't work in my case because if any file is removed in `<hash>` but not in my HEAD then `git checkout` doesn't remove the file in my HEAD

On Mon, Aug 23, 2021 at 8:53 PM Jeff King <peff@peff.net> wrote:
Show 27 quoted lines
>
> On Mon, Aug 23, 2021 at 07:31:29PM +0200, Nikita Bobko wrote:
>
> > ```
> > git init
> > echo foo > foo
> > git add --all
> > git commit -m 'foo'
> >
> > echo bar > foo
> > git add --all
> > git commit -m 'bar'
> >
> > git read-tree HEAD~:foo
> > ```
> > EXP: Read information about file `foo` into index
> > ACT: `fatal: failed to unpack tree object HEAD~:foo`
> >
> > I expect it to work because manpage says that `git-read-tree` accepts
> > `<tree-ish>`
>
> In your example, "foo" is a blob, not a tree. And thus HEAD~:foo
> is not a tree-ish. Just "HEAD~" would be.
>
> What are you trying to do?
>
> -Peff
Elijah Newren· Aug 23, 2021, 19:23 UTC · re: Nikita Bobko · lore

Re: git read-tree doesn't accept tree-ish

Hi,
On Mon, Aug 23, 2021 at 12:09 PM Nikita Bobko <nikitabobko@gmail.com> wrote:
Show 6 quoted lines
>
> Yes, now I see. Thanks!
>
> I was trying to read state of a particular directory/file from a
> particular revision into my index but so far didn't manage to do it
> reliably.
Please don't top-post on this list.
Show 11 quoted lines
> I was using `git read-tree <hash>:<path>` and it doesn't work for
> files (because they are blobs not tree-ish as you elaborated it to me)
> and for directories, in my cases, it fails with not so helpful
> message:
> ```
> error: Entry '<path>/<subpath>' overlaps with '<path>/<subpath>'.  Cannot bind.
> ```
>
> `git checkout <hash> -- <path>` also doesn't work in my case because
> if any file is removed in `<hash>` but not in my HEAD then `git
> checkout` doesn't remove the file in my HEAD
Have you tried `git restore --source=<hash> -- <path>` ?
Junio C Hamano· Aug 23, 2021, 19:27 UTC · re: Nikita Bobko · lore

Re: git read-tree doesn't accept tree-ish

Nikita Bobko <nikitabobko@gmail.com> writes:
> `git checkout <hash> -- <path>` also doesn't work in my case because
> if any file is removed in `<hash>` but not in my HEAD then `git
> checkout` doesn't remove the file in my HEAD
Look for "--no-overlay" option in "git checkout", perhaps?
Nikita Bobko· Aug 23, 2021, 19:38 UTC · re: Junio C Hamano · lore

Re: git read-tree doesn't accept tree-ish

> Have you tried `git restore --source=<hash> -- <path>` ?
At a glance, it looks what I am looking for
> Look for "--no-overlay" option in "git checkout", perhaps?
Oh, and it works too. So many ways to do almost exactly the same thing...

Anyway, thanks everyone for the help! My issue may be considered as resolved. Now I will dig into manpages and experiments to see which of the suggested variants suits me best

On Mon, Aug 23, 2021 at 9:27 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 8 quoted lines
>
> Nikita Bobko <nikitabobko@gmail.com> writes:
>
> > `git checkout <hash> -- <path>` also doesn't work in my case because
> > if any file is removed in `<hash>` but not in my HEAD then `git
> > checkout` doesn't remove the file in my HEAD
>
> Look for "--no-overlay" option in "git checkout", perhaps?

← back to recent threads