Understanding and working with git remotes
Do you have same git repository over different platforms? Are you searching for the convenient way to manage them all at once within a single local repository?
Introduction to git remote?
A remote in Git is a version of your repository that is hosted on the Internet or network somewhere. Remotes make it easy to collaborate with others, as you can fetch and push changes. You can also use them to create backups of your repository.
It's common to have a single remote, called
origin, but you can have multiple remotes. This can be useful in a few different scenarios. For example, you might want to contribute to someone else's repository and have a remote for their repository. Or, you might want to set up a
mirror of your repository on a different hosting service.
How does a git remote work?
There are two link urls for a remote git repository.
- fetch url: to perform operations like fetch and pull from remote server to local computer.
- push url: to perform push operations from local computer to remote server.
To check the all the remotes within a repository, you can use the git remote command.
$ git remote -v
origin email@example.com:whoisYoges/website.git (fetch)
origin firstname.lastname@example.org:whoisYoges/website.git (push)
Here, for a remote named
origin, we have the same urls for both fetch and push operations cause we're fetching/pulling from and pushing to the same remote repository. If you fetch/pull from one repository and push to other the urls can be different even in same remote.
Adding a new remote
To add a new remote to your repository, use the git remote add command. The basic syntax is:
git remote add <name> <url>
Replace <name> with a short, memorable name for the remote, and <url> with the URL of the remote repository. For example, if you want to add a remote for a repository of this website hosted on GitHub, you might use a command like this:
git remote add github email@example.com:whoisYoges/website.git
This will add a remote called
github that points to the specified repository. You can then use this remote like any other. For example, to fetch changes from the remote, you can use the git fetch command:
git fetch github
Again, if you want to add another remote for the same repository hosted on sourcehut, you might use a command like this:
git remote add sourcehut firstname.lastname@example.org:~whoisyoges/website
This will add a remote called
sourcehut that points to the specified repository and then you can use this remote like any other. For example, to push changes to the remote, you can use the git push command.
For example, to push your local
master branch to the
sourcehut remote, you can use a command like this:
git push sourcehut master
This can be useful for collaborating with others, as you can push your work-in-progress to a branch on the remote and ask for feedback before merging it into the main branch.
You can view a list of your remotes at any time by running the git remote command with no arguments. This will list the names of all your remotes.
$ git remote
To see more information about a specific remote, use the git remote show command, followed by the name of the remote. This will display information about the
sourcehut remote, including the URL and the branches that are being tracked. For example:
$ git remote show sourcehut
* remote sourcehut
Fetch URL: email@example.com:~whoisyoges/website
Push URL: firstname.lastname@example.org:~whoisyoges/website
HEAD branch: master
Local ref configured for 'git push':
master pushes to master (fast-forwardable)
Multiple push urls in a single remote
The above method allows us to add multiple remotes and push the local repository to the them one by one, but we cannot push our local repository to all of the remotes at once.
So, Follow the procedure below for adding multiple push urls in a single remote to push a local repository to all of the remotes at once using a single command.
Define a git remote which will point to multiple git remotes, we call it
git remote add all REMOTE-URL-1.
This REMOTE-URL-1 will be used to perform fetch/pull operations.
git remote add all email@example.com:~whoisyoges/website
Now, add all the push urls one by one in the remote
Here, I'm adding the remote repositories of this website available in sourcehut, github, and codeberg.
git remote set-url --add --push all firstname.lastname@example.org:~whoisyoges/website
git remote set-url --add --push all email@example.com:whoisYoges/website.git
git remote set-url --add --push all firstname.lastname@example.org:whoisYoges/website.git
Confirm the addition of remote urls by using git remote command.
$ git remote -v
all email@example.com:~whoisyoges/website (fetch)
all firstname.lastname@example.org:~whoisyoges/website (push)
all email@example.com:whoisYoges/website.git (push)
all firstname.lastname@example.org:whoisYoges/website.git (push)
Push a branch to all the remotes with
git push all BRANCH – replace BRANCH with a real branch name.
You cannot fetch updates from multiple remotes in this method.
Note: The branch name of all the remote repositories must be same.
In summary, adding and using multiple remotes in a Git repository can be a useful way to collaborate with others and make backups of your work. By using the git remote and git push commands, you can easily work with multiple remotes and push and fetch changes as needed.