Git Set Remote
Git Remotes Mixed Up
If you’re using git for any length of time, professionally, or for hobbies,
you’ll probably run into the problem of using other publicly available
repositories. If you git clone
from such a repository, make your changes
commit, then push you’ll probably run into a huge problem; you’re now asking to
modify a repository you do not own! How do you change where you’re pushing to?
The answer is “remotes” which is pretty cryptic to be honest…
Set a New Remote
If you’ve cloned a template repository and want to push to a new GitHub repository after cloning from another repo remove the original remote then add the new remote then push the main (or master) branch to the (hopefully) empty repository:
git remote remove origin
git remote add origin url/your-project.git
git push origin main -u
About Git Remotes
A remote is a reference to another git repository. This can be a network
location, like a GitHub repository or another local repository. If you have
several remotes and several branches it can get complicated fast. Each branch
can track its own remote and when you push
to those remotes, they’re updated
(if you’re allowed to push). After a push
both the remote and local branch
will look identical (and importantly they’ll share hashes for each commit).
If you’re cloning GitHub repositories with the normal git-scm tool, usually your local repository will set the GitHub repository as it’s upstream remote. Every time you try to push it’ll ask to do so on the upstream GitHub repo. This is fine if you own the repo, or if you’re a main contributor, but on larger projects you are often restricted from making direct pushes to some or all branches of the GitHub repository. That case is out of scope for this note.
If you’re using a repo as a template, or using the source code for your own project but adding to it in a different way, then you need to set up your remotes like this.
If this isn’t enough info, check here, and here for even more details.
Notes
This post is super short, and that’s because it’s a “Note”. I might come back to reference this when I forget how to do this.