Skip to content
Lesson: Git RemotesSwitch
← DevOps Operations

Delivery and containers

Git Remotes

Connect your repository to another copy and exchange commits with fetch, pull and push.

A remote is a saved name and address for another copy of your repository, usually on a server. You exchange commits with it to share work.

List and add remotes

If you cloned a project, a remote called origin already exists:

$ git remote -v
origin	https://example.com/team/website.git (fetch)
origin	https://example.com/team/website.git (push)

If you started with git init, add one yourself:

$ git remote add origin https://example.com/team/website.git

origin is only a conventional name. Adding a remote does not transfer anything. Addresses can use HTTPS or SSH, such as [email protected]:team/website.git.

Push your commits

The first time you push a branch, set its upstream with -u:

$ git push -u origin main

After that, plain git push on that branch knows where to go. A push is rejected if the remote branch has commits you do not have. Get those commits first, then push again. Avoid forcing a push on shared branches, because it can discard other people's work.

Fetch and pull

git fetch downloads new commits and updates remote-tracking names such as origin/main. It leaves your own branches and files alone, so you can look before you combine:

$ git fetch origin
$ git log --oneline main..origin/main
7ad90be Update footer

git pull does a fetch and then integrates the changes into your current branch, by merging or rebasing depending on your settings. Recent Git versions ask you to choose if both sides have new commits and you have not configured this. You can be explicit:

$ git pull --no-rebase

Use --rebase to replay your commits on top of the new ones, or --ff-only to update only when no merge or rebase is needed.

Change or remove a remote

$ git remote set-url origin [email protected]:team/website.git
$ git remote remove origin

Removing a remote only changes your local configuration. The repository on the server is untouched.

SSH addresses need a key set up on the server. The SSH lab covers how keys work. See also the git remote documentation.

Try it

Run git init --bare /tmp/hub.git to make a local "server". Add it as a remote of a practice repository, push, then clone /tmp/hub.git into another folder. If the clone warns that the remote HEAD refers to a nonexistent ref, run git switch with your branch name inside it.

Check your understanding

Git Remotes

All 3 sections · no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.