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