Search code examples
gitgit-remote

Replace a git remote without affecting branches


I am mid-development into a project and due to DevOps changes there is suddenly the need to re-connect to the remote (with a new URL). What are the steps that I need to take to remove the old remote and establish connection with the new one without ruining my unmerged branches?

I have tried updating the remote URL using:

git remote set-url origin codecommit::us-west-2://myrepo

but getting this error when fetching from master:

fatal: repository 'https://git-codecommit.us-west-2.amazonaws.com/v1/repos/myrepo/' not found

Am I missing something?

As background, the CodeCommit repo that I used to work with has been changed to require federated access. So I want to use git-remote-codecommit (GRC) with the corresponding URL instead of the old standard HTTPS URL.


Solution

  • First, before changing origin, you can check if an URL work with:

    git ls-remote <newURL>
    

    You can execute that command anywhere, and as long as it does not fully work... you don't need to worry about git remote set-url origin.

    Second, I know about this codecommit AWS endpoints (HTTPS or SSH), but not about "federated".
    Double-check the process described in "Using Federated Identities with AWS CodeCommit", especially the /home/ec2-user/.aws directory content.

    The OP Mossi adds in the comments:

    Turned out the IAM role that the AWS Credential Helper needed had disappeared from my environment variables.

    I added it back and I was able to connect to the repo using the GRC URL. In short set-url was the solution.