Skip to content
Tutorials Center
My learning

Topics

Lesson 71 of 78 ยท Projects

Project: Simulating a Team Workflow

Play both sides of a team workflow in one script: a bare remote, two clones, feature branches, pull requests as merges, review by diff, a merge conflict and a rejected push.

Most Git problems appear only when more than one person is involved: a push is rejected, a branch is out of date, two people changed the same line. You cannot learn that from a single repository, and you do not need a team or a hosting service to practice it. A bare repository on the local disk can play the server, and two clones can play two developers.

In this project you will run a whole collaboration in four stages: set up a shared remote, work on two feature branches in parallel, review and merge them as if they were pull requests, resolve a conflict, and survive a rejected push. You are Alice, Bob and the maintainer at the same time, so you see both sides of every step. Each stage is a complete script that repeats the earlier stages, so every example runs on its own.

Pull requests are a feature of hosting services such as GitHub, not of Git. What the services do underneath is simple: they compare two branches, show the difference for review, and when someone clicks the merge button they create a merge commit on the target branch. We do exactly those steps with git fetch, git diff main...branch and git merge --no-ff. For the real services see Pull Requests and GitHub Basics.

The setup

The picture for the whole project:

TEXT
                     origin.git  (bare repository = the "server")
                      ^        ^
          push / fetch |        | push / fetch
                       |        |
                  alice/        bob/        (two clones = two developers)
              plan.txt          plan.txt    (each has its own working tree,
                                             its own branches, its own config)

A bare repository has no working tree, only the Git database, which is why it is the right shape for something you push to. Each clone gets its own user.name and user.email through git -C <clone> config, so commits carry the right author without any environment variables. The helper functions alice and bob at the top of the script run git -C alice ... and git -C bob ... (the -C option means "act as if Git was started in that directory") and fix the commit date, so the hashes you see are reproducible.

The shared project is a trip plan: a text file plan.txt with three lines. Conflicts need two people editing the same line, and the plan gives them one, the departure time.

Stage 1: a remote, two clones, two branches

Alice creates the first commit and pushes it. Bob clones afterwards. Then both create a feature branch from the same starting point and make their change: Alice adds a packing list and moves the departure to 09:00, while Bob moves the departure to 09:30. Both change the same line, but neither knows about the other yet, and that is the usual cause of conflicts.

Bash Stage 1: remote, clones and branches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

This stage prints nothing, so there is nothing to compare, but three things happened that matter later. push -u set the upstream of each local branch, so a plain git push and git pull on those branches know where to go (Push and Pull). Both feature branches now exist on the server, which is what a pull request is built on. Neither developer has seen the other's work: the clones only update when someone runs git fetch.

Stage 2: code review by diff, then the merge button

Alice reviews Bob's branch. A reviewer needs two answers: which commits are in the branch, and what do they change? git log main..origin/feature/schedule lists commits that are on the feature branch and not on main. git diff main...origin/feature/schedule (three dots) compares the branch tip to the point where it left main, so it shows only what the branch itself changed, even if main has moved on since (Comparing Commits).

The review leads to a request: "please also state the arrival time". In a hosting service that is a comment on the pull request. Bob answers with another commit on the same branch and pushes it, and the review simply continues on the updated branch: the pull request is the branch, not a snapshot of it.

When Alice approves she plays the merge button: git merge --no-ff -m "Merge pull request #1 from bob/feature/schedule". --no-ff guarantees a merge commit that groups the branch's commits (Merging). The server's graph shows the shape: two commits on a side line, joined by the merge commit.

Bash Stage 2: review and merge pull request 1

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Stage 3: an out-of-date branch and a conflict

Now it is the turn of Alice's pull request. Her branch still has the old departure time, and main has moved: Bob's merged branch also changed the departure line. Hosting services call this state "this branch is out of date" or "has conflicts". The author of the branch fixes it by bringing main into the branch (a merge, or a rebase) and resolving the conflict there, not on main.

Alice runs git merge origin/main on her feature branch and Git stops: CONFLICT (content): Merge conflict in plan.txt. git status -s shows UU (both sides modified, unmerged), and the file now contains conflict markers:

  • between <<<<<<< HEAD and ======= is the version from the branch you are on (09:00);
  • between ======= and >>>>>>> origin/main is the incoming version (09:30);
  • everything outside the markers merged cleanly, including Bob's Arrival: 12:15 line.

