askvity

How do I change my push on Origin?

Published in Git Configuration 2 mins read

To change the push destination (where you send your code) for the origin remote in your Git repository, use the git remote set-url command. Here's how:

Changing the Push URL for Origin

The command you need is:

git remote set-url --push origin <git-repository-name>

Where <git-repository-name> is the new URL of the Git repository you want to push to.

Example:

Let's say your original push URL for origin was https://github.com/old-user/old-repo.git and you want to change it to https://github.com/new-user/new-repo.git. You would use:

git remote set-url --push origin https://github.com/new-user/new-repo.git

Verifying the Change

After running the command, you can verify that the push URL has been updated by running:

git remote -v

This command will show you the fetch and push URLs for all your remotes, including origin. Look for the line that says origin https://github.com/new-user/new-repo.git (push) to confirm the change.

Important Considerations

  • Pushing requires authentication: Ensure that you have the correct credentials (username/password or SSH key) configured for the new repository URL. Git may prompt you for your credentials when you attempt to push.
  • Repository Permissions: You must have push access (write permissions) to the new Git repository to successfully push your changes.
  • Existing Branches: This command only changes the default push URL. If you have configured specific branches to push to different locations, those configurations will remain unchanged.

Adding a Push URL (Instead of Changing)

The original instructions mention --add, which adds a push URL in addition to the existing one. While technically functional, this is generally not what you want when you are trying to change your push origin. It's more common to replace the existing one, as shown above.

If you did use --add by mistake, you can remove the old URL using git remote remove-url before setting the correct one. For example:

git remote remove-url --push origin https://github.com/old-user/old-repo.git
git remote set-url --push origin https://github.com/new-user/new-repo.git

Related Articles