class: center, middle, inverse, title-slide .title[ # Git and Github Workshop ] .subtitle[ ## Part Two ] .date[ ### November 7, 2022 ] --- # Topics .bg-near-white.b--gray.ba.bw2.br3.shadow-5.ph4[ Merge conflicts ] .bg-near-white.b--gray.ba.bw2.br3.shadow-5.ph4.mt2[ Branching ] .bg-near-white.b--gray.ba.bw2.br3.shadow-5.ph4.mt2[ Pull requests ] .bg-near-white.b--gray.ba.bw2.br3.shadow-5.ph4.mt2[ Forking ] -- .bg-near-green.b--green.ba.bw2.br3.shadow-5.ph4.mt2[ Github Desktop (local 💻) & Github (web/remote ☁️) ] --- class: inverse, center, middle # Merge Conflicts --- ## Merge Conflicts <img src = "https://upload.wikimedia.org/wikipedia/commons/9/97/Paragraph-based_prototype_%E2%80%93_rough_visualization_of_the_functionality.png" /> Image by [Johanna Strodt (Wikipedia: Edit Conflict)](https://commons.wikimedia.org/wiki/File:Paragraph-based_prototype_%E2%80%93_rough_visualization_of_the_functionality.png) --- ## Merge Conflicts When do you encounter merge conflicts? -- **.green[When things diverge and two people have changed the same lines in a file.]** -- .pull-left.center[ 😎 Developer A ] .pull-right.center[ 🤓 Developer B ] -- .pull-left.center[ - Scripting (lines 1-10) 💻 ] .pull-right.center[ - Scripting (lines 1-10) 💻 ] -- .pull-left.center[ - Scripting 💻 / Coffee break ☕ ] .pull-right.center[ - Commits & pushes to Github ⬆️ ] -- .pull-left.center[ - Commits & tries to push to Github ⬆️ (Forgot to pull and/or pulls after the fact) ] .pull-right.center[ ] -- Committing/Pushing when not in-sync with the latest commits on Github creates the divergence. -- .pull-left.center[ - receives ⚠️. Need to resolve conflict. ] .pull-right.center[ ] --- class: inverse, center, middle # How to Resolve a Conflict --- ## Resolving Conflicts Github embeds **.green[conflict dividers]** in the file. The `=======` line is the "center" of the conflict. .mt5.center[ ![](images/mc-resolve.png) ] Keep the version you want (or create something new), but remember to remove the conflict divider and chevrons/labels before committing and pushing. --- ## Minimizing Conflicts <img src="images/mc-tweet.png" width="400px" style="display: block; margin: auto;" /> --- ## Minimizing Conflicts .panelset.sideways[ .panel[.panel-name[Github Desktop] Notice the message indicating when there's new commits to the repo that require pulling - Before working on your project, pull! - Click on the grey `Fetch origin`/`Pull origin` button or - Click the blue button in the middle of the screen ] .panel[.panel-name[Command Line] If you're ready to add, commit, and push, but realized someone else has just pushed to the repo... 1. `git stash save` (removes your changes and sets them aside) 2. `git pull origin branch-name` 3. `git stash pop` (throw your changes back into the code) 4. `git add .` 5. `git commit -m 'your log message here'` 6. `git push origin branch-name` ] ] --- class: inverse, center, middle # Branching --- ## Branches **.green[A version of the repo (in the repo)]** that allows concurrent work on different parts of a project .f6[Image and other related images by [Jeff Olson](https://medium.com/upperlinecode/teaching-git-github-to-teenagers-branching-and-merging-6416a365458a)] .flex.justify-around[ .div[ ![Tree with squirrel and bike](images/repo-branch-04.png) ] .div[ ![House with path](images/repo-branch-05.png) ] ] --- ## Benefits of Branches - > Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository. - Default branch: `main` (formerly k.a. `master`), can merge content of a branch with `main` or another branch within the repo - Can have multiple branches - Branches can be deleted <center> <img src = "https://www.nobledesktop.com/image/gitresources/git-branches-merge.png" width="50%"/> </center> --- ## Branches .panelset.sideways[ .panel[.panel-name[Github Desktop] - Click on `Branch` > `New Branch` .bg-near-green.b--green.ba.bw2.br3.shadow-5.ph4.mt2[ The new branch will be a copy of the branch that you were currently on ] ] .panel[.panel-name[Command Line] - `git checkout -b name-of-new-branch` - Creates a new branch and moves you onto the branch. - `git branch name-of-new-branch` - Creates a new branch only. - `git branch` - Lists all branches in the repo. `*` indicates which branch you're on. - `git checkout name-of-existing-branch` - Moves you onto branch. ] .panel[.panel-name[Tips] - Make sure you're on the right branch before starting your work - If files are open and you've switched branches, double check the file has switched (or close and open the file to be safe) ] ] --- class: inverse, center, middle # Pull Requests --- ## Pull Requests - Notifying collaborators that you want to pull code into another branch - Creating a PR doesn't merge, just notifies and analyzes for conflicts ![](images/pr-open.png) --- ![](images/pr-created.png) --- class: inverse, center, middle # Forking --- ## Fork .panelset.sideways[ .panel[.panel-name[About] > A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project. - When the project diverges from the original intent - A stepping-stone for your new project - [Metropolitan Council (MN) forks PSRC's Travel Survey App](https://github.com/Metropolitan-Council/travel-study-stories) ![Met Council Github page](images/fork-02.png) ] .panel[.panel-name[Clone or Fork?] Both copy a repository, but the purpose is different .pull-left[ ### Clone - Creates a linked copy - Contribute by pushing and pulling ] .pull-right[ ### Fork - Creates an independent copy - Contribute via PR ] ] ] --- ## Undo-ing .panelset.sideways[ .panel[.panel-name[Github Desktop] - After committing, click `Undo` ] .panel[.panel-name[Command Line] - `Git reset HEAD dummy_file_01.R` (unstaging a file before a commit) - `git checkout 36b761 dummy_file_01.R` (reverting a file to previous commit) - `git checkout 36b761` (revert the whole repo to previous commit) ] ]