• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SomeDevTips

Tips and code examples for software developers

  • WordPress
  • PHP

Git: how to change the remote repository of a project

5 March 2020. Categories: Version control

You have a git project with a remote repository (for example GitHub, Bitbucket or a git server in your organization). You need to change the git remote repository of your project because the server is no longer the same.

A remote repository is a server where you store your project so that several developers can work on it. Git identifies every repository by a url and a name. The name is an alias that is used to identify the repository regardless of its url. It is both a shortcut and a way to ensure that the remote reference is consistent throughout the project life.

Two possible cases are of remote repository change are:

  • your project has been moved to a different server in your organization;
  • your entire git server has been transferred to another machine with a different name.

So, now you have to update your Git project to change its remote repository.

How you can do it ?

  1. At your command prompt execute:
    git remote -v
    This command will list the current remote repositories, each one of them with its name and url. For example the output could be:
     
    origin  http://myserver/myproject.git (fetch)
    origin  http://myserver/myproject.git (push) 
     
    origin is the name (alias) of the remote repository.
  2. Execute:
    git remote set-url <name> <newurl> 
    to change the remote. For example, if the remote name is origin and the new url is http://newserver/myproject.git, then the command will be:
    git remote set-url origin http://newserver/myproject.git 
  3. Execute again:
    git remote -v
    to check that the new url has been set both for push and fetch operations.

Since you did not change the name (alias) of the remote repository, the commands to push/fetch to/from the server will be the same as before.

For example to push your last commit of the master branch to your remote server, you will still do:

git push origin master

However git will send this commit to newserver instead of myserver.

You can find the complete syntax of the git remote command at this link: https://git-scm.com/docs/git-remote

Primary Sidebar

Recent articles

  • WordPress: let an author manage categories and tags
  • A local development environment for web applications
  • Stacks and queues: features
  • A simple backup script for WordPress
  • Git: how to change the remote repository of a project

Categories

  • Algorithms and data structures
  • PHP
  • Version control
  • Web
  • WordPress

© Copyright 2019-2023 · Somedevtips