The Plain Picture#
A shared album in the school library. Everyone keeps their own full copy at home (Git is distributed, Ch 1). You go to the library to pick up photos others added, and to drop off yours.
The Real Names#
A remote is another copy of the repository, usually on GitHub. The default name for the one you cloned from is
origin.git clone <url>— download a full copy of a repository, including all history.git fetch— download new commits from the remote, without changing your branches. They appear as remote-tracking branches likeorigin/main— your local record of where the remote's branches were last time you looked.git pull=git fetch+ merge (or rebase, with--rebase)origin/maininto your current branch.git push— upload your commits to the remote and move its branch pointer.
Under the Hood#
GitHub (origin) Your laptop
─────────────── ───────────
main ──▶ C fetch origin/main ──▶ C (your record of GitHub's main)
────────▶ main ──▶ B (your local main, behind)
pull = fetch + merge origin/main into main
<────────
push your new commits go up; GitHub's main moves forwardConnecting with SSH keys: GitHub authenticates you with the same kind of SSH key pair as servers
(The Linux Command Line Ch 10): add your public key in GitHub's settings, then clone with
git@github.com:org/studybuddy.git. (The alternative is HTTPS with a personal access token.)
A normal day:
git switch main
git pull --rebase # get the latest
git switch -c feature/quiz-timer # new branch for today's work (Ch 4)
# ... edit, then:
git add -p && git commit -m "Add countdown timer to quizzes"
git push -u origin feature/quiz-timer # -u: remember that this branch tracks origin/feature/quiz-timerPush rejected? ! [rejected] main -> main (fetch first) means the remote has commits you don't have. Pull first, then
push again. Don't reach for --force.
git push --force replaces the remote branch with yours, deleting any commits on the remote that you don't have.
On a shared branch that destroys teammates' work. If you really must (e.g. after rebasing your own feature branch), use
git push --force-with-lease, which refuses if someone else pushed in the meantime.
Check yourself
4 questionsfetch vs pull?
Show answerHide answer
fetch downloads new commits without changing your branches; pull fetches and then merges (or rebases) them into
your branch.
What is origin/main?
Show answerHide answer
Your local record of where the remote's main branch pointed the last time you fetched.
Your push is rejected with "fetch first". What happened, and what do you do?
Show answerHide answer
Someone else pushed commits you don't have. Pull (merge or rebase), resolve anything, then push again.
Why is --force-with-lease safer than --force?
Show answerHide answer
It refuses to overwrite the remote if it contains commits you haven't seen, so you can't silently delete others' work.