Is it possible to pull just one file in Git?

GitGit MergeGit PullGit Fetch

Git Problem Overview


I am working on a Git branch that has some broken tests, and I would like to pull (merge changes, not just overwrite) these tests from another branch where they are already fixed.

I know I can do

git pull origin that_other_branch

but this will attempt to merge lots of other files, for that I am not yet ready.

Is it possible to pull and merge only the specified file (and not everything) from that another branch?

This is not a duplicate of Git pull request for just one file as all answers to that question are how to revert the locally changed file to the repository version, without changing any branches.

Git Solutions


Solution 1 - Git

Here is a slightly easier method I just came up with when researching this:

git fetch {remote}
git checkout FETCH_HEAD -- {file}

Solution 2 - Git

You can fetch and then check out only one file in this way:

git fetch
git checkout -m <revision> <yourfilepath>
git add <yourfilepath>
git commit

Regarding the git checkout command:

  • <revision> -- a branch name, i.e. origin/master
  • <yourfilepath> does not include the repository name (that you can get from clicking copy path button on a file page on GitHub), i.e. README.md

Solution 3 - Git

git checkout master -- myplugin.js

> master = branch name

> myplugin.js = file name

Solution 4 - Git

@Mawardy's answer worked for me, but my changes were on the remote so I had to specify the origin

git checkout origin/master -- {filename}

Solution 5 - Git

Yes, here is the process:

# Navigate to a directory and initiate a local repository
git init        

# Add remote repository to be tracked for changes:   
git remote add origin https://github.com/username/repository_name.git

# Track all changes made on above remote repository
# This will show files on remote repository not available on local repository
git fetch

# Add file present in staging area for checkout
git check origin/master -m /path/to/file
# NOTE: /path/to/file is a relative path from repository_name
git add /path/to/file

# Verify track of file(s) being committed to local repository
git status

# Commit to local repository
git commit -m "commit message"

# You may perform a final check of the staging area again with git status

Solution 6 - Git

In order to overwrite the local file with the file from some other repo or branch, the command is...

git checkout remoteName/branchName filePath

Here is an example I previously used...

git checkout origin/master package-lock.json

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAudrius MeškauskasView Question on Stackoverflow
Solution 1 - GitChrisView Answer on Stackoverflow
Solution 2 - GitalerootView Answer on Stackoverflow
Solution 3 - GitMawardyView Answer on Stackoverflow
Solution 4 - GitLuke FlournoyView Answer on Stackoverflow
Solution 5 - Gituser9652688View Answer on Stackoverflow
Solution 6 - GitShawn AshtonView Answer on Stackoverflow