How to Push From an Existing Remote Repository to a Different Remote Repository in Git
This tutorial will teach you how to push from the existing remote repository to a different remote repository in Git.
Git is a version control system utilized to track changes in a project directory. Git uses commits for such purposes.
In Git, a local repository and its branches are set up to track the remote repository and its branches.
Sometimes, we may wish to push changes done in the local repository to a different remote repository instead of the existing one. We can use the git remote
command for such purposes.
We will now illustrate this with an example.
Push From an Existing Remote Repository to a Different Remote Repository in Git
Git is used in a collaborative development environment to keep track of the modifications done to the files in the project directory. In Git, often, we have a local repository and its branches set up to track a remote Git repository and its branches.
We use the git pull
command to pull the changes from the Git remote repository into the local repository and the git push
command to push the changes to the remote.
Sometimes, we may wish to push the changes to a different remote repository instead of the existing one, i.e., one that we use to pull in the remote changes.
Suppose we have a repository named My_Project
hosted on the server https://git.fedorahosted.org/
. We have to clone the Git repository My_Project
in our local machine.
Then we can execute the git pull
and git push
commands, respectively, to pull in and push the changes from and to the existing remote repository hosted on the server https://git.fedorahosted.org/
.
We now wish to push the changes to a different remote Git repository hosted on a different server, https://github.com/
.
First, we need to create a new remote Git repository on the server Github named My_Project
.
We have already cloned the remote repository My_Project
hosted on the server https://git.fedorahosted.org/
on our local machine. We will now rename the remote given by the alias origin
to upstream
as follows.
$ git remote rename origin upstream
After this, we need to add the new and different remote repository URL hosted on Github using the command git remote
as follows.
$ git remote add origin https://github.com/johndoe/My_Project.git
Thus, now we have set up a different remote Git repository URL. Now, we can use the git push
command to push the local changes to the remote repository hosted on Github, as follows.
$ git push origin master
We can still pull in the changes from the original and existing remote repository hosted on the server https://git.fedorahosted.org/
using the git pull
command.
$ git pull upstream master
Thus, we have learned how to push from the existing remote repository to a different remote repository in Git.
For more information, please visit the following sources: