Search code examples
gitgit-mergegit-pullgit-fetch

Is it possible to pull just one file in Git?


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.


Solution

  • 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