Search code examples
libgit2sharp

Changing the remote url of a repository in libgit2sharp


How can we change the remote url of a repository?

using (var repository = new Repository(repositoryPath))
{
    //Change the remote url first
    //Then checkout 
}

Solution

  • How can we change the remote url of a repository?

    var newUrl = "https://github.com/owner/my_repository.git";";
    
    Remote remote = repo.Network.Remotes[name];
    
    // This will update the remote configuration, persist it
    // and return a new instance of the updated remote
    
    Remote updatedremote = repo.Network.Remotes.Update(remote, r => r.Url = newUrl);
    

    For what it's worth, most of the remote properties can be updated by following the same pattern. Feel free to take a peek at the RemoteFixture.cs test suite for more detailed examples.