Skip to contentSkip to conversation

How do I get the file contents from an arbitrary revision to stdout?

DISCUSSION Alan Mackenzie 4 participants 6 messages Oct 8, 2021

lore
Share & tools
lore
#0 lore raw

Hello, git.

I want to get a file's content from a particular revision onto stdout for backup purposes. I can't see how to do this. Help me, please!

Let's say the file is foo.c, and I want to get the version from the head revision of bar-branch.

I would like there to be a command something like:

    $ git cat bar-branch -- foo.c

.. Is there such a command, and if so, what's it called and how do I use it? I assumed it might be git cat-file, but I couldn't get it to work, and couldn't understand it's man page.

Where might I have found this information for myself?

Thanks in advance for the help!

Signature · 1 line
Alan Mackenzie (Nuremberg, Germany).
#1 lore raw
Quoted 7 lines · Alan Mackenzie

Hello, git.

I want to get a file's content from a particular revision onto stdout for backup purposes. I can't see how to do this. Help me, please!

Let's say the file is foo.c, and I want to get the version from the head revision of bar-branch.

git show bar-branch:foo.c

-K

#2 lore raw

Hello, Konstantin.

Quoted 10 lines · Konstantin Ryabitsev
Quoted 7 lines · Alan Mackenzie

Hello, git.

I want to get a file's content from a particular revision onto stdout for backup purposes. I can't see how to do this. Help me, please!

Let's say the file is foo.c, and I want to get the version from the head revision of bar-branch.

git show bar-branch:foo.c

Thank you indeed. That works just right!

Quoted 1 line

-K

Signature · 1 line
Alan Mackenzie (Nuremberg, Germany).
#3 lore raw
In reply to Alan Mackenzie
Quoted 7 lines · Alan Mackenzie

I would like there to be a command something like:

    $ git cat bar-branch -- foo.c

.. Is there such a command, and if so, what's it called and how do I use it? I assumed it might be git cat-file, but I couldn't get it to work, and couldn't understand it's man page.

Konstantin pointed you at git-show, which is what I would have used. But I just wanted to mention that you were on the right track. The invocation you wanted was:

git cat-file blob bar-branch:foo.c

or:

git cat-file -p bar-branch:foo.c

(the "-p" is "pretty-print based on the object's type", so the two are equivalent).

Quoted 1 line

Where might I have found this information for myself?

You found the cat-file manpage, which I agree is a bit thick. An Examples section would probably help a lot.

The other thing that might have helped is the gitrevisions(7) page, especially the <rev>:<path> entry. Your example above to use "<revs> -- <pathspec>" was a good thought, but the path there is for limiting diffs and traversals. What you want here is to specify the name of a single object, and gitrevisions gives all the ways to do that.

-Peff

#4 lore raw
Quoted 4 lines · Jeff King

git cat-file -p bar-branch:foo.c

(the "-p" is "pretty-print based on the object's type", so the two are equivalent).

IIRC, "cat-file -p" doesn't do textconv, but "show" would, and depending on the use case the difference may matter.

#5 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 4 lines · Jeff King

git cat-file -p bar-branch:foo.c

(the "-p" is "pretty-print based on the object's type", so the two are equivalent).

IIRC, "cat-file -p" doesn't do textconv, but "show" would, and depending on the use case the difference may matter.

Sorry, I should have been more clear. I meant that the two cat-file invocations are equivalent (using "blob" versus "-p").

I agree that for human consumption, "show" is preferable.

-Peff