How do I add file to remote Git repo (Github) without cloning the whole repo first

后端 未结 3 1033
难免孤独
难免孤独 2020-12-15 05:25

This Git question is very similar to another one about SVN.

I have a repo full of big files and I need to add a single file to it. This used to be very easy in SVN.<

相关标签:
3条回答
  • 2020-12-15 05:35

    The repo includes a collection of rather fatty binary files

    That is not what a git repo is designed to do, as I explained here.

    You can't push something without having fetched first, so unless your repo is hosted in a service which supports adding files through its web interface (like GitHub, as commented by heinrich5991), you won't be able to import just one file.

    One alternative would be to have a dedicated server hosting a full copy of your repo at all time, to which you could add one file, and from which you could then push to the target repo.

    0 讨论(0)
  • 2020-12-15 05:54

    There is no way to do that. I just made a test following:

    1. mkdir hello_independent (note:"hello" is my previous repository's name)
    2. git init (note:create a repository with blank here)
    3. git remote set-url origin https://github.com/solio/GitPractice.git (note:set the local repository point to the remote repository.)
    4. vim newfilefromindependentfolder.md (note:add a new file)
    5. git add --a
    6. git commit -m "test commit from independent folder."
    7. git push origin master (note:this operation is the key)

    it will show this error:(sorry I can't upload the image of my operation and hint message without 10 reputation)

    here is my comprehension of the message:when I do this there are two different independent workflow,they have nothing relationship.So when you want to commit the independent files to the remote repository you must merge the local repository and the remote.So the best way to solve this is git clone the remote repository.

    0 讨论(0)
  • 2020-12-15 05:56

    For GitHub at least (not git itself), the GitHub API provides a way to create a file without cloning:

    https://developer.github.com/v3/repos/contents/#create-a-file

    PUT /repos/:owner/:repo/contents/:path
    
    # Parameters #
    ----------------------------------------------------------------
    Name     Type    Description
    ------   ------  -----------------------------------------------
    path     string  Required. The content path.
    message  string  Required. The commit message.
    content  string  Required. The new file content, Base64-encoded.
    branch   string  Branch name. Default: repo's default branch.
    

    Minimal example JSON input:

    {
      "message": "my commit message",
      "content": "bXkgbmV3IGZpbGUgY29udGVudHM="
    }
    

    So you can write a script to base64-encode the file contents, then have it use curl or such to send some JSON like above to https://api.github.com/repos/:owner/:repo/contents/:path

    If it succeeds, you get a JSON response back that includes the GitHub URL(s) for the created file.

    Can also update files without cloning: https://developer.github.com/v3/repos/contents/#update-a-file

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