I have a Git repository and I'd like to see how some files looked a few months ago. I found the revision at that date; it's 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8
. I need to see what one file looks like, and also save it as a ("new") file.
I managed to see the file using gitk
, but it doesn't have an option to save it. I tried with command-line tools, the closest I got was:
git-show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8 my_file.txt
However, this command shows a diff, and not the file contents. I know I can later use something like PAGER=cat
and redirect output to a file, but I don't know how to get to the actual file content.
Basically, I'm looking for something like svn cat.
git checkout origin/master -- path/to/file // git checkout <local repo name (default is origin)>/<branch name> -- path/to/file will checkout the particular file from the downloaded changes (origin/master).
You can't clone a single file using git. Git is a distributed version control system, the Idea behind its clone functionality is to have a complete copy of project and all versions of files related to that project.
How do I pull a specific commit? The short answer is: you cannot pull a specific commit from a remote. However, you may fetch new data from the remote and then use git-checkout COMMIT_ID to view the code at the COMMIT_ID .
If you wish to replace/overwrite the content of a file in your current branch with the content of the file from a previous commit or a different branch, you can do so with these commands:
git checkout 08618129e66127921fbfcbc205a06153c92622fe path/to/file.txt
or
git checkout mybranchname path/to/file.txt
You will then have to commit those changes in order for them to be effective in the current branch.
git show
To complete your own answer, the syntax is indeed
git show object
git show $REV:$FILE
git show somebranch:from/the/root/myfile.txt
git show HEAD^^^:test/test.py
The command takes the usual style of revision, meaning you can use any of the following:
HEAD
+ x number of ^
charactersTip It's important to remember that when using "git show
", always specify a path from the root of the repository, not your current directory position.
(Although Mike Morearty mentions that, at least with git 1.7.5.4, you can specify a relative path by putting "./
" at the beginning of the path. For example:
git show HEAD^^:./test.py
)
git restore
With Git 2.23+ (August 2019), you can also use git restore
which replaces the confusing git checkout
command
git restore -s <SHA1> -- afile
git restore -s somebranch -- afile
That would restore on the working tree only the file as present in the "source" (-s
) commit SHA1 or branch somebranch
.
To restore also the index:
git restore -s <SHA1> -SW -- afile
(-SW
: short for --staged --worktree
)
As noted in the comments by starwarswii
It lets you pipe the contents into a file, which is great if you want to just quickly compare files from a commit.
E.g. you can do:
git show 1234:path/to/file.txt > new.txt git show 1234~:path/to/file.txt > old.txt
then compare them.
Before git1.5.x, this was done with some plumbing:
git ls-tree <rev>
show a list of one or more 'blob' objects within a commit
git cat-file blob <file-SHA1>
cat a file as it has been committed within a specific revision (similar to svn
cat).
use git ls-tree
to retrieve the value of a given file-sha1
git cat-file -p $(git-ls-tree $REV $file | cut -d " " -f 3 | cut -f 1)::
git-ls-tree
lists the object ID for $file
in revision $REV
, this is cut out of the output and used as an argument to git-cat-file
, which should really be called git-cat-object
, and simply dumps that object to stdout
.
Note: since Git 2.11 (Q4 2016), you can apply a content filter to the git cat-file
output.
See
commit 3214594,
commit 7bcf341 (09 Sep 2016),
commit 7bcf341 (09 Sep 2016), and
commit b9e62f6,
commit 16dcc29 (24 Aug 2016) by Johannes Schindelin (dscho
).
(Merged by Junio C Hamano -- gitster
-- in commit 7889ed2, 21 Sep 2016)
git config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <"
git cat-file --textconv --batch
Note: "git cat-file --textconv
" started segfaulting recently (2017), which has been corrected in Git 2.15 (Q4 2017)
See commit cc0ea7c (21 Sep 2017) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
-- in commit bfbc2fc, 28 Sep 2017)
You need to provide the full path to the file:
git show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8:full/repo/path/to/my_file.txt
The easiest way is to write:
git show HASH:file/path/name.ext > some_new_name.ext
where:
git show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8:my_file.txt > my_file.txt.OLD
This will save my_file.txt from revision 27cf8e as a new file with name my_file.txt.OLD
It was tested with Git 2.4.5.
If you want to retrieve deleted file you can use HASH~1
(one commit before specified HASH).
EXAMPLE:
git show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8~1:deleted_file.txt > deleted_file.txt
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