algal
algal

Reputation: 28094

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

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 file, and all I want to do is push an updated version of the file onto the master. Ideally I'd like this to work with any valid remote repo URL, but it would still be helpful if it worked only with https-based git URLs.

I'm guessing this is impossible, since it does not seem to be possible even to retrieve a single file without cloning in the general case, according to the answer How to "git show" on a remote repo? . But I'm hoping there's a workaround that uses some of the lower level git commands.

Upvotes: 41

Views: 34286

Answers (8)

Ilya Gazman
Ilya Gazman

Reputation: 32226

I haven't tested it yet, but here is an idea.

Adding new file

Create a new repo with no files and commit it with a message "day 1".
Next, add all the other commits and the rest of your project.

Now check out that "day 1" and create a branch out of it.
Do a shallow copy clone to this branch with --depth 1, it should be very light.
Now finally, add your new file and create a PR to your main branch via Github APIs.

Done!

Updating file

Same strategy as the new file, only now you will end up with a conflict. You can potentially avoid it with an auto rebase resolution, not sure how to do it on a PR level so...

Upvotes: 0

rogrp6
rogrp6

Reputation: 57

If you are using Github you can use the Github Web Editor. The most easy way to update files without cloning the repo in your local is:

  1. Go to the Github repo in your browser
  2. Then, press .

You should see Visual Studio Code running on your browser. Then, create the folders or drag and drop files from your computer

For save the changes press: Ctrl + Shift + G or click the 'Source Control' option in the left bar, and commit the changes.

Upvotes: 1

tarkh
tarkh

Reputation: 2549

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

Upvotes: 5

Klaymen
Klaymen

Reputation: 83

Just discovered this issue and thought of an update.

If you're using github just prefix your repo url with https://gitpod.io#.

It's a service that allows you to update your repo online, so you can update files without cloning locally.

Upvotes: 6

Fatih Akgun
Fatih Akgun

Reputation: 553

Yes you can push a new version using tagging

follow this steps

in your new project root

  1. git init
  2. git remote add origin [email protected]:yourusername/yourpoject
  3. git tag -a v2.0 -m 'version 2.0'
  4. git add .
  5. git commit -m "New Version 2.0 :rocket:"
  6. git push -u origin v2.0

now you have a new branch called v2.0 with your new project and your master branch stays untouched. After that if you wan't you can change your default branch in your github project settings.

Upvotes: 12

kernelsmith
kernelsmith

Reputation: 87

@algal, for your particular use-case, you might be able to use a submodule (http://git-scm.com/docs/git-submodule). You could clone and commit to the submodule, w/o cloning the "supermodule" which could contain the "massive" code you mentioned. The supermodule could reference the submodule for the config info you mentioned.

Upvotes: 0

kostix
kostix

Reputation: 55443

Impossible. But since a prospective commit would just need to have one single commit as its parent it's possible to employ the so-called "shallow cloning" and fetch just the tip commit of the branch you need. This will bring only a minimum amount of objects from the remote. Look for the --depth command-line option of git clone.

Upvotes: 15

Oleksi
Oleksi

Reputation: 13097

No, I don't think that's possible. You need to clone the whole repository for this to work. Git needs to know about all the files and .git to do its job correctly. This is why you can't just push arbitrary files like that.

Upvotes: 2

Related Questions