Skip to content

Pull one file from your Git repository

Table of Contents

Recover a singular file from a previous commit

The git restore Method (the "Modern Approach")

Using git restore is now the preferred way to recover files from a previous commit.
You can use git restore to restore a specific file from a particular commit

  1. Get the hash of the commit where the version of the file you want is located.

    git log --oneline
    

  2. Restore the file from a specific commit.

    git restore --source={commit_hash} -- {file_path}  
    

    • {commit_hash} is the hash of the commit you want to restore from.
    • {file_path} is the relative path to the file you want to restore.
  3. Stage and commit the file.

    git add {file_path} && git commit -m "fix: Restore {file_path} from {commit_hash}"
    

Done.


The fetch method

  1. Get the hash of the commit from where you want to pull your file.

    git branch -v
    

  2. Call fetch

    git fetch
    

  3. Checkout the file you want from the commit

    git checkout -m {revision} {the_file_path}
    

  4. {revision} is the hash of the commit

  5. {the_file_path} is the path to the file you want. Does not include repo name.

  6. Add and commit the file

    git add the_file_path
    git commit
    

  7. Done.

The git checkout Method

git checkout is "deprecated", but still works.

Getting a File from a Previous Commit

If you want to check out a file from a specific commit:

git checkout {commit_hash} -- {file_path}

Getting a File from the Current Branch's History

To get a file's version from the current branch in your local repo (e.g., main):

git checkout main -- {file_path}

  • Replace main with the name of the branch (if it's different).

Getting a File from a Remote Branch

If you want a file from a remote branch in a remote repo (e.g., origin/main):

git checkout origin/main -- {file_path}

  • Replace main with the name of the branch (if it's different).

Pulling with git archive

This method is a bit more verbose, but it doesn't overwrite the local version of the file you're trying to pull.

  • git archive:

    • You can use the git archive command to extract a specific file from the remote repository without pulling the whole repository.
    • git archive can also be used to create a zip/tar archive of specific files or directories.

      git archive --remote=ssh://git@{repo_url} {branch_name} {file_path} | tar -xO > {local_file_path} 
      

    • This command fetches the file at {file_path} from {branch_name} on the remote repository at {repo_url} and saves it as {local_file_path}.