How to retrieve a single file from a specific revision in Git?

后端 未结 10 1267
臣服心动
臣服心动 2020-11-22 15:50

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<

相关标签:
10条回答
  • 2020-11-22 16:18

    Get the file from a previous commit through checking-out previous commit and copying file.

    • Note which branch you are on: git branch
    • Checkout the previous commit you want: git checkout 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8
    • Copy the file you want to a temporary location
    • Checkout the branch you started from: git checkout theBranchYouNoted
    • Copy in the file you placed in a temporary location
    • Commit your change to git: git commit -m "added file ?? from previous commit"
    0 讨论(0)
  • 2020-11-22 16:22

    This will help you get all deleted files between commits without specifying the path, useful if there are a lot of files deleted.

    git diff --name-only --diff-filter=D $commit~1 $commit | xargs git checkout $commit~1
    
    0 讨论(0)
  • 2020-11-22 16:23

    The easiest way is to write:

    git show HASH:file/path/name.ext > some_new_name.ext
    

    where:

    • HASH is the Git revision SHA-1 hash number
    • file/path/name.ext is name of the file you are looking for
    • some_new_name.ext is path and name where the old file should be saved

    Example

    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
    
    0 讨论(0)
  • You need to provide the full path to the file:

    git show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8:full/repo/path/to/my_file.txt
    
    0 讨论(0)
提交回复
热议问题