37

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.

Community
  • 1
  • 1
algal
  • 27,584
  • 13
  • 78
  • 80
  • 1
    Don't think it's possible, nor it should really, why would you edit files without having the entire repo in the first place? – Madara's Ghost Apr 18 '13 at 08:12
  • 1
    If the remote repo is hosted at [github](http://github.com), you can edit the file directly there. Otherwise I don't think it's possible. – Michael Wild Apr 18 '13 at 08:17
  • 4
    @MadaraUchiha Why? Because sometimes you only need to make a small change to a massive repo, and it costs a lot of time to clone the whole thing. In my case, a management app needs to update metadata contained in hundreds of git repos, but doesn't want to maintain local copies of all of them only for that purpose. – algal Apr 18 '13 at 15:59

7 Answers7

15

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.

kostix
  • 51,517
  • 14
  • 93
  • 176
  • Interesting feature! The man pages says though: _you cannot clone or fetch from it, **nor push from nor into it**_. So even though you could use this to create a patch and send the patch individually, it won't help you with pushing changes automatically. – Shahbaz Apr 18 '13 at 09:25
  • @Shahbaz The manual is actually wrong there, you can push from it just fine… – poke Apr 18 '13 at 09:35
  • @poke, I haven't tried it, but if you are sure, consider filing a bug report on the manual page. – Shahbaz Apr 18 '13 at 09:41
  • 3
    @Shahbaz, yes, the manual is wrong. The Git protocol, when doing push, goes something like this: "hey, the remote side, please update that ref `refs/heads/master` with this pile of objects; the SHA-1 of that ref before my update was AAA, the SHA-1 of the new tip will be BBB", so the shallow clone has all the information needed to pass a new commit to the remote. – kostix Apr 18 '13 at 09:43
  • @kostix, yes I understood the logic. But if there is nothing behind the scenes preventing this, then better file a bug report on the man page. – Shahbaz Apr 18 '13 at 09:44
  • 3
    @Shahbaz, [done](http://article.gmane.org/gmane.comp.version-control.git/221634). – kostix Apr 18 '13 at 09:58
12

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 git@github.com: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.

Fatih Akgun
  • 479
  • 1
  • 6
  • 18
  • 2
    +1 Although this does not answer the exact question (it was requested to update a file in master), it still finds usefulness since this way you avoid to have the sources from where you push. – reallynice Sep 04 '18 at 09:38
5

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.

Klaymen
  • 73
  • 1
  • 3
  • 9
  • Thaks man, but is there something like that for Azure from microsoft ? – Mina Shaker Mar 03 '19 at 11:06
  • Funny enough, since then I've started working in Azure :) The user suggested way here works for Azure Devops repos: https://developercommunity.visualstudio.com/content/problem/800140/no-edit-button-when-viewing-file-in-azure-devops.html – Klaymen Oct 07 '20 at 15:15
5

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

tarkh
  • 2,424
  • 1
  • 9
  • 12
2

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.

Oleksi
  • 12,947
  • 4
  • 56
  • 80
0

@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.

0

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.

Aspasia
  • 1,521
  • 1
  • 13
  • 23
rogrp6
  • 47
  • 1
  • 5