Remotes: push and pull · Git and GitHub
How do you share commits with a team?
Course contentsWhy you need it
Your repository lives on your computer; the team's copy lives on a server such as GitHub. You push your commits up and pull your teammates' commits down. Getting this rhythm right is most of what "using Git on a team" means day to day.
The intuition
A remote is a named address of another copy, usually origin.
git push: send your new commits to origin.git pull: fetch new commits from origin and merge them into your branch.origin/mainis your local note of where main was on the server, last time you looked.
If a teammate pushed first, your push is rejected: the server has commits you don't. Pull, fix any conflict, then push again. Never force your way past this on a shared branch.
Definition
Remote
Another copy of the repository, reached by a URL and a short name such as origin.
Push and pull
push uploads your commits; pull downloads others' commits and merges them.
Examples
Push your work
Run it in the lessonCommit locally, then push. Until you push, nobody else can see it.
echo "def pay(): pass" > pay.py
git add pay.py
git commit -m "add payment"
git status
git push
git status- L4Your branch is ahead of origin/main by 1 commit.
- L5Upload.
- L6Now up to date with origin/main.
Rejected, pull, push
Run it in the lessonA teammate pushed while you worked. Pull their commit first, then push.
echo "def pay(): pass" > pay.py
git add pay.py
git commit -m "add payment"
git pull
git push
git log --oneline- L3If you pushed now, it would be rejected: the server has a commit you don't.
- L4Fetch and merge the teammate's commit. Different files, so no conflict.
- L5Now the push goes through.
- L6Your commit, theirs, and the merge that joins them.
Common mistakes
git push --force on a shared branch
Force-pushing replaces the server's history with yours and silently deletes teammates' commits. Pull instead.
Pushing secrets
Once a password is pushed, assume it's public, even if you delete it in the next commit: the history still has it. Change the password, and use .gitignore (next lesson).
Quick quiz
1. `git push` is rejected because the remote contains work you do not have. What do you do?
Show the answer
Answer: B
Bring their commits in, merge, then push. --force would delete their work.
2. What is `origin/main`?
Show the answer
Answer: A
It's only updated when you fetch, pull or push, so it can be out of date.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictAhead or behind?
After these commands, what does git status say about main and origin/main?
- ModifyDon't force it
A teammate pushed
add docswhile you worked. This forces your push and would delete their commit. Do it the right way so the server ends up with both commits. - WriteShare a branch
Make a branch
search, commit a new filesearch.py(any content) with the messageadd search, and push the branch to origin withgit push -u origin search.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Remote | 远程仓库 | Another copy of the repository, such as one on GitHub. |
| Push | 推送 | Upload your commits to a remote. |
| Pull | 拉取 | Download a remote's commits and merge them. |