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:
- An existing remote name, for example, origin
- A new URL for the remote, for example:
- https://github.com/user/repo2.git // if you're updating to use HTTPS
- firstname.lastname@example.org: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 https://github.com/old_user/old_repo_url.git (fetch) origin https://github.com/old_user/old_repo_url.git (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: https://github.com/user/repo.git
So we will change the remote url and then verify that our changes have been made.
$ git remote set-url origin https://github.com/user/repo2.git # Change the 'origin' remote's URL $ git remote -v # Verify new remote URL # origin https://github.com/user/repo2.git (fetch) # origin https://github.com/user/repo2.git (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.