Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename a git remote?

Tags:

git

I currently have a git remote called heroku and I'd like to rename it to production.

$ git remote -v
heroku  https://git.heroku.com/example.git (fetch)
heroku  https://git.heroku.com/example.git (push)
like image 549
rouan Avatar asked Nov 21 '15 06:11

rouan


3 Answers

$ git remote rename <old-name> <new-name>

So, for this example:

$ git remote rename heroku production

Useful docs here: https://help.github.com/articles/renaming-a-remote/

like image 127
rouan Avatar answered Oct 18 '22 23:10

rouan


Be aware that until Git 2.11, git remote rename might try to rename a non-existing remote!

That is fixed in Git 2.12 (Q1 2017): See commit e459b07, commit af5bacf (19 Jan 2017) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit fe575f0, 31 Jan 2017)

remote rename: more carefully determine whether a remote is configured

With anticipatory tweaking for remotes defined in ~/.gitconfig (e.g. "remote.origin.prune" set to true, even though there may or may not actually be "origin" remote defined in a particular Git repository), "git remote rename" and other commands misinterpreted and behaved as if such a non-existing remote actually existed.


With Git 2.26 (Q1 2020), "git remote rename X Y" needs to adjust configuration variables (e.g. branch.<name>.remote) whose value used to be X to Y.
branch.<name>.pushRemote is now also updated.

See commit b3fd6cb (01 Feb 2020), and commit f2a2327, commit 923d4a5, commit ceff1a1, commit 1a83068, commit 88f8576 (27 Jan 2020) by Bert Wesarg (bertwesarg).
(Merged by Junio C Hamano -- gitster -- in commit d0038f4, 25 Feb 2020)

remote rename/remove: handle branch..pushRemote config values

Signed-off-by: Bert Wesarg

When renaming or removing a remote with

git remote rename X Y
git remote remove X

Git already renames/removes any config values from

branch.<name>.remote = X

to

branch.<name>.remote = Y

As branch..pushRemote also names a remote, it now also renames or removes these config values from

branch.<name>.pushRemote = X

to

branch.<name>.pushRemote = Y

And:

remote rename/remove: gently handle remote.pushDefault config

Signed-off-by: Bert Wesarg

When renaming a remote with

git remote rename X Y
git remote remove X

Git already renames or removes any branch.<name>.remote and branch.<name>.pushRemote configurations if their value is X.

However remote.pushDefault needs a more gentle approach, as this may be set in a non-repo configuration file.
In such a case only a warning is printed, such as:

warning: The global configuration remote.pushDefault in:
 $HOME/.gitconfig:35
now names the non-existent remote origin

It is changed to remote.pushDefault = Y or removed when set in a repo configuration though.

like image 31
VonC Avatar answered Oct 18 '22 23:10

VonC


A common use case is to switch origin to your fork and rename the current origin to upstream.

A simple remote rename will leave pushDefault and branch config values pointing towards the renamed remote and not at origin as is generally intended. If you are doing this, then this flow will work best...

First get the current origin:

git remote -v

Record the value (OLD_URL) and now change it with set-url followed by adding the old origin as a new upstream:

git remote set-url origin NEW_URL
git remote add upstream OLD_URL
like image 2
nikc Avatar answered Oct 19 '22 00:10

nikc