Paulund
2015-03-16 #git

Change URL Of Git Repository

In this tutorial we are going to learn how you can change the URL of your GIT repository to move the code onto a new server or host. There are different approaches you can do for this task, you can either simply change the origin URL and commit everything to this new repository, then you will be starting from scratch on this new server. But you will lose all the previous branches and tags you had on project, if this isn't a problem then changing origin URL is fine, mostly likely you will want all previous branches and this tutorial will help with this.

Changing The Repository Origin

First I'll start off by explaining how you can simply change the origin URL of the repository. At the root of the project where GIT is setup you simply need to run the following command.

git remote set-url origin git://{NEW_GIT_REPO_URL}

To check that this has worked and that you now using to the remote repository you can check what it's set to.


git remote show origin

If this says the new repository then every time you push or pull the repository it will get the code from this new location. ## Get All Branches From The Old Repository

If you want to get all the previous branches and tags you can use the following commands. First before you switch the remote origin to the new server you will need to pull all branches from the old server.


git fetch origin

This will make your local copy aware of all the branches but won't establish a local copy of all the branches code for this you will need to run a checkout on the remote branches to pull the code to your local. Run a check on the local repository of all branches available.


git branch -a

This will list all the branches you have locally, if any are missing then run a checkout on the missing branches.


git checkout -b missing-branch origin/missing-branch

Now we are able to change the remote repository to the new origin and push all branches and tags up to the new server.

git remote set-url origin git://{NEW_GIT_REPO_URL}

To push all branches you will use the command.


git push --all origin

To push all the tags to the server you will use the command.


git push --tags origin

That's it we now have moved to the new repository with all the branches/tags and commits from the old server just from a couple of commands.