askvity

How do I change Origin on GitHub?

Published in Git Configuration 3 mins read

To change the remote origin URL in your Git repository, use the git remote set-url command. This allows you to update the URL where your local repository pushes and fetches changes from a remote repository like GitHub.

Steps to Change the Origin URL

  1. Open your terminal or command prompt: Navigate to the root directory of your local Git repository.

  2. Use the git remote set-url command: The basic syntax is:

    git remote set-url origin <new_url>
    • origin is the standard name for the remote repository. If you've named it something else, replace "origin" with that name.
    • <new_url> is the new URL of your GitHub repository. You can use either an HTTPS or an SSH URL.
  3. Determine the correct URL: On GitHub, navigate to your repository. Click the "Code" button. You'll see options for HTTPS and SSH. Choose the one you prefer.

    • HTTPS URL: Looks like https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.git
    • SSH URL: Looks like [email protected]:YOUR_USERNAME/YOUR_REPOSITORY.git
  4. Example using HTTPS:

    git remote set-url origin https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.git

    Replace YOUR_USERNAME and YOUR_REPOSITORY with your actual GitHub username and repository name.

  5. Example using SSH:

    git remote set-url origin [email protected]:YOUR_USERNAME/YOUR_REPOSITORY.git

    Replace YOUR_USERNAME and YOUR_REPOSITORY with your actual GitHub username and repository name. You'll also need to have SSH keys set up with GitHub for this to work without prompting for a password each time.

  6. Verify the change: After running the command, you can verify that the origin URL has been updated by using the following command:

    git remote -v

    This will list the remote repositories, including their fetch and push URLs. You should see the new URL you just set.

Choosing Between HTTPS and SSH

  • HTTPS: Easier to set up initially. You'll likely be prompted for your username and password (or a personal access token) when pushing or pulling.
  • SSH: More secure and convenient once set up. Requires generating an SSH key and adding it to your GitHub account. After the initial setup, you typically won't be prompted for credentials.

When to Change the Origin

You might need to change the origin in these situations:

  • You moved your repository to a different GitHub account.
  • The repository name has changed.
  • You initially set up the origin with the wrong URL.
  • You want to switch between HTTPS and SSH.

Changing the origin URL ensures that your local repository is connected to the correct remote repository on GitHub.

Related Articles