Remotes and collaboration
Understand the idea
A remote is another repository. push, fetch, and pull move labels between the two.
The idea
A is another repository known by a short name. is only the usual name. git clone their-slug creates your own repository whose origin points at theirs and whose commits start as a copy. git fetch updates origin's recorded heads. git pull fetches and then merges that head into your branch. git push moves their branch only when your commit contains their tip, unless you pass --force.
What changes
Fetch adds their commits to your object store and sets origin/main on the graph. Your stays put. Push, when you own the target, copies your commits into that repository and moves its label. If their label has commits you do not have, the push stops. --force moves it anyway and is the overwrite the undo lesson warned about. You can fetch a classmate's visible repository. You cannot push into it.
| Command | Your branch | Their branch |
|---|---|---|
| git fetch origin | Unmoved | Recorded as origin/main |
| git pull origin main | Merges their tip | Unmoved |
| git push origin main | Unmoved | Moves, if the push is a fast-forward |
| git push --force origin main | Unmoved | Moves even if commits are dropped |
The command
On a repository you own twice, or on a clone, run git remote add origin their-slug, then git push origin main. Clone with git clone their-slug. The new repository is yours, origin tracks theirs, and the graph shows the copied commits. The command reference is the full set this terminal runs.
The mistake
Check your graph
After fetch, origin/main appears on their tip and your main label does not move. After a fast-forward push, their branch hash matches yours. A rejected push leaves both labels where they were.
Your repository
Add origin, push a branch, and clone a visible repository on this platform.
Step 1
Name the other repository
origin is the usual name. practice is a second repository created for you when you run this, so you have somewhere to push.
Step 2
Make a commit the remote does not have
In the editor, add this line to README.md: Ready to push
Step 3
Stage the new line
origin does not have this edit until you commit and push.
Step 4
Commit it
Your main is now ahead of origin.
Step 5
Push your branch
This moves origin’s main to your commit, because the push is a fast-forward and you own that repository.
Step 6
Fetch without moving your branch
Fetch records origin/main on the graph. Your own main label stays where it is.
The editor edits the working tree. Start a repository and the files appear here.
Commit graph
No commits yet. git init creates the first one.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.