Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show the contents of a file at a specific state of a git repo?

Tags:

git

I want to show the contents of a file given by a path at a specific state of a git repo. I unsuccessfully tried this:

git show f825334150cd4bc8f46656b2daa8fa1e92f7796d:Katana/source/Git/GitLocalBranch.h fatal: ambiguous argument 'f825334150cd4bc8f46656b2daa8fa1e92f7796d:Katana/source/Git/GitLocalBranch.h': unknown revision or path not in the working tree. Use '--' to separate paths from revisions 

The commit in question didn't modify the file specified. How can I show the contents of a file at a given state (specified by a commit hash) regardless of the involvement of the file in the commit?

like image 762
richcollins Avatar asked Mar 23 '10 01:03

richcollins


People also ask

How do I show the contents of a file in git?

The Git Show command allows us to view files as they existed in a previous state. The version can be a commit ID, tag, or even a branch name. The file must be the path to a file. For example, the following would output a contents of a file named internal/example/module.go file from a tagged commit called “release-23”.

What are the 3 git states that a file can be in?

Git has three main states that your files can reside in: modified, staged, and committed: Modified means that you have changed the file but have not committed it to your database yet. Staged means that you have marked a modified file in its current version to go into your next commit snapshot.

How do I check my git state?

To check the status, open the git bash, and run the status command on your desired directory. It will run as follows: $ git status.


2 Answers

It's probably problem with your path specification.

This works, shows version of Makefile in commit b1b22df407417...

git show b1b22df407417:Makefile 

Or current version in master branch

git show master:Makefile 

Or current version in exper branch:

git show exper:Makefile 

Or previous version on the exper branch:

git show exper^:Makefile 

And so on

like image 174
stefanB Avatar answered Oct 03 '22 15:10

stefanB


The syntax you're using matches that shown in the examples for the git show manpage, but git seems to be hinting that you should specify like this:

# I _don't_ think this is your answer... git show f825334150 -- Katana/source/Git/GitLocalBranch.h 

which I've definitely used for git log and is in its manpage.

My gut, though, tells me that you using an absolute path and not the path inside the top of your git work tree. You need to make sure that if your .git directory is at Katana/source/Git/.git, then you chop off everything before the .git, like so:

git show f825334150:GitLocalBranch.h 

If you're trying to show a git blob from outside the git working area, you need to do something like this:

GIT_DIR=Katana/source/Git git show f825334150:GitLocalBranch.h 

This will tell git where it can find the data for the your repository.

Bottom line: double-check your paths and make sure they're right. You might need to set a GIT_DIR if you're not running your command from inside the git work area.

like image 28
Mike Seplowitz Avatar answered Oct 03 '22 14:10

Mike Seplowitz