How to get a copy of an older version of a file in a git repository?

前端 未结 3 361
遥遥无期
遥遥无期 2020-12-23 16:13

I have a version of a .tex file from a number of commits ago that I would like to get a copy of. I have the sha1 hash value for the commit that has the version

相关标签:
3条回答
  • 2020-12-23 17:02

    Use git show with absolute repository paths:

    workdir$ git show revision:repo/module/package/code.file > code.file.old

    or paths relative to the current directory:

    package$ git show revision:./code.file > workdir/code.file.old

    In contrast to checking out a file, show does not intrinsically change your workspace, you can do anything you like with the output.

    Credit to this answer, and of course see full details in the manual.

    0 讨论(0)
  • 2020-12-23 17:03

    I think that the best solution is to overwrite temporally your file. In your top-level of your repository:

    git checkout <sha1> file.tex
    cp file.tex directory
    git checkout HEAD file.tex
    
    0 讨论(0)
  • 2020-12-23 17:12

    You can use git cat-file to dump the contents of the file to the standard output and redirect that into your desired destination:

    git cat-file -p <sha1>:./file.tex > wherever.tex
    

    The ./ is necessary if you are in a subdirectory of the repository, if you're in the top-level of the repository it may be omitted. Also, that may not work in older versions of git, in which case you'd need to explicitly supply the full path to the file relative to the repository's root.

    0 讨论(0)
提交回复
热议问题