What is a Git remote?
A Git remote refers to a version of a repository that is hosted on the internet, or on a network somewhere outside your local machine. Remotes make it possible to collaborate with others by allowing multiple developers to push to and pull from the repository. Typically, a remote is a repository on a service like GitHub, GitLab, or Bitbucket, or on a private server.
This guide will explain what Git remotes are, how to list them, and provide some context on their usage.
Listing git remotes
Before you push your changes or fetch updates from the codebase, it's useful to check which remotes are configured. This helps ensure that you are interacting with the correct repository and can serve as a quick check to make sure you have configured your remotes properly.
How to list all remotes
To see which remotes are currently configured for your repository, you can use the git remote
command. This command lists the names of all remotes:
git remote
When run, this might output something like:
originupstream
Explanation:
- origin: This is the default name Git gives to the remote from which you cloned the repository.
- upstream: Often used to refer to the original repository when yours is a fork of another project.
Viewing remote urls
To see more detailed information about these remotes, including their URLs, you can use:
git remote -v
This command outputs the remote names along with the URLs associated with the fetch and push operations:
origin https://github.com/user/repo.git (fetch)origin https://github.com/user/repo.git (push)upstream https://github.com/original/repo.git (fetch)upstream https://github.com/original/repo.git (push)
Listing remotes with more detail
For even more detailed information about a specific remote, use:
git remote show <remote-name>
For example:
git remote show origin
This command will give detailed information about the remote called "origin," including its URL, the branches it tracks, and more.
Example output:
* remote originFetch URL: https://github.com/user/repo.gitPush URL: https://github.com/user/repo.gitHEAD branch: mainRemote branches:main trackeddevelop trackedLocal branch configured for 'git pull':main merges with remote maindevelop merges with remote developLocal ref configured for 'git push':main pushes to main (up to date)develop pushes to develop (up to date)
Managing git remotes
Beyond listing remotes, you may need to manage them by adding new ones or changing existing ones.
Adding a remote
In open-source projects, it's common to fork a repository to make changes independently. After forking, developers typically add the original repository as an additional remote (often named upstream) to their local clone. This setup allows them to fetch the latest updates from the original repository and keep their fork up to date. It's also necessary for submitting pull requests that merge their changes back into the main project.
If you want to add a new remote, run:
git remote add <remote-name> <remote-url>
For example:
git remote add upstream https://github.com/original/repo.git
Changing a remote's url
To change the URL of an existing remote, for instance if the repository has moved, run:
git remote set-url origin https://github.com/newuser/repo.git
Removing a remote
To remove a remote, for example if it was deleted, you can run:
git remote remove <remote-name>
If we were to remove the upstream we added in the last example, we'd run:
git remote remove upstream
For further reading, see the official Git documentation.