Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Picture How Fetch, Pull and Push Move

In this video, you replayed the round trip between your laptop, GitHub and the back-office computer as a picture of three copies. Each copy has its own main, and your laptop and the back office also keep an origin/main, which records what that computer last heard about GitHub’s main through a fetch or a successful push.

Key Points

git push copies your new commits to GitHub and moves GitHub’s main, but only as a fast-forward, to a commit that already has GitHub’s current commit behind it. A push that goes through also moves your origin/main. When GitHub has a commit your branch doesn’t, such as next week’s deals from the back office, GitHub refuses the push:

Language: Shell
 ! [rejected]        main -> main (fetch first)

git status compares main with origin/main, your record of GitHub. Right after the back office pushed, the laptop still said:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last two lessons, a pull merged the back office’s work into yours. But the back office doesn’t have your deli hours yet. When you pull there, will it merge too?

00:10 A picture of what each command moves tells you. There’s a copy of the project on your laptop, one on GitHub, and one on the back-office computer. All three start at the commit that planned the move to GitHub.

00:24 Each copy has its own main. Your laptop and the back office also keep origin/main next to it. That’s what each computer last heard about GitHub’s main, through a fetch or a successful push.

00:38 Start at the back office. You commit next week’s deals there, and the back office’s main moves up to the new commit. Then you push. Git copies the commit to GitHub, and GitHub’s main slides forward to it.

00:52 The back office’s origin/main follows, because a push that goes through updates the record too. Now look at your laptop. Its main and its origin/main still sit on the first commit.

01:05 That’s why the laptop’s git status said up to date. It compared main with a record that was out of date.

01:13 On the laptop, you commit the deli counter’s hours. The laptop’s main moves up to it. When you push, GitHub’s main would have to jump from the deals to the deli hours.

01:25 The deli commit doesn’t have the deals behind it, so that jump would drop the deals from GitHub’s main.

01:32 A push only moves GitHub’s main forward, to a commit that already has GitHub’s current commit behind it, like the back office’s push a moment ago. That’s a fast-forward.

01:43 This push isn’t one, so GitHub refuses it, and your laptop keeps its commit.

01:49 So you run git fetch on the laptop. Git copies the deals commit from GitHub to your laptop, and the laptop’s origin/main moves to it.

01:59 Your main stays where it was, and so do your files. A fetch only updates the laptop’s record of GitHub.

02:07 Main and origin/main have split, one commit on each side. Git status calls that diverged.

02:17 A pull does that fetch for you, then merges origin/main into your main.

02:23 Here both sides have a commit of their own, so main can’t slide forward. Git makes a merge commit with two parents, the deli hours and the deals. Your main moves up to it, and your files now have both changes.

02:38 Now the push works. Git copies the deli hours and the merge to GitHub. The merge commit has the deals behind it, so GitHub’s main slides up to it as a fast-forward, and your origin/main follows.

02:53 Back to the opening question. You haven’t pulled at the back office yet. Look at its column. Your turn: pause the video and predict whether this pull makes a merge commit or a fast-forward.

03:09 It’s a fast-forward. The fetch brings the deli hours and the merge, and origin/main moves up. The back office has no commits of its own, so its main slides forward to the merge, and its files get the deli hours.

03:24 Git prints Fast-forward, and all three copies match again.

03:29 Here’s what each command moves. A push moves GitHub’s main, only as a fast-forward, and your origin/main follows. A fetch moves only your origin/main, never your own branch or files.

03:43 A pull moves your main and your files. If you’re only behind, main slides forward. If you’ve diverged, you get a merge commit. If GitHub has nothing new, Git says Already up to date.

03:57 So fetch first, and git status tells you whether you’re behind or diverged. Then you know what a pull will do. Next, a short quiz lets you check your fetch, pull and push.

04:08 After that, you’ll see what happens when both sides change the same line.

Become a Member to join the conversation.