Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git push directly to URL without needing a remote?

Tags:

git

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.

like image 927
Andreas Gohr Avatar asked Sep 02 '17 13:09

Andreas Gohr


People also ask

How do I push code into URL?

You should follow steps similar to the ones in Migrate to CodeCommit. Run the git remote set-url --add --push origin git-repository-name command where git-repository-name is the URL and name of the Git repository where you want to host your code. This changes the push destination of origin to that Git repository.

Can you git push without pulling?

Always Pull Before a Push This is a bedrock rule. Before you try to push code out to the repository, you should always pull all the current changes from the remote repository to your local machine. Doing so will ensure that your local copy is in sync with the remote repository.

Do you need a remote repository to use git?

You don't need to have a remote repository at all. You can have the full git experience, with commits, branches, merges, rebases, etc, with only a local repository. The purpose of a remote repository (eg, GitHub) is to publish your code to the world (or to some people) and allow them to read or write it.


2 Answers

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

like image 151
LightBender Avatar answered Oct 20 '22 01:10

LightBender


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
like image 15
kgriffs Avatar answered Oct 20 '22 00:10

kgriffs