Get a file from a git repository at a specific revision number
I have a Git repository in which there files are committed through only one branch. I just use these commands:
git init git add -A git commit -m "msg"
I want to get a specific revision of the file. This revision is specified with a number. Currently I use this:
- git-svn branching
- How to set up svn conflict resolution with meld?
- Fusing two git svn repos at point of divergence
- Subversion: Get ip-address of user in pre-commit hook?
- include multiple folders using svndumpfilter
- Drawbacks of using GIT-SVN over other SVN clients?
if total number of revisions is 1:
git show HEAD:path
and if the revision number is > 1, then:
git show HEAD~n:path
However, when I run the code there are some cases in which I get an error saying something like: Invalid object name: HEADn (where n is a number). In subversion I used the
svn cat command.
Since I update the repository constantly, I don’t want to checkout the repository at a revision if possible.
2 Solutions collect form web for “Get a file from a git repository at a specific revision number”
Other commands equivalent to
git cat-file blob ref:path/to/file git show ref:path/to/some/file.cs.
See Specifying Revisions for replacing ref by (a SHA1, a tag, a branch, … or, as you are using,
<rev>~<n>, e.g. master~3)
The main thing to remember about git, is that everything is based around your commits. A commit hash identifies it, and tags, branches are just additional mechanisms to identify a commit, though in the case of a branch its not static, it moves with the HEAD.
so..in order to checkout the file at a particular revision, you just need to find what commit that revision was in, you can look at the history of a particular file with this…
git log --no-merges --oneline -- <path>
or if you are using Git Extensions or other GUI there will be a file history command to show you visually.
Once you know what the commit hash is you can checkout the file at that point using
git checkout <hash>:<path>