Add new remote to existing git repo

2024-01-23

Here's a step-by-step guide to add a new remote to your existing Git repository and set it as default upstream:

  1. Navigate to Your Repository: Use cd to navigate to the local Git repository where you want to add the new remote.
  2. Add New Remote: Execute git remote add <remote-name> <remote-url>.
    • <remote-name> is the name you assign to the new remote (e.g., origin, upstream). Though chances are origin is already taken - I named mine github
    • <remote-url> is the URL of the new remote repository (e.g., https://github.com/user/repo.git). You have to create a remote repo beforehand1
  3. Verify Addition: Run git remote -v to verify that the new remote has been added and to view all remotes.
  4. Push local branch to new remote: Run git push -u <remote-name> <branch-name>. If you need to look up which branches you have: run git branch, most of the time main brunch is called main or master. -u option would set the new remote as upstream of the local branch. This sets that remote as default target for git fetch, git pull and git push commands.

Now you can push to, pull from, and fetch from the repositories using the remote names to specify which repository you are interacting with.

1

Notable exception is SourceHut, where you can just push your code to a repository that doesn't yet exist and it will create it :D, check it out here