I want to have several repositories in my GitHub account connected with various Android Studio projects I am working right now. I have finished my first and I have pushed it to a repository in my GitHub account. But when I am trying to push another Android Studio project to a different repository (in that same GitHub account) the following message appeared: couldn't add remote: remote origin already exists.
.
After looking for more info online, I have manage to learn the basics of how remotes are working and a way to fix the issue. But what I can't fully understand is if I have to change this one default remote (origin as an example) every time I am working to a new project and I want to push changes to my GitHub repositories. According to some solutions, you can change this (one?) remote to different URLs with the git remote add name url
command.
Am I missing something or every time someone wants to push some changes in different project (going to different repositories on the same GitHub account) should do this git remote add <name> url
procedure first. Even if you have to push several changes to different projects in the same day? And why I can't use multiple remotes with different names that to be set only once and linked to specific projects/repositories for the same GitHub account?
CodePudding user response:
I'm not entirely sure what happened to get you to your current state, but I think something's going wrong. I am going to provide an explanation which I hope will be illuminating, but if it's not, please post a copy of the .git/config
file for your project and update your question.
A remote is a specification of where git can find a repository to sync changes with. It's normally a URL, but it can even be a file path on the current computer where you have another copy of the repo.
This means that each project has it's own remote.
If I clone a project called tuplet at https://github.com/codeinred/tuplet.git, that's the remote for that project. If I create a different project, it'll get a different remote.
If you create a project on your local machine, and you want to add a remote later on, first you need to make sure the remote repository exists (go to github and make a new project), then you need to copy the git url for that particular project. This is when you use git remote add
. If you re-use the remote for an existing project, it'll likely refuse to push because that remote already exists.
Side note: Having multiple remotes.
Sometimes a local repository has multiple remotes. This is something you set up intentionally, because you want to track a different version of the project.
For example, when I fork a project, I'll add a remote for the original repository, so that I can download any changes they make. Typically, this remote will be named upstream
.
git remote add upstream
# Download upstream changes
git fetch upstream
# Compare your changes to theirs
# Use the name of whatever branch you want to compare
git diff main upstream/main
# Merge changes into your current branch
git merge upstream/main
CodePudding user response:
The remote origin url is specific for each project and only needs to be set once for each project. so on your development machine you have many projects, inside the root folder of each individual project you initialise a git repository using git init
. This initialises a local git repository inside that root folder for your specific project, not for the whole machine.
When you set up your bitbucket or github or whatever repository for that specific project you need to tell your local project what the url is for that specific project only.
Do this by adding the remote origin making sure you are inside the root of your specific project and using git remote add origin someurl
where someurl is the url of the remote repository.
Once these are set for each individual project you never have to change them. because they are set against the local repository in the root of your local project so when you run git push
or for the first time, git push origin main
or master or whatever the name of your main root git branch is, for the first time, then git will automatically know where to push to every time and because you are running these commands inside your projects root folder git will pick up that remote url in your project specific repository.
So just to clarify, the git commands are repository specific. So long as you hace an initialised local git repository in the root of each project and commands are run only from inside that project specific root folder then you will not need to change anything each time you push