A remote is a named address that your local Git repository can use to contact another repository. The other repository can live on a hosting service or in a directory on the same machine. This practice example uses a separate local directory, so it works without a hosting account.
Git · Lesson 6
Remotes
Learn how a Git remote connects repositories and distinguish fetching from pushing.
Adding an Address
From the practice repository, create a separate bare repository. A bare repository holds history and refs without a project working tree. It can receive commits from your workspace. A fresh practice run needs an unused journey-remote.git directory.
$ git init --bare --initial-branch=main ../journey-remote.git
$ git remote add origin ../journey-remote.git
$ git remote -v
The name origin is a common convention. The remotes listing shows which address that name refers to.
You ran git remote add and saw origin in the list. What did Git do?
Sending a Branch
Use git push to send local commits and ask the remote to update its branch. The -u option records the remote branch as the upstream, the branch Git associates with your local one for later commands.
$ git push -u origin main
This pushes only to the separate practice directory. A shared remote may require authorization and can reject updates, so check its response before treating a push as successful.
Which command sends a new local main commit to this remote?
Receiving an Update
Use git fetch origin to refresh your local view of remote history. Git uses a remote-tracking branch, such as origin/main, to record what it last learned. You can inspect it with git log --oneline origin/main. Fetch by itself leaves the checked-out working tree on your current local branch.
$ git fetch origin
$ git log --oneline origin/main
origin/main moved after a fetch, but your local main stayed put. Which statement fits?
Lesson complete
You finished Remotes
You can now track which repository a Git command affects.
Add and inspect a remote address.
Push a branch and identify its upstream.
Fetch new remote history without changing the checked-out files.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account