September 12, 2022

How to Change git repo URL without impacting the uncommitted /unsynched code

  September 12, 2022

 What happens if the repo URL that we are using is suddenly changed and a new URL is provided.

If there are no uncommitted codes, we can simply clone the repo from the new location and continue our work.

But what if there is a lot of non-synced code in our local and we need to push those to the repo in the new URL?

** This is applicable if the existing repo is deleted and an exact copy of that repo is created in another location. We can use this solution in the case of Project Renaming also in azure.

We can fix this issue by changing the remote URL of the repo.

We can use the below commands through the git terminal

//To change the URL of a Git remote, you have to use the “git remote set-url” command and specify the name of the remote as well as the new remote URL to be changed
$ git remote set-url <remote_name> <remote_url>

//To verify the changes we made, you can use the “git remote” command with the “-v” option
$ git remote -v

$ git remote set-url origin https://myrepo/new-path.git
$ git remote -v

If you are using Visual Studio, This can also be done through UI.

To Add/Remove remote

Go to Git > Manage remotes

If you want to use the same remote name, first add a new remote with another name. Then remove the existing remote and rename the newly added remote to the needed name.

If the needed remote name is 'origin', steps will be
  • Add a new repo with name origin1
  • Remove the older repo named origin
  • Edit and rename the new repo to origin
After this we can do a Fetch/Sync once, then we will be connected to the new repo URL and we can commit and push as usual.

Thanks for reading How to Change git repo URL without impacting the uncommitted /unsynched code

« Prev Post

No comments:

Post a Comment

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...