0

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>
CodeWalrus
  • 5,628
  • 1
  • 16
  • 18

2 Answers2

3

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
Community
  • 1
  • 1
jakub.g
  • 38,512
  • 12
  • 92
  • 130
  • I don't think they really want to pull from upstream and push to the fork, I'd suggest to edit your answer to have the last part first, because that's the right answer for most people. – morxa Oct 21 '16 at 09:23
1

you should add a new remote host with

git remote add newHostName  https://gitlab.com/myName/railsgoat.git 

and then

git push newHostName branchname
felix
  • 9,007
  • 7
  • 41
  • 62