28

I know that I can pull directly from a remote repository into my current branch using an URL without having to add the repository as a remote like this:

git pull git://github.com/them/repository.git theirbranch

This is quite convenient to quickly checkout pull requests.

Now what I wonder is if it is possible to do the same for pushing (assuming I have the needed repository access). I tried the following but it does not work:

git push git://github.com/them/repository.git theirbranch

I know that I could add the URL via git remote add and then push to that named remote, but it seems overkill to create a config for a one-off push. I feel like it should possible but I simply can't figure out the right syntax and all examples always use named remotes.

3 Answers 3

23

It is certainly possible to push to a remote branch, but you may need to specify your local branch name, as well as use the HTTP URI, in order to do so.

Suppose you maintain a project and would like to make a few tweaks to a pull request before merging it. You start by pulling the feature branch:

$ git checkout -b username-featurename master
$ git pull git://github.com/username/projectname.git featurename

After making your changes, you can push directly to the original feature branch, assuming the owner has granted you the appropriate permissions. You will probably only be allowed to do this with the HTTP URI, and will also have to specify your local branch name in the refspec:

$ git push https://github.com/username/projectname.git username-featurename:featurename
1
14

According to the git documentation for push, the repository can be specified as either a valid git url or a reference to a saved remote.

This will work with remote urls:

ssh://[user@]host.xz[:port]/path/to/repo.git/
git://host.xz[:port]/path/to/repo.git/
http[s]://host.xz[:port]/path/to/repo.git/
ftp[s]://host.xz[:port]/path/to/repo.git/

Or local repos:

/path/to/repo.git/
file:///path/to/repo.git/

And supports all the usual accoutrements (username, port).

0

You can:

git push --set-upstream (the URL you're pushing to here) (the branch you are pushing to here)

that should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.