How do I rename a git remote?

Git

Git Problem Overview


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)

Git Solutions


Solution 1 - Git

$ 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/

Solution 2 - Git

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..remote = X

> to

> branch..remote = Y

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

> branch..pushRemote = X

> to

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

Solution 3 - Git

As you linked and said yourself in your answer, you just have to type

git remote rename heroku production

see bottom of the page: https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrouanView Question on Stackoverflow
Solution 1 - GitrouanView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitmdcView Answer on Stackoverflow