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
- Open
numbers.txtin VS Code - Move one number to its correct position
- 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
- Go to Azure DevOps in your browser
- Navigate to Repos → Pull Requests 3. Click New Pull Request
- Set:
- Source branch:
<branch-name> - Target branch:
main
- Source branch:
- Add a title describing your change
- Click Create
Step 6: Review and Merge
- Review the changes shown in the PR (Person B)
- If everything looks good, click Complete
- Select Complete merge
- 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
- Create a new branch for your next change
- Make changes, commit, push
- Create another PR
- Continue until all numbers are sorted
Step 9: Create a merge conflict
-
Both people should create a branch with changes to
feature-1andfeature-2, you task is to change the position of number 5. Where you place it is up to you. -
Now both people should push their respective branch
git push <the-branch>- Person A pushes their branch
git push feature-1 - Person B pushes their branch
git push feature-2
- Person A pushes their branch
-
Now merge
feature-1branch first, going throught the Pull Request flow (see Step 5). -
Then merge
feature-2branch second, and notice you'll get a MERGE CONFLICT. -
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 module03-branching-and-merging
, merging the feature-branch into themainbranch, we merge themainbranch into the feature branch.Doing so ensures that the
mainbranch maintains the integrity it's supposed to have and the conflicts are solved on the feature-branch side before being merged into themainbranch. The idea here being that, we ONLY modify the main branch through merges facilitated (and reviewed) by Pull Requests. We want themainbranch 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 by opening in VSCode and choosing the changes you want. # How you solve it is up to you. # 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 -
Now the owner of
feature-2can 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 <branch-name> |
Create and switch to new branch |
git switch <branch-name> |
Switch to branch |
git push |
Push branch to Azure DevOps |
git pull |
Get latest changes from remote |
Cheatsheet
Solving merge conflicts
git switch main
git pull
git switch <branch-name>
git merge main
# ... Solve the conflicts
git push
"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