Everyone Can Learn Program
中文 Start learning
Core · GitLesson 5 of 6Free to read

Remotes: push and pull · Git and GitHub

How do you share commits with a team?

Course contents

Why 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/main is 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 lesson

Commit 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
Step by step
  1. L4Your branch is ahead of origin/main by 1 commit.
  2. L5Upload.
  3. L6Now up to date with origin/main.

Rejected, pull, push

Run it in the lesson

A 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
Step by step
  1. L3If you pushed now, it would be rejected: the server has a commit you don't.
  2. L4Fetch and merge the teammate's commit. Different files, so no conflict.
  3. L5Now the push goes through.
  4. 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?

  1. Agit push --force
  2. Bgit pull, then git push
  3. CDelete the repository and clone again
Show the answer

Answer: B

Bring their commits in, merge, then push. --force would delete their work.

2. What is `origin/main`?

  1. AYour local record of where main was on the server
  2. BA second copy of main you edit
  3. CThe newest commit on GitHub, always
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.

  1. PredictAhead or behind?

    After these commands, what does git status say about main and origin/main?

  2. ModifyDon't force it

    A teammate pushed add docs while you worked. This forces your push and would delete their commit. Do it the right way so the server ends up with both commits.

  3. WriteShare a branch

    Make a branch search, commit a new file search.py (any content) with the message add search, and push the branch to origin with git 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

TermChineseDefinition
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.