You can change your repository URL without losing your work
When you set up a project with Git, it stores the address of where your code lives — usually on GitHub, GitLab, or another hosting service. That address is called a remote. If you need to move your project to a different repository, change hosting services, or fix a wrong URL, you do not have to start over. Git lets you update the remote address in a few commands, and your local files stay exactly as they are.
The most common reason to change a repository is moving from one service to another — say, from GitHub to GitLab — or switching from a personal account to an organization account. You might also need to fix a typo in the URL or update from HTTP to SSH (a more find connection method). All of these changes happen in the same way.
Key Takeaways
- Your local Git folder stores the remote URL in a hidden file called .git/config, which you can view and edit without touching your actual code.
- The command git remote -v shows you exactly what remote address Git is currently using for pushing and pulling.
- You change the remote URL with git remote set-url origin [new-url], where origin is the default remote name and [new-url] is the new repository address.
- After changing the remote, test the connection with git remote -v again to confirm the change took effect before you push code.
- If you are switching to SSH, you will need to generate an SSH key and add it to your new hosting service before your first push will work.
Check what remote you are currently using
Before you change anything, see what Git currently has stored. Open your terminal or command prompt, navigate to your project folder, and type:
git remote -v
This shows you the remote name (usually origin) and the full URL. You will see two lines — one for fetch (pulling code down) and one for push (sending code up). Most of the time both point to the same place. If you see nothing, your project has no remote set yet, and you can add one instead of changing one.
Update the remote URL with set-url
Once you know the current URL, use the set-url command to change it. The syntax is:
git remote set-url origin https://github.com/your-username/new-repo-name.git
Replace https://github.com/your-username/new-repo-name.git with the actual URL of your new repository. You can copy this URL directly from your new repository's page on GitHub, GitLab, or whichever service you are using — there is usually a green "Code" button or similar that gives you the URL to paste.
If your remote has a different name than origin (which is rare but happens), use that name instead. For example, git remote set-url upstream https://....
Verify the change worked
After you run set-url, check that it took effect:
git remote -v
You should see the new URL next to both fetch and push. If you see the old URL still there, the command did not run or you made a typo. Try the set-url command again, paying close attention to the URL format.
Switch from HTTPS to SSH (or vice versa)
The URL format matters. HTTPS URLs look like https://github.com/username/repo.git and SSH URLs look like git@github.com:username/repo.git. If you are switching between them, use the same set-url command — just paste the new format.
If you are moving to SSH for the first time, you will need to generate an SSH key on your computer and add it to your hosting service's settings. GitHub, GitLab, and others have guides for this in their documentation. Without the key set up, your push will fail with a permission error. HTTPS does not require this extra step — you just need your username and password (or a personal access token, depending on the service).
What happens to your local files and history
Changing the remote URL does not touch your actual code or your Git history. All your commits, branches, and local work stay exactly as they are. You are only telling Git where to send that work when you push. This means you can safely change the remote, test it, and change it back if needed.
Your first push to the new remote might take longer than usual because Git is uploading everything for the first time. After that, pushes will only send the new commits you have made since the last push.
Troubleshooting a failed push after changing the remote
If you change the remote and then try to push but get an error, the most common causes are: the new repository does not exist yet (create it first on your hosting service), you do not have permission to push to it (check your account access), or your authentication is not set up (especially with SSH). If you get a message about the remote URL being wrong, double-check it character by character — URLs are case-sensitive and even a small typo will fail.
If you are switching services entirely and the new repository is empty, your first push should work fine. If the new repository already has commits in it that your local copy does not have, Git will refuse to push until you pull those commits down first. Use git pull origin main (or master, depending on the branch name) to sync before pushing.
Frequently Asked Questions
Do I lose my commit history if I change the remote?
No. Your entire commit history lives in your local .git folder. Changing the remote only changes where Git sends that history when you push. All your commits stay on your computer.
What if I want to push to two different repositories at the same time?
You can add a second remote with a different name: git remote add backup https://.... Then push to both with git push origin main and git push backup main. Most people only need one remote, but this is useful for backup or mirror setups.
Can I undo a remote change if I make a mistake?
Yes. Just run set-url again with the old URL, or use git remote remove origin followed by git remote add origin [url] to start fresh. Nothing is permanent until you actually push code.
Why does my push fail with a permission error after changing the remote?
The new repository may not exist yet, or you may not have access to it. If you are switching to SSH, you need an SSH key set up on your hosting service first. If you are using HTTPS, make sure your username and password (or personal access token) are correct for the new service.
Do I need to change the remote if I just rename my repository on GitHub?
No. GitHub automatically redirects the old URL to the new one for a while, so your pushes will still work. But it is good practice to update it anyway so you are not relying on a redirect: use set-url with the new repository name.