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:

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.

  • Issue with SVN Commit for certain File Extension
  • Tortoise SVN : How to ignore bin contents from commit
  • Internet connection Drops during an SVN commit
  • Speed up SVN with local repository?
  • EGIT - after merging the second time still got same conflicts
  • How to migrate from SVN to GIT and keep history for moved files from different projects
  • SVN change username
  • Using git-svn to merge a svn branch back into trunk and trunk back into the branch
  • 2 Solutions collect form web for “Get a file from a git repository at a specific revision number”

    Other commands equivalent to svn cat:

    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. 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>
    Git Baby is a git and github fan, let's start git clone.