Search code examples
gitgit-pushgit-remotegit-config

Git create remote repository on push


I have been trying to figure this one out but I am having a hard time doing so. I am currently working on an open source project that requires me to allow a user to push to remote repository without it already existing there. I want to avoid manually logging in to a server and running git init or git init --bare.

For obvious reasons, I get the following error when trying to push my local repository to a path that doesn't point to an existing repository on the remote server:

fatal: '/var/repositories/myrepo' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

But I would like to be able to run for example the following command:

git push origin master

And have that create /myrepo in /var/repositories if it does not yet exist. How would I be able to accomplish this? I would assume it is some kind of (global) git config setting you would probably set on the remote server, or otherwise a (repository specific) git config locally, but I couldn't figure it out.

Any help would be much appreciated!

Thanks!


Solution

    1. Checkout and track the branch from the remote:

      git checkout -t origin/funbranch
      
    2. Branch off of it:

      git checkout -b mybranch
      
    3. Push your new one up, it will create a new branch automatically on the remote:

      git push origin mybranch
      

    It should say "created new remote branch origin/mybranch"

    If you are still getting "Remote end Hung up", it sounds like a security thing. Do you have SSH keys installed correctly, and do you have write permissions on the remote server?

    The way most open source projects work, you have to create a fork (a clone inherently) that you use to do your work because most of the time you don't have write permissions to the repo. When you have changes, you will then send a pull request to the repository owner, and he/she will pull from your fork the changes that they want.