← Git & GitHub from Zero
Teamwork on GitHub: pull requests and merge conflicts

Lesson 5 of 7

Teamwork on GitHub: pull requests and merge conflicts

Publish a branch, open a pull request, catch up with your team using git pull --rebase, and resolve a merge conflict step by step.

The idea in plain words

Think of a shared document with an editor. You write your section in your own copy rather than in the final document. When it is ready you hand it to the editor for review, which is a pull request.

Meanwhile others keep editing, so before you hand in you catch up with their changes. If two writers changed the same sentence, someone has to pick the right words. That is a merge conflict, and a map of who changed what helps everyone follow the story.

Commands in this lesson

git push -u origin <branch>
Publish a new branch to GitHub so you can open a pull request.
Pull request
A request on GitHub to review your branch and merge it into main.
git pull --rebase
Fetch your team’s commits and replay yours on top for a straight history.
Resolve a merge conflict
Edit the file, remove the markers, then git add and git commit.
git log --graph --oneline
Draw the history as a map of branches and merges.

Written notes from the video

Why teamwork needs a process

What happens when two people change the same file at the same time? This is Git and GitHub from zero, episode five: teamwork, with pull requests and merge conflicts. It sounds scary. It is actually a routine skill, and you will see why.

The shared document analogy

Think of a shared document with an editor. You write your section in your own copy, not in the final document. When it is ready, you hand it to the editor for review. That is a pull request. Meanwhile, others keep editing. Before you hand in, you catch up with their changes. If two writers changed the same sentence, someone has to pick the right words. That is a merge conflict. And a map of who changed what, and when, helps everyone understand the story.

Command 1: git push -u and pull requests

Step one: work on a branch, and publish it with git push -u origin, and the branch name. The first time you push a new branch, the -u links it to GitHub. Now open GitHub. It offers to create a pull request for your branch. A pull request says: here are my changes, please review them, and merge them into main. Teammates comment, you fix things and push again, and the pull request updates itself. When everyone is happy, someone clicks merge, and your work joins main.

Command 2: git pull --rebase

Now a common problem. You try to push, and GitHub says no. The message says: rejected, fetch first. A teammate pushed while you were working. Fix it with git pull --rebase. It fetches their commits, then replays your commits on top of them. The history stays a clean, straight line. Now push again, and it works.

Command 3: merge conflicts

Sometimes Git cannot combine changes by itself. If two branches changed the same line, Git stops and marks the problem inside the file. The top part, after the HEAD marker, is your version. The bottom part is the incoming version. Nothing is broken. Git is simply asking you to choose.

Resolving a conflict

To resolve it, open the file and decide what the final text should be. Delete the three marker lines, and keep the wording you want. You can keep one version, or combine both. Then stage the file with git add, and finish with git commit. The conflict is resolved. Run your tests, and carry on. If you panic, git merge --abort takes you back to before the merge.

Command 4: git log --graph --oneline

Last command: git log, with graph and oneline. It draws your history as a map, with a star for each commit. Lines show branches splitting apart, and joining again. Our merge commit has two parents, one from each branch.

Recap

Recap. Push a branch with -u, then open a pull request for review. Catch up with git pull --rebase. Resolve conflicts by editing, adding, and committing. And see the whole story with git log --graph.

Next episode

Next episode: rescue tools. Stash, cherry-pick, reflog, reset and rebase. Subscribe so you do not miss it, and find written notes for every lesson at changecv.com/skills.

Common questions

What is a pull request?

A pull request is a request on GitHub to merge one branch into another. It gives your team a place to review the changes, comment, and ask for fixes before anything reaches main.

What causes a merge conflict?

Two branches changed the same lines of a file, or one branch edited a file the other deleted. Git cannot tell which version you want, so it stops and asks you to choose.

What do <<<<<<<, ======= and >>>>>>> mean in a conflicted file?

The part between <<<<<<< HEAD and ======= is your current version. The part between ======= and >>>>>>> is the incoming version. Delete the three marker lines and keep the text you want.

What is the difference between git pull and git pull --rebase?

git pull merges the remote changes into your branch and may create a merge commit. git pull --rebase replays your commits on top of the remote ones, which keeps the history in a straight line. Follow whichever convention your team uses.

How do I cancel a merge that went wrong?

While the merge is still in progress, run git merge --abort. Git puts your branch back to the state it was in before the merge.