Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

svn import -m "Adding just a file" file_name http://path/to/svn/repo/file_name

How to achieve this simple task in Git?

like image 499
sscarduzio Avatar asked Oct 27 '13 14:10

sscarduzio


1 Answers

For GitHub (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 as above to https://api.github.com/repos/:owner/:repo/contents/:path

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

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

like image 59
sideshowbarker Avatar answered Oct 23 '22 15:10

sideshowbarker