Remotes and synchronization
Use a remote, pull request and automated check to propose a reviewable change.
THE IDEA
A remote is another copy of the repository
Git has no central server built in. Every clone is a full repository. A remote is just a name for the address of another copy, most often one on a hosting service. When you run git clone you get every commit, and Git writes down one remote called origin.
git clone https://forge.example/acme/shop.git
cd shop
git remote -v
# origin https://forge.example/acme/shop.git (fetch)
# origin https://forge.example/acme/shop.git (push)
git branch -vv
# * main 3943813 [origin/main] Add cartNotice origin/main in the brackets. That is a remote-tracking branch: your repository's bookmark for "where main was on origin the last time I talked to it". You cannot commit on it. Only fetch, pull and push move it.
Fetch, pull and push move different things
git fetch downloads new commits and moves your bookmarks. It never touches your branches or your files. git pull is fetch followed by an integration (a merge, or a rebase if you ask for one). git push uploads your commits and moves the branch on the server, but only if that move is a fast-forward.
The server (origin)
Your laptop (origin/main is your last known copy of the server)
What git status says right now
Press a button. Every command runs on real simulated repositories.
Predict first. Maya pushed two commits to the server a minute ago. You have not run any Git command since. You type git status. What does it say about origin/main?
When both sides have moved
If the server has commits you do not have and you have commits it does not have, the histories have diverged. A push would make the server drop Maya's work, so the server refuses it ("fetch first" when you have not even downloaded her commits, "non-fast-forward" when you have). The fix is always to integrate first: git pull (current Git asks you to choose merge or rebase when branches have diverged) and then push.
git fetch # see what is new, change nothing
git log main..origin/main # read the incoming commits
git pull --no-rebase # merge them (or: git pull --rebase)
git push Predict first. A push is rejected with (fetch first). What is the best first move?
Chapter context
GitHub hosts repositories and adds collaboration around Git objects. A pull request is a review and discussion mechanism; the commits and branch history still follow Git rules.
TRY IT YOURSELF
You are in your home folder. The hosted repository is https://forge.example/acme/shop.git. A teammate will push while you work; the graphs on the right show your repository and the server.
- Copy the hosted repository to your machine with clone, then go into the new folder
- A teammate has pushed since you cloned. Download their commits WITHOUT changing your branch or your files
- Read what arrived before you integrate it: list the commits that origin/main has and your main does not
- Make your own commit (for example a notes.txt file) so that your branch and origin/main have both moved
- Try a plain git push. The server must refuse it because it would throw the teammate's work away
- Integrate their commits (git pull --no-rebase), then push until your main and the server's main are the same commit
Your repository
Not cloned yet. Use git clone.
The server
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Is fetch the same as merge?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
For independent jobs with durations t1 and t2 running simultaneously, ideal elapsed time is max(t1,t2), excluding startup and queueing. A dependency adds a sequential edge and can make durations add instead.
Worked example
A build takes 3 minutes. Two test jobs taking 4 and 6 minutes start after the build and run together. The ideal critical path is 3 + max(4, 6) = 9 minutes, not 13. Real runners can add scheduling overhead.
Reference code
The following example uses the Python standard library and does not call a remote service.
build, tests = 3, [4, 6]
assert build + max(tests) == 9
assert build + sum(tests) == 13
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
A green check is evidence for the checks that ran, not proof of every behavior. Forked code must not automatically receive trusted secrets.