Same-Branch Collaboration

Course 1 · Ch 10 · Final Chapter · Scenario
Two People on the Same Branch
Push rejections, fast-forward pulls, and resolving your first simple merge conflict — what happens the moment a second person joins your repo

Every chapter so far assumed you're the only person touching the repository. The moment a second person commits to the same branch, a few new situations become possible — none of them are mistakes, all of them are completely normal, and this final chapter of Course 1 walks through exactly what each one looks like and how to handle it calmly.

The Core Rule: Git Won't Let You Silently Overwrite Someone Else's Work

This is the single most important thing to understand before working with anyone else: git is specifically designed to reject a push that would erase commits it doesn't recognise. You cannot accidentally destroy a collaborator's work just by pushing — git stops you and asks you to reconcile first.

Scenario A: Someone Else Pushed Before You

You
Your collaborator
09:00
Pull latest master, both start at the same commit
Pull latest master, same commit
09:30
Editing header.css locally
Commits and pushes a fix to footer.css
09:45
Commits header.css, tries to push
$ git push
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'github.com:you/project.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally.

This is not an error in the sense of something being broken — it's git correctly noticing that the remote has a commit (your collaborator's footer fix) that your local branch doesn't have. The fix is simply to pull first:

$ git pull
Merge made by the 'ort' strategy.
footer.css | 3 +++
$ git push
To github.com:you/project.git
a1b2c3d..9f8e7d6 master -> master

Because you and your collaborator edited different files (header.css vs footer.css), git merges them automatically with no conflict at all — this is the most common outcome by far in day-to-day collaboration.

Pull before you start work, not just before you push
Pulling at the start of a working session, not just when a push gets rejected, reduces how often your local branch drifts far from the remote — smaller, more frequent catch-ups produce smaller, easier-to-resolve differences than one big gap.

Scenario B: You Both Edited the Same Lines — a Real Conflict

Sometimes the overlap is real: you and your collaborator both changed the exact same line of the exact same file. Git can't guess which version is correct, so it pauses and asks you to decide.

$ git pull
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Opening index.html shows git's conflict markers directly in the file:

<header>
<<<<<<< HEAD
<h1>Welcome to my site</h1>
=======
<h1>Welcome, friend!</h1>
>>>>>>> 9f8e7d6 (their commit)
</header>

Everything between <<<<<<< HEAD and ======= is your version; everything between ======= and >>>>>>> is theirs. Resolving means manually editing the file to keep whichever version is correct (or a combination of both) and removing the marker lines entirely.

1
Open the conflicted file and decide what the final version should look like
Talk to your collaborator if it's unclear which version (or what combination) is correct — this is a conversation, not just a technical resolution.
2
Delete the <<<<<<<, =======, and >>>>>>> marker lines
Leaving any marker lines in place will break the file — they're not valid HTML/code, just git's way of showing you both versions side by side.
3
Stage the resolved file and commit
git add index.html then git commit — git pre-fills a "Merge branch..." message; usually fine to keep as-is.
4
Push the resolved merge
A normal git push — the conflict is resolved locally, so this completes the sync.
If you panic mid-conflict, you can always back out
git merge --abort (or git pull --abort if mid-pull) cancels the merge entirely and returns you to exactly where you were before — no harm done. There's no time pressure to resolve a conflict immediately; backing out and asking a collaborator for help is always a safe option.

Preventing Most Conflicts Before They Happen

  • Pull frequently — at the start of each session, and before pushing, not just when forced to.
  • Use branches for anything non-trivial (Chapter 4) — working in your own branch and merging via a reviewed PR (Chapter 7) catches most overlaps before they become a tense same-branch conflict.
  • Communicate about who's touching what — a quick "I'm working on the header right now" message prevents most same-line conflicts before git ever needs to flag one.
  • Commit and push in smaller, more frequent chunks — large, infrequent commits diverge further from the shared branch and produce bigger, harder conflicts when they finally meet.
This is genuinely the easy version of this problem
Everything in this chapter assumes two people, working politely, on small changes. Course 2's "multiple users on the same branch" scenario chapter covers what happens at team scale — diverged history, force-push dangers, and branch protection rules — and Course 3 covers recovering when a history rewrite goes wrong across a team. This chapter is the foundation those build on.

Chapter 10 Quick Reference — and Course 1 Wrap-Up

  • Push rejected ("fetch first"): the remote has commits you don't — just git pull, then push again
  • Different files changed: git merges automatically, no conflict, no manual intervention needed
  • Same lines changed: a real conflict — git inserts <<<<<<< / ======= / >>>>>>> markers for you to resolve manually
  • Resolving: edit to the correct final version, delete all marker lines, git add, git commit, git push
  • Stuck or unsure? git merge --abort backs out completely, no harm done — ask a collaborator if needed
  • Prevent conflicts: pull often, use branches for non-trivial work, communicate, commit in smaller chunks
  • Course 1 recap: install & setup (Ch1) → commits (Ch2) → history (Ch3) → branching (Ch4) → GitHub (Ch5) → .gitignore (Ch6) → pull requests (Ch7) → undoing mistakes (Ch8) → moving projects (Ch9) → collaboration basics (Ch10)
  • Next: Course 2 — Git & GitHub for Teams (branching strategies, merge vs rebase, fork workflows, CI basics, and team-scale scenarios)