Is it possible to pull just one file in Git?

339,625

Solution 1

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 2

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

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

Solution 3

git checkout master -- myplugin.js

master = branch name

myplugin.js = file name

Solution 4

@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

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
Share:
339,625

Related videos on Youtube

Audrius Meškauskas
Author by

Audrius Meškauskas

A C++ developer in robotics now, ROS. Used to be Java developer for a long time.

Updated on July 08, 2022

Comments

  • Audrius Meškauskas
    Audrius Meškauskas almost 2 years

    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.

  • Audrius Meškauskas
    Audrius Meškauskas almost 11 years
    The hash codes required by -m switch can be printed with git branch -v . Awesome!
  • rd108
    rd108 over 10 years
    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.
  • Dan
    Dan over 10 years
    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
  • Marcello Nuccio
    Marcello Nuccio over 9 years
    you can get the latest remote hash from .git/refs/remotes/origin/master.
  • Dennis van der Schagt
    Dennis van der Schagt over 9 years
    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.
  • Nagappa L M
    Nagappa L M almost 9 years
    Is it possible to pull specific file without pulling other files ?
  • Wakan Tanka
    Wakan Tanka over 7 years
    @aleroot what does <revision> stands for?
  • Antony D'Andrea
    Antony D'Andrea over 6 years
    fatal: invalid reference: FETCH_HEAD
  • Dr_Zaszuś
    Dr_Zaszuś over 5 years
    This answer possibly lacks a working example. It is unclear if one needs to commit next.
  • wrangler
    wrangler over 5 years
    This just overwrites the file, doesn't merge. May be it used to work before but dosn't work now
  • aleroot
    aleroot over 5 years
    @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 ...
  • liyuan
    liyuan about 5 years
    and is there a way to revert back?
  • mati kepson
    mati kepson almost 5 years
    don't forget to 'git pull' before ;)
  • Cloud Cho
    Cloud Cho over 4 years
    @Chris do we need {remote} if we are already in Branch? If yes, why?
  • JosephK
    JosephK about 4 years
    Not the same as pull, which will attempt a merge, which is why we want to use pull.
  • trash80
    trash80 almost 3 years
    I missed the git checkout line because it's hidden between comments. Also, this says "git check" rather than "git checkout".