Git Remote

A remote is a copy of your repository in another place, usually on a server like GitHub, GitLab or Bitbucket. The git remote command manages the connections to those places. The default name of the main remote is origin.

Syntax

git remote
git remote -v
git remote add <name> <url>
git remote rename <old> <new>
git remote set-url <name> <new-url>
git remote remove <name>
git remote show <name>

Add a remote

We use a local bare repository as the server in this example. With GitHub, the URL will be like https://github.com/username/project.git or git@github.com:username/project.git.

Terminal output

$ git remote
$ git remote add origin ../server.git
$ git remote -v
origin ../server.git (fetch)
origin ../server.git (push)

Push to it and see the details

Terminal output

$ git push -u origin main
To ../server.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
$ git remote show origin
* remote origin
Fetch URL: ../server.git
Push URL: ../server.git
HEAD branch: main
Remote branch:
main tracked
Local branch configured for 'git pull':
main merges with remote main
Local ref configured for 'git push':
main pushes to main (up to date)

Change the URL

Use this when the repository is moved, or when you want to change from HTTPS to SSH.

git remote set-url origin git@github.com:username/project.git

More than one remote

You can add many remotes. A common case is a fork, where origin is your copy and upstream is the original project.

git remote add upstream https://github.com/original-owner/project.git
git fetch upstream

Rename and remove

Terminal output

$ git remote rename origin backup
$ git remote -v
backup ../server.git (fetch)
backup ../server.git (push)
$ git remote remove backup
$ git remote -v

Why origin?

The name origin is not special. It is just the default name Git gives to the place you cloned from. You can use any name. Using the same name in all projects helps you remember.

Note: After you add a remote, you can send your commits with git push and take the new ones with git pull. Next: Git Push.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.