Git: How to pull a single file from a server repository in Git?

后端 未结 8 1093
小蘑菇
小蘑菇 2021-01-29 17:57

I am working on a site with a server running Git. I am using Git for deployment (not GitHub). This was set up prior to my involvement using a hook method, and I referred to this

相关标签:
8条回答
  • 2021-01-29 18:21
    git fetch --all
    git checkout origin/master -- <your_file_path>
    git add <your_file_path>
    git commit -m "<your_file_name> updated"
    

    This is assuming you are pulling the file from origin/master.

    0 讨论(0)
  • 2021-01-29 18:24

    Try using:

    git checkout branchName -- fileName
    

    Ex:

    git checkout master -- index.php
    
    0 讨论(0)
  • 2021-01-29 18:28

    It is possible to do (in the deployed repository):

    git fetch
    // git fetch will download all the recent changes, but it will not put it in your current checked out code (working area).
    

    Followed by:

    git checkout origin/master -- path/to/file
    // git checkout <local repo name (default is origin)>/<branch name> -- path/to/file will checkout the particular file from the downloaded changes (origin/master).
    
    0 讨论(0)
  • 2021-01-29 18:28
    https://raw.githubusercontent.com/[USER-NAME]/[REPOSITORY-NAME]/[BRANCH-NAME]/[FILE-PATH]
    

    Ex. https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php

    Through this you would get the contents of an individual file as a row text. You can download that text with wget.

    Ex. https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php

    0 讨论(0)
  • 2021-01-29 18:30

    This scenario comes up when you -- or forces greater than you -- have mangled a file in your local repo and you just want to restore a fresh copy of the latest version of it from the repo. Simply deleting the file with /bin/rm (not git rm) or renaming/hiding it and then issuing a git pull will not work: git notices the file's absence and assumes you probably want it gone from the repo (git diff will show all lines deleted from the missing file).

    git pull not restoring locally missing files has always frustrated me about git, perhaps since I have been influenced by other version control systems (e.g. svn update which I believe will restore files that have been locally hidden).

    git reset --hard HEAD is an alternative way to restore the file of interest as it throws away any uncommitted changes you have. However, as noted here, git reset is is a potentially dangerous command if you have any other uncommitted changes that you care about.

    The git fetch ... git checkout strategy noted above by @chrismillah is a nice surgical way to restore the file in question.

    0 讨论(0)
  • 2021-01-29 18:32

    This windows batch works regardless of whether or not it's on GitHub. I'm using it because it shows some stark caveats. You'll notice that the operation is slow and traversing hundreds of megabytes of data, so don't use this method if your requirements are based on available bandwidth/R-W memory.

    sparse_checkout.bat

    pushd "%~dp0"
    if not exist .\ms-server-essentials-docs mkdir .\ms-server-essentials-docs
    pushd .\ms-server-essentials-docs
    git init
    git remote add origin -f https://github.com/MicrosoftDocs/windowsserverdocs.git
    git config core.sparseCheckout true
    (echo EssentialsDocs)>>.git\info\sparse-checkout
    git pull origin master
    

    =>

    C:\Users\user name\Desktop>sparse_checkout.bat

    C:\Users\user name\Desktop>pushd "C:\Users\user name\Desktop\"

    C:\Users\user name\Desktop>if not exist .\ms-server-essentials-docs mkdir .\ms-server-essentials-docs

    C:\Users\user name\Desktop>pushd .\ms-server-essentials-docs

    C:\Users\user name\Desktop\ms-server-essentials-docs>git init Initialized empty Git repository in C:/Users/user name/Desktop/ms-server-essentials-docs/.git/

    C:\Users\user name\Desktop\ms-server-essentials-docs>git remote add origin -f https://github.com/MicrosoftDocs/windowsserverdocs.git Updating origin remote: Enumerating objects: 97, done. remote: Counting objects: 100% (97/97), done. remote: Compressing objects: 100% (44/44), done. remote: Total 145517 (delta 63), reused 76 (delta 53), pack-reused 145420 Receiving objects: 100% (145517/145517), 751.33 MiB | 32.06 MiB/s, done. Resolving deltas: 100% (102110/102110), done. From https://github.com/MicrosoftDocs/windowsserverdocs * [new branch]
    1106-conflict -> origin/1106-conflict * [new branch]
    FromPrivateRepo -> origin/FromPrivateRepo * [new branch]
    PR183 -> origin/PR183 * [new branch]
    conflictfix -> origin/conflictfix * [new branch]
    eross-msft-patch-1 -> origin/eross-msft-patch-1 * [new branch]
    master -> origin/master * [new branch] patch-1
    -> origin/patch-1 * [new branch] repo_sync_working_branch -> origin/repo_sync_working_branch * [new branch]
    shortpatti-patch-1 -> origin/shortpatti-patch-1 * [new branch]
    shortpatti-patch-2 -> origin/shortpatti-patch-2 * [new branch]
    shortpatti-patch-3 -> origin/shortpatti-patch-3 * [new branch]
    shortpatti-patch-4 -> origin/shortpatti-patch-4 * [new branch]
    shortpatti-patch-5 -> origin/shortpatti-patch-5 * [new branch]
    shortpatti-patch-6 -> origin/shortpatti-patch-6 * [new branch]
    shortpatti-patch-7 -> origin/shortpatti-patch-7 * [new branch]
    shortpatti-patch-8 -> origin/shortpatti-patch-8

    C:\Users\user name\Desktop\ms-server-essentials-docs>git config core.sparseCheckout true

    C:\Users\user name\Desktop\ms-server-essentials-docs>(echo EssentialsDocs ) 1>>.git\info\sparse-checkout

    C:\Users\user name\Desktop\ms-server-essentials-docs>git pull origin master
    From https://github.com/MicrosoftDocs/windowsserverdocs
    * branch master -> FETCH_HEAD

    0 讨论(0)
提交回复
热议问题