Is it possible to pull just one file in Git?

后端 未结 5 969
北海茫月
北海茫月 2020-11-30 16:43

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

相关标签:
5条回答
  • 2020-11-30 16:54

    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

    0 讨论(0)
  • 2020-11-30 16:57

    @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}
    
    0 讨论(0)
  • 2020-11-30 16:58
    git checkout master -- myplugin.js
    

    master = branch name

    myplugin.js = file name

    0 讨论(0)
  • 2020-11-30 17:09

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

    git fetch {remote}
    git checkout FETCH_HEAD -- {file}
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题