Level 6

Push & pull

Push your local commits to a remote repository, and pull a friend's commits back into yours.

Congrats on pushing your way into Level 6. Speaking of pushing: when you add a new page to your scrapbook, you can send it to your friends. And when they add pages, you can fetch them into yours.

Since you duplicated your scrapbook with git clone, the copy already has the same three pages, same IDs and all. To test sending a page, you need a new page to send.

Step 1 of 9
0 xp

A fourth memento version

You edit brew.py and change its code to:

print("Chug coffee")

The changed memento lands on your table with its pencil badge. You know this dance from Level 3: stage it, then commit it.

Stage the change

Show off some of what you learned in earlier levels. First, the change goes to the staging area.

Type this into the terminal below
git add brew.py
safe 'add' stages files; nothing is discarded.

Commit it

Then commit it:

Type this into the terminal below
git commit -m "Update brew.py"

Your new fourth commit is at the front of the chain, on main, here in your local repository.

The remote, the copy you made in Level 5, has no idea this commit exists. It only knows about the three pages that existed when you cloned it.

safe Creates a new commit; nothing at risk.

Four here, three there

That is the gap you are about to close. Your book has four pages; the family copy has three.

Before you push for the first time there is one tiny extra step. You must tell Git that your local main branch should sync up with the remote's copy of main. The remote's copy of a branch is known as the upstream branch, or the remote branch. Once linked, Git knows where your pages go.

Push

git push sends every new page on your branch to the linked scrapbook named origin. --set-upstream links your local main with the remote's main, and it is only needed the first time. From now on, plain git push knows where to go.

Type this into the terminal below
git push --set-upstream origin main

On the graph a new label appears: origin/main. That is Git's note of where the remote's main was the last time you talked to it. Woohoo, you successfully pushed your fourth commit. Your friends and family can now see the fourth page in the official copy. Now stop dilly-dallying.

safe No upstream tracking ref found; cannot compare with remote.

A friend adds a page

Meanwhile, Your Friend has cloned the family copy too, and has stuck a fifth page onto it: a new file, friend.py. The shared scrapbook is now one page ahead of yours.

You need a way to retrieve that page from the shared copy into your own. In Git speak, that is called pulling changes into your repository, and git pull is the command for it.

Pull

Bring the page over:

Type this into the terminal below
git pull

Yup. Your main branch now contains a fifth commit, and you did not make it. You made the first four yourself; the fifth was made by Your Friend. Hover it and you will see their name on the stamp, and that they created a new file called friend.py.

Git's true purpose is enabling exactly this kind of collaboration and tracking by multiple developers.

safe 'pull' fetches and merges; local commits stay reachable.

Pull is two steps in one

Under the hood, git pull does two things: it fetches the remote's new pages, then merges them into your branch. You can do the first half on its own with git fetch, which downloads new pages and updates the origin/main label without touching your own branch. Handy when you want to look before you leap.

There is also a flag you will hear about: git push --force tells the remote to accept your branch no matter what it currently holds. On a branch someone else pushed to first, that erases their pages. It has its uses, and everyday work is not one of them.

Your turn: what goes up

You pulled your friend's page and made no new commits since your push. Which command would send something to the remote right now?

git-sim
Initial CommitDevlands

The graph appears here when the first command runs. The simulations are drawn by git-sim from a real repository.

The scrapbook · one picture for every Git idea

The scrapbook picture appears as the story is told.