CodeWalrus CodeWalrus - 1 month ago 9
Git Question

How can I change the remote (hosted) repo to which my workstation (local) repo pushes information?

I forked a project from GitHub.

From my workstation, I wish to be able to push to my Gitlab, i.e.,

git push


Here is what I get on my workstation thus far:

$ git remote -v
origin https://github.com/OWASP/railsgoat.git (fetch)
origin https://github.com/OWASP/railsgoat.git (push)


I think it will be a success when it instead states, for my purposes:

$ git remote -v
origin https://github.com/OWASP/railsgoat.git (fetch)
origin https://gitlab.com/myName/railsgoat.git (push)


I did try looking at the documentation, however the solution as of yet evades me:

git remote set-url [--push] <name> <newurl> [<oldurl>]
git remote set-url --add [--push] <name> <newurl>
git remote set-url --delete [--push] <name> <url>

Answer

In fact, it's rather awkward to have separate fetch and push URLs for same remote (you can do it, but it's not the typical way to achieve what you're asking for). Typically you'd add another remote, and then push to that remote.

git remote set-url origin https://gitlab.com/myName/railsgoat.git
git remote add upstream https://github.com/OWASP/railsgoat.git

then you will use

git fetch upstream

to get changes from upstream repo and

git push origin

to push to your fork.

The names upstream and origin are not set in stone, though these are typical names used by the community. You can name upstream something else, it's just an identifier. origin is the name of remote given to the one from which you originally cloned.

The convention is to use origin for something you control (your fork), and upstream for something you don't control (central repo).

Then, the next concept is the "upstream branch" for a given branch (the naming is confusing).

Basically, git push needs to know to which branch you want to push from and to (git is overly flexible in this case which can be daunting at the beginning).

Say you want to push from master to origin/master, and you always want git push in the future (when in master) to do the same, you'd do:

git push -u origin master

(-u is a shorthand for --set-upstream, which you can later edit via --set-upstream-to, which makes git remember that the branch you push to right now is the default branch to be used in the future).

Once done, you can in the future use just

git push

One more note, if for some reason you're using git 1.x still, it's recommended to execute this

git config --global push.default simple

to change the behavior of what git push does (read more here)


To do the exact thing you were asking for, you'd do

git remote set-url --push origin https://gitlab.com/myName/railsgoat.git