I want to extract a copy of the latest version of a file held in a git repository, and pass it into a script for some processing. With svn or hg, I just use the "cat" command:
Print the specified files as they were at the given revision. If no revision is given, the parent of the working directory is used, or tip if no revision is checked out.
(that's from the description of hg cat in the hg documentation)
What's the equivalent command to do this with git?
The cat (short for “concatenate“) command is one of the most frequently used commands in Linux/Unix-like operating systems. cat command allows us to create single or multiple files, view content of a file, concatenate files and redirect output in terminal or files.
Git's cat-file doesn't really stand for "concatenate"; it simply is a reference to the behavior of the cat command. If --batch or --batch-check is given, cat-file will read objects from stdin, one per line, and print information about them.
git show rev:path/to/file
Where rev is the revision.
See http://git.or.cz/course/svn.html for a comparison of git and svn commands.
there is "git cat-file" which you can run like this:
$ git cat-file blob v1.0:path/to/file
where you can replace 'v1.0' with the branch, tag or commit SHA you want and then 'path/to/file' with the relative path in repository. You can also pass '-s' to see the size of the content if you want.
might be closer to the 'cat' commands you are used to, though the previously mentioned 'show' will do much the same thing.
git show
is the command you are looking for. From the documentation:
git show next~10:Documentation/README
Shows the contents of the file Documentation/README as they were
current in the 10th last commit of the branch next.
Also work with branch names (like HEAD in the 1st p) :
git show $branch:$filename
Use git show
, as in git show commit_sha_id:path/to/some/file.cs
.
I wrote a git cat shell script, which is up on github
There doesn't appear to be a direct substitute. This blog entry details how to do the equivalent by determining the latest commit, then determining the hash for the file in that commit, and then dumping it out.
git log ...
git ls-tree ...
git show -p ...
(the blog entry has typos and uses the above with the command svn
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With