Files
git-workshop/01-essentials/08-multiplayer/03_README.md

4.7 KiB

Multiplayer Git

Work with others using branches and pull requests.

Goal

Learn to collaborate on a shared repository using:

  • Branches - work independently without breaking main
  • Pull Requests - review and merge changes safely

The Workflow

1. Create branch    →    2. Make changes    →    3. Push branch
6. Delete branch    ←    5. Merge PR        ←    4. Create PR

This is how professional teams work together on code.


Step 1: Clone the Repository

Get the repository URL from your facilitator, then:

git clone <repository-url>
code <repository-name>

Step 2: Create a Branch

Never work directly on main. Create your own branch:

git switch -c <branch>

This creates a new branch and switches to it.


Step 3: Make Changes

  1. Open numbers.txt in VS Code
  2. Move one number to its correct position
  3. Save the file (Ctrl+S)

Step 4: Commit and Push

git add .
git commit -m "fix: move 7 to correct position"
git push <branch-name>

Your branch is now on Azure DevOps.


Step 5: Create a Pull Request

Detailed guide

  1. Go to Azure DevOps in your browser
  2. Navigate to ReposPull Requests 3. Click New Pull Request
  3. Set:
    • Source branch: <branch-name>
    • Target branch: main
  4. Add a title describing your change
  5. Click Create

Step 6: Review and Merge

  1. Review the changes shown in the PR (Person B)
  2. If everything looks good, click Complete
  3. Select Complete merge
  4. Your changes are now in main

Step 7: Update Your Local Main

After merging, update your local copy:

git switch main
git pull

Step 8: Repeat

  1. Create a new branch for your next change
  2. Make changes, commit, push
  3. Create another PR
  4. Continue until all numbers are sorted

Step 9: Create a merge conflict

  1. Both people should create a branch with changes to feature-1 and feature-2, you task is to change the position of number 5. Where you place it is up to you.

  2. Now both people should push their respective branch git push <the-branch>

    1. Person A pushes their branch git push feature-1
    2. Person B pushes their branch git push feature-2
  3. Now merge feature-1 branch first, going throught the Pull Request flow (see Step 5).

  4. Then merge feature-2 branch second, and notice you'll get a MERGE CONFLICT.

  5. The owner of feature-2 (Person B) is now tasked with resolving the conflict.

    In order to solve merge conflicts through a Shared Server (Azure DevOps) you have to do merges in reverse, meaning: Instead of, like in module 03-branching-and-merging, merging the feature-branch into the main branch, we merge the main branch into the feature branch.

    Doing so ensures that the main branch maintains the integrity it's supposed to have and the conflicts are solved on the feature-branch side before being merged into the main branch. The idea here being that, we ONLY modify the main branch through merges facilitated (and reviewed) by Pull Requests. We want the main branch to be as stable as possible.

    # First get the latest changes on main
    git switch main
    git pull
    
    # Then go back to the branch you can from
    git switch feature-2
    
    # Now we resolve the merge. We're merging the main branch INTO the feature-2 branch.
    git merge main
    # Resolve the merge conflict in numbers.txt
    # Once resolved
    git add numbers.txt
    git commit
    # VSCode will open up with a default message of "Merge main into feature-2"
    # finish the commit. And push the changes
    git push
    
  6. Now the owner of feature-2 can checkout the pull request on azure again and see that the merge conflict has been resolved and can therefore "Complete" the merge request, using the button in the top right corner with the name "Complete"

Quick Reference

Command What It Does
git switch -c <name> Create and switch to new branch
git push -u origin <branch> Push branch to Azure DevOps
git switch main Switch to main branch
git pull Get latest changes from remote

Common Issues

"My PR has conflicts"

  1. Update your branch with latest main:
    git switch main
    git pull
    git switch <branch-name>
    git merge main
    
  2. Resolve conflicts in VS Code
  3. Commit and push again

"I need to make more changes to my PR"

Just commit and push to the same branch - the PR updates automatically:

git add .
git commit -m "fix: address review feedback"
git push