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 What Each Merge Button Does

In this video, you pictured your first pull request, format-and-lint with its four commits, merged with each of GitHub’s three merge buttons. You saw why git branch -d refused to delete your branch after the squash merge, even though GitHub said Merged. In the video, the letters A to D stand in for the ids of your branch’s four commits. Here are the key points:

  • Every commit has its own id, the hash that git log shows. Two commits with the same message and the same changes are still two different commits if their ids differ.

  • Create a merge commit keeps your commits as they are. GitHub adds a merge commit to main with two parents: the last commit of main and your branch’s last commit. It makes that merge commit even when main didn’t move and a merge on your laptop would have been a fast-forward.

  • Squash and merge makes one new commit. It holds the changes of all four commits, has one parent, and takes the pull request’s title as its message. Your four commits aren’t in the history of main at all.

  • Rebase and merge copies your commits onto the tip of main, one by one, without a merge commit. The copies have the same messages and the same changes, but every copy is a new commit with a new id. GitHub makes new copies even when main hasn’t moved.

  • Here, git branch -d looks for your branch’s own commits in the history of main. That’s on main, after you’ve deleted the branch on GitHub, run git pull and run git fetch --prune. Git compares ids, not messages. After a merge commit, the commits are there, so the branch is deleted without complaint. After a squash or a rebase merge, they aren’t, even though all your work is in main.

This is what git branch -d says after each button, once the branch is deleted on GitHub and you’ve run git pull and git fetch --prune on main. After Create a merge commit, it deletes the branch, and … stands for your branch’s last commit id:

Language: Shell
$ git branch -d format-and-lint
Deleted branch format-and-lint (was …).

After Squash and merge or Rebase and merge, it refuses, starting with this line:

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 lesson, you squash-merged your first pull request. Then git branch -d refused to delete your branch, even though GitHub said it was merged.

00:11 That refusal makes sense once you picture what the merge button does to main. Its menu offers three ways to merge. Each one leaves a different history behind.

00:22 Here’s your history right before you clicked. Each dot is a commit, pointing back to its parent. Main ends at the commit that planned the move to pull requests.

00:32 Your branch, format-and-lint, adds four commits on top. Every commit has its own id, the hash that git log shows. Here, the letters A to D stand in for those ids.

00:46 Start with Create a merge commit, the default. GitHub keeps your four commits as they are, with the same ids. It adds a merge commit to main with two parents.

00:57 One is main’s last commit. The other is D. A merge on your laptop would have been a fast-forward here, because main didn’t move while the pull request was open.

01:07 GitHub’s button makes a merge commit anyway.

01:11 Now follow the arrows back from main. Through the arrow to D, you reach D, C, B and A. So git branch -d finds all of your branch’s commits in main’s history.

01:24 It deletes the branch without a word of complaint.

01:28 Now rewind to before the merge, and pick Squash and merge this time. GitHub takes the changes of all four commits and puts them in one new commit on main.

01:38 It has one parent, and the pull request’s title as its message. Here it’s called S. Follow the arrows back from main, and you find S, then the commits main already had.

01:50 A, B, C and D aren’t among them. Your branch still ends at D, and D isn’t in main’s history, so git branch -d refuses. All your work is in main, though, inside S.

02:06 The third button is Rebase and merge. GitHub copies your four commits onto the tip of main, one by one. The copies have the same messages and the same changes.

02:18 Your turn. Pause the video and predict whether git branch -d counts these copies as your branch’s commits.

02:30 It doesn’t. Every copy is a new commit with a new id, so here they’re A′ to D′. Git compares ids, not messages. So A to D still aren’t in main’s history, and git branch -d refuses, the same as after a squash.

02:50 GitHub makes these new copies even when main hasn’t moved.

02:56 So git branch -d looks for your branch’s own commits in main’s history. You run it on main, after the pull and the prune. After a merge commit, they’re there.

03:07 After a squash or a rebase merge, they aren’t, even though all your work is.

03:13 That’s also why you don’t keep working on the old branch. A new pull request from it would list A to D again, and show their changes as new. Delete it, with git branch -d first.

03:26 Use a capital D only when it refuses, and then only if the pull request is merged and you haven’t added anything since. Then start the next change on a fresh branch from an updated main.

03:38 This course squashes, so main gets one commit per pull request. In the next lesson, you’ll start the second issue, the closing-soon notice, on a fresh branch.

Become a Member to join the conversation.