How to update a file in remote repo, without cloning that repo first?

前端 未结 6 1870
天命终不由人
天命终不由人 2020-12-14 07:39

Is there a way to push a commit to a remote git repo, without first making a local clone of that repo?

I have a valid URL for the remote repo, I know the path of the

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 07:56

    Well, if you use GitHub, then you could do this easily with GitHub API
    https://developer.github.com/v3/repos/contents/#create-or-update-a-file

    Basically you do:

    1. Create OAuth App and get Access Token
      https://developer.github.com/apps/building-oauth-apps/

    2. Make request to GitHub API:
      Set Authorization header with your Access Token (OAuth 2.0)
      PUT https://api.github.com/repos/:owner/:repo/contents/:path
      with JSON body like this:

    {
      "message": "my test commit through API",
      "content": "QmFzZTY0IGVuY29kZWQgY29udGVudCBoZXJl",
      "sha": "cd220ncv229f2e4a95bce426ff48b1ae6b885b3a42"
    }
    

    Where:
    message - your commit message
    content - updated content encoded in Base64 format
    sha - sha for updating file, you can get it by API request
    GET https://api.github.com/repos/:owner/:repo/contents/:path

    :owner - repository owner
    :repo - repository name
    :path - path to file within repository

    Example request:
    https://api.github.com/repos/nodejs/node/contents/README.md

提交回复
热议问题