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.