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
-
Get the hash of the commit where the version of the file you want is located.
git log --oneline
-
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.
-
Stage and commit the file.
git add {file_path} && git commit -m "fix: Restore {file_path} from {commit_hash}"
Done.
The fetch method¶
- Get the hash of the commit from where you want to pull your file.
git branch -v
- Call fetch
git fetch
- Checkout the file you want from the commit
git checkout -m {revision} {the_file_path}
{revision}
is the hash of the commit{the_file_path}
is the path to the file you want. Does not include repo name.- Add and commit the file
git add the_file_path git commit
- 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}
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}
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}
{file_path}
from {branch_name}
on the remote repository at {repo_url}
and saves it as {local_file_path}
.
Other Relevant Git Commands¶
-
git stash
: Temporarily saves changes that you want to set aside.- This can be used to restore a file without losing your local changes.
git stash push -m "Saving current work"
- This can be used to restore a file without losing your local changes.
-
git reflog
: Use when you want to see all actions taken in the repo, even those not visible ingit log
.- Great for finding older commit hashes.
git reflog
- Great for finding older commit hashes.
-
git reset
: Resets the currentHEAD
to a specific commit or state.- This can be dangerous, but is powerful for undoing changes.
git reset --hard {commit_hash}
--hard
discards the changes of the commit specified by{commit_hash}
.--soft
will keep the changes of the commit specified by{commit_hash}
.- This leaves the changes as uncommitted changes.
- This can be dangerous, but is powerful for undoing changes.