243

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.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93

7 Answers7

261

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

git fetch {remote}
git checkout FETCH_HEAD -- {file}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris
  • 3,833
  • 3
  • 14
  • 11
219

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
xpt
  • 20,363
  • 37
  • 127
  • 216
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 26
    The hash codes required by -m switch can be printed with git branch -v . Awesome! – Audrius Meškauskas Apr 30 '13 at 07:46
  • 3
    Excellent. This has helped me on more than one occasion. To clarify, the -m flag seems to be happy with the hash of the commit from which you want to pull your single file. – rd108 Nov 14 '13 at 22:36
  • 1
    This worked for me as well. Though, I ran git log on my remote branch to find the reversion: e.g. $ git log remotes/origin/master – Dan Dec 23 '13 at 17:50
  • 1
    you can get the latest remote hash from `.git/refs/remotes/origin/master`. – Marcello Nuccio Jul 24 '14 at 08:40
  • Git automatically adds the file that is being checked out to the index (in the version that I use: `1.9.4.msysgit.1`). So, the third step is not needed but it doesn't do any harm. – Dennis van der Schagt Jan 12 '15 at 23:36
  • 7
    Is it possible to pull specific file without pulling other files ? – Nagappa L M May 13 '15 at 10:53
  • 3
    @aleroot what does stands for? – Wakan Tanka Dec 01 '16 at 14:11
  • 1
    This just overwrites the file, doesn't merge. May be it used to work before but dosn't work now – wrangler Dec 04 '18 at 14:01
  • @wrangler the solution work as expected and solve the problem of the OP since he has accepted the answer... If you have other requirements than the OP then it would be better to ask a specific question instead of downvoting and bad commenting an accepted answer ... – aleroot Dec 04 '18 at 15:58
56
git checkout master -- myplugin.js

master = branch name

myplugin.js = file name

Mawardy
  • 3,618
  • 2
  • 33
  • 37
38

@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}
Luke Flournoy
  • 3,393
  • 2
  • 16
  • 22
5

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
Shawn Ashton
  • 357
  • 5
  • 10
1

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
  • I missed the git checkout line because it's hidden between comments. Also, this says "git check" rather than "git checkout". – trash80 May 19 '21 at 12:39
0

To pull a specific file from a Git repository, you can use the git checkout command with the path to the file. Here's the syntax:

git checkout <commit> -- <file-path>

Replace <commit> with the commit hash, branch name, or tag name that contains the desired version of the file. Replace <file-path> with the path to the specific file you want to pull.

Here's an example to illustrate the usage:

git checkout main -- path/to/file.txt

In this example, it pulls the file named file.txt from the main branch.

If you want to update your local repository to the latest version of the file on the remote repository, you can run the git pull command after checking out the specific file:

git pull origin main

Replace origin with the appropriate remote name and main with the branch name you want to pull updates from.

Note: If you have any uncommitted changes in your local repository, it's recommended to stash or commit them before performing the checkout operation to avoid conflicts.

QAscoop
  • 9
  • 2