Changing a Git Repo's Remote URL

Published on Jan 3, 2015
A useful practice to get used to in the Terminal is changing the remote path of your repos should you decide to change Git hosting services. Most developers keep there repos in two places: Github or Bitbucket, however, for the select few they decide to host their own Git repos. Generally I don't like to do this for plethora of reasons, but I digress. For those three options it may become necessary to change or move your repo to a new location and this is the step by step process to get your repo moved while keeping all your Git commits and history. ## Set Remote URL The git remote set-url command changes an existing remote repository URL. This command takes two arguments: 1. An existing remote name, for example, origin 2. A new URL for the remote, for example: - // if you're updating to use HTTPS - [email protected]:user/repo2.git // if you're updating to use SSH You can see what your current repository push and fetch URLs are right now by typing the following command into the terminal. $ git remote -v This will out put something along these lines: origin (fetch) origin (push) So every time you do a push, pull, or fetch from terminal the commands are being run against these URLs to verify if there are any changes that you have locally or in the remote repos that are different. ## Set Up a New Bare Repo For this exercise we have setup a bare repository (using our imaginations) and want to move our repository on our local machine to use this new location. The only thing you really need is the URL to the new repository location. In the example below this will be: So we will change the remote url and then verify that our changes have been made. $ git remote set-url origin # Change the 'origin' remote's URL $ git remote -v # Verify new remote URL # origin (fetch) # origin (push) Now we should see the new URLs that we will be pushing our repository to. In order to successfully sync up our new repository it is recommended that we do an initial git push in order to push all the commit history and files to our new location. Happy committing.
Justin Hough author picture
Justin Hough

Chief Development Officer at Hounder. He is a Christian, husband, father, writer, developer, designer, and a digital carpenter crafting amazing web experience. Also, created the Centurion Framework many moons ago.