Resolving a conflict is an editing job: decide what the final text should be, remove the markers, then git add the file and commit. The script writes the finished file in one printf. The choice here is 09:30, because the later train was agreed in the review; the right resolution always depends on the project, not on Git. See Resolving Merge Conflicts for tools and strategies.

The resolved branch is pushed. Bob, now the reviewer, uses diff --stat for an overview, brings his local main up to date with git pull, merges pull request 2, and pushes.

Bash Stage 3: conflict and pull request 2

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Look at the server's graph at the end. It tells the whole story: pull request 1 merged first, then Alice's branch with the merge-from-main commit and the final merge of pull request 2. Some teams prefer to avoid the extra "Merge remote-tracking branch" commit by rebasing the branch onto main instead, which gives a linear history at the price of rewriting the branch's commits (and the golden rule: only rebase branches nobody else uses).

Stage 4: two people push to main at once

Even with branches and reviews, two people sometimes push to the same branch within minutes. The second push is rejected, because accepting it would throw away the first person's commit. Alice pushes a README first. Bob has already made his own commit on top of the old main, and when he pushes Git says:

TEXT
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to ...
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. ...

(That is the real message from the run below, shortened.) Nothing is broken. Bob's commit is safe locally, and the fix is to integrate Alice's commit first. git pull --rebase fetches, then replays Bob's commit on top of the new main, which keeps the history a straight line. Then the push is accepted. Finally the script checks that the server, Alice and Bob all point to the same commit, with uniq -c counting identical lines.

Bash Stage 4: a rejected push

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The end of the output shows 3 9636782: three repositories, one commit. The graph shows Bob's .gitignore commit sitting on top of Alice's README commit, with no extra merge commit. Never use git push --force to "fix" a rejected push on a shared branch: it deletes the other person's work from the server. If you must force, use --force-with-lease, which refuses when the remote changed since you last fetched (Push and Pull).

What the real thing adds

A hosting service gives you the same Git operations plus a web interface and rules around them:

  • Branch protection: require a pull request and approvals before anything reaches main, so the stage 4 situation cannot happen on main at all.
  • Automated checks: a CI job runs the tests on every pull request (see GitHub Actions).
  • Merge strategies: squash merge (one commit per pull request), rebase merge (linear history) or merge commit (what we did).
  • Review tools: inline comments on lines of a diff, suggested changes, required reviewers.

Your local commands stay the same, which is the point of this simulation: when something odd happens in a real pull request, you can reproduce it with two clones and a bare repository.

Try variations

  1. In stage 3, resolve the conflict by rebasing Alice's branch onto origin/main (git rebase origin/main) instead of merging, and compare the graph. After a rebase her push needs --force-with-lease.
  2. Add a third developer, Carol, who clones after stage 2 and starts a branch from the new main.
  3. Make Bob's branch contain a typo, have Alice ask for a fix, and clean the branch with a fixup and autosquash before the merge.
  4. Add a pre-push hook to Bob's clone that refuses pushes to main (Hooks).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is a bare repository used as the shared remote?

    A bare repository holds only the Git database. Pushes update its refs and objects without touching any working files, which is why servers use bare repositories.
  2. 2. What does git diff main...origin/feature/schedule (three dots) show?

    Three dots compare the branch tip with the merge base, so only the branch's own work appears even when main has moved on.
  3. 3. During the conflict, which text is between <<<<<<< HEAD and =======?

    HEAD is where you are. The part above ======= is your side, and the part below it, up to >>>>>>>, is the incoming side.
  4. 4. Bob's push was rejected with (fetch first). What is the safe next step?

    The server has commits Bob does not have. Integrating them first keeps everyone's work; forcing would erase Alice's commit from the server.
  5. 5. Which Git command did we use to play the merge button of a pull request?

    A pull request is not a Git object. The merge button creates a merge commit on the target branch, which we reproduce with git merge --no-ff and a push.

Summary

  • A bare repository plus two clones is a complete team simulation that needs no network and no account.
  • Branches are shared by pushing them; git fetch tells you what others did; nothing changes in your working tree until you merge, rebase or switch.
  • Review is git log main..branch (which commits) and git diff main...branch (what changed).
  • A "pull request merge" is a merge commit on main; a branch that is out of date is brought up to date and its conflicts are resolved in the branch.
  • A rejected push means the remote has commits you lack: integrate with git pull or git pull --rebase, then push; avoid --force.

Next: Project: Release Automation Script