# 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: ```powershell git clone code ``` --- ## Step 2: Create a Branch Never work directly on `main`. Create your own branch: ```powershell git switch -c ``` 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 ```powershell git add . git commit -m "fix: move 7 to correct position" git push ``` Your branch is now on Azure DevOps. --- ## Step 5: Create a Pull Request [Detailed guide](https://learn.microsoft.com/en-us/azure/devops/repos/git/pull-requests?view=azure-devops&tabs=browser#create-a-pull-request) 1. Go to Azure DevOps in your browser 2. Navigate to **Repos** → **Pull Requests** 3. Click **New Pull Request** 4. Set: - **Source branch:** `` - **Target branch:** `main` 5. Add a title describing your change 6. 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: ```powershell 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 ` 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. ```pwsh # 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 ``` 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 ` | Create and switch to new branch | | `git switch ` | Switch to branch | | `git push` | Push branch to Azure DevOps | | `git pull` | Get latest changes from remote | --- ## Cheatsheet ### Solving merge conflicts ```pwsh git switch main git pull git switch 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: ```pwsh git add . git commit -m "fix: address review feedback" git push ```