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:
- Navigate to Your Repository: Use
cd
to navigate to the local Git repository where you want to add the new remote. - 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 areorigin
is already taken - I named minegithub
<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
- Verify Addition: Run
git remote -v
to verify that the new remote has been added and to view all remotes. - Push local branch to new remote: Run
git push -u <remote-name> <branch-name>
. If you need to look up which branches you have: rungit branch
, most of the time main brunch is calledmain
ormaster
.-u
option would set the new remote as upstream of the local branch. This sets that remote as default target forgit fetch
,git pull
andgit 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