URLs are long, complicated, and easy to mistype. It's much easier to give each remote a name. The git remote
command lets us manage a list of remotes for each git repository.
To add a new remote repository, you can use the git remote add
command, followed by a remote name for the remote repository and remote repository URL.
git remote add <remote-name> <remote-repository-url>
For example, to add a remote repository with the remote name origin
and the URL https://github.com/user/repo.git
, you can use the following command:
git remote add origin https://github.com/user/repo.git
To rename a remote repository, you can use the git remote rename
command followed by the current remote name of the remote repository and the new remote name you want to assign.
git remote rename <old-remote-name> <new-remote-name>
For example, to rename a remote repository with the shortname origin
to upstream
, you can use the following command:
git remote rename origin upstream
To remove a remote repository, you can use the git remote remove
command followed by the remote name of the remote repository.
git remote remove <remote-name>
For example, to remove a remote repository with the remote name upstream
, you can use the following command:
git remote remove upstream
To fetch updates from a remote repository, you can use the git fetch
command followed by the remote name of the remote repository. This command will download the latest changes from the remote repository without merging them into your local branch.
git fetch <remote-name>
For example, to fetch updates from a remote repository with the remote name origin
, you can use the following command:
git fetch origin
To merge the fetched changes into your local branch, you can use the git merge
command. However, a more convenient way to accomplish this is to use the git pull
command, which fetches and merges the changes in a single step. To pull updates from a remote repository, use the git pull
command followed by the remote name of the remote repository and the remote branch you want to pull from.
git pull <remote-name> <remote-branch-name>
For example, to pull updates from the main
branch of a remote repository with the remote name origin
, you can use the following command:
git pull origin main
To push changes to a remote repository, use the git push
command followed by the remote name of the remote repository and the local branch name you want to push.
git push <remote-name> <local-branch-name>
For example, to push changes from your local main
branch to a remote repository with the remote name origin
, you can use the following command:
git push origin main
To check the status of remote branches, you can use the git remote show
command, followed by the remote name of the remote repository. This command will display information about the remote branches, such as their status compared to your local branches and any new branches that have been added to the remote repository.
git remote show <remote-name> <remote-branch-name>
For example, to check the status of remote branches for a remote repository with the remote name origin
, you can use the following command:
git remote show origin main