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

后端 未结 3 1108
难免孤独
难免孤独 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: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

提交回复
热议问题