From 267aaa2e425e6ae0dc37dbc2e0b49999fa0b8454 Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Tue, 5 Mar 2024 19:43:55 +0000 Subject: [PATCH] add content on create new branch (#25) --- episodes/09-conflict.md | 185 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) diff --git a/episodes/09-conflict.md b/episodes/09-conflict.md index 3b4aaab6d6..a9f615d447 100644 --- a/episodes/09-conflict.md +++ b/episodes/09-conflict.md @@ -40,6 +40,191 @@ different changes to each copy. Version control helps us manage these [conflicts](../learners/reference.md#conflict) by giving us tools to [resolve](../learners/reference.md#resolve) overlapping changes. +Before resolving conflicts, we'll review two features that a **good collaborator** puts in practice: Create branches and Atomic commits. + +## Create branches + +First, verify that you are working on the main branch: + +```bash +$ git status +``` + +```output +On branch main +``` + +The `git branch` command **creates** a branch of the name `newbranch`: + +```bash +$ git branch newbranch +``` + +The `git checkout` command **switch** from the current branch (in this case, `main`) to the `newbranch`: + +```bash +$ git checkout newbranch +``` + +```output +Switched to branch 'newbranch' +``` + +Now, you can confirm that we are in `newbranch` using `git status`: + +```bash +$ git status +``` + +```output +On branch newbranch +nothing to commit, working tree clean +``` + +Now, let's make an edit to the `sitrep.Rmd` file: + +```r +usethis::edit_file("sitrep.Rmd") +``` + +```output +Comparison of attack rates in different age groups +This can identify priority groups for interventions +Maps illustrate the spread and impact of outbreak +Use packages listed in the CRAN Task View: Epidemiology +``` + +Add and commit this change to the local repository: + +```bash +$ git add sitrep.Rmd +$ git commit -m "Add edit in new branch" +``` + +```output +[newbranch 60d5ff9] Add edit in new branch + 1 file changed, 1 insertion(+) +``` + +Push commit to the remote repository. NOTE: instead of `git push origin main`, we replace `main` with the name of the new branch: + +```bash +$ git push origin newbranch +``` + +```output +Enumerating objects: 5, done. +Counting objects: 100% (5/5), done. +Delta compression using up to 12 threads +Compressing objects: 100% (3/3), done. +Writing objects: 100% (3/3), 339 bytes | 169.00 KiB/s, done. +Total 3 (delta 2), reused 0 (delta 0), pack-reused 0 +remote: Resolving deltas: 100% (2/2), completed with 2 local objects. +remote: +remote: Create a pull request for 'newbranch' on GitHub by visiting: +remote: https://github.com/vlad/cases/pull/new/newbranch +remote: +To https://github.com/vlad/cases.git + * [new branch] newbranch -> newbranch +``` + +On GitHub, we get a notification to Create a **Pull Request**: + +![](fig/github-branch-pullrequest.png) + +In your local repository, you can leave your work parked for a while on that branch. To return to `main` (or to any other branch), you can use the same command to **switch** between branches: + +```bash +$ git checkout main +``` + +```output +Switched to branch 'main' +Your branch is up to date with 'origin/main'. +``` + +## Atomic commits + +A good commit is also an __atomic commit__, like in the commit of the last challenge: + +```bash +$ git add lab-test.jpg +$ git add lab-site.jpg +$ git commit -m "Use two images: test and site" +``` + +Commits should be ‘atomic’, meaning that they should do one simple thing and they should do it completely. ([The Turing Way Community](https://the-turing-way.netlify.app/reproducible-research/vcs/vcs-git-compare#good-practice)). Atomic commits prioritize one unit of change instead of the quantity of changes. + +![Two related modified lines must be part of the same commit.](fig/git-rstudio-27.png) + +In Rstudio, the Review changes window has a button called “Stage chunk”. This helps to make atomic commits, even if you change a lot of lines in a single edit. You can either select them to make isolated commits or to unite them to be part of the same commit. + +![Two unrelated edits must be part of two isolated commits.](fig/git-rstudio-35.png) + +::::::::::::::::: checklist + +### Good practice + +A good atomic commit: + +- Includes more than one file that involves one unit of change. + +- Isolate or includes multiple edited lines using the "Stage chunk" button in Rstudio. + +- Does not include all the files in one commit. + +::::::::::::::::::::::::::: + +## Group Challenge + +::::::::::::::::::::::::::::::::::::::: challenge + +## A Typical Work Session + +You sit down at your computer to work on a shared project that is tracked in a +remote Git repository. During your work session, you take the following +actions, but not in this order: + +- *Make changes* by appending the number `100` to a text file `numbers.md` +- *Update remote* repository to match the local repository +- *Celebrate* your success with some fancy beverage(s) +- *Update local* repository to match the remote repository +- *Stage changes* to be committed +- *Commit changes* to the local repository + +In what order should you perform these actions to minimize the chances of +conflicts? Put the commands above in order in the *action* column of the table +below. When you have the order right, see if you can write the corresponding +commands in the *command* column. A few steps are populated to get you +started. + +| order | action . . . . . . . . . . | command . . . . . . . . . . | +| ----- | -------------------------- | --------------------------------------------- | +| 1 | | | +| 2 | | `echo 100 >> numbers.md` | +| 3 | | | +| 4 | | | +| 5 | | | +| 6 | Celebrate! | `AFK` | + +::::::::::::::: solution + +## Solution + +| order | action . . . . . . | command . . . . . . . . . . . . . . . . . . . | +| ----- | -------------------------- | --------------------------------------------- | +| 1 | Update local | `git pull origin main` | +| 2 | Make changes | `echo 100 >> numbers.md` | +| 3 | Stage changes | `git add numbers.md` | +| 4 | Commit changes | `git commit -m "Add 100 to numbers.md"` | +| 5 | Update remote | `git push origin main` | +| 6 | Celebrate! | `AFK` | + +::::::::::::::::::::::::: + +:::::::::::::::::::::::::::::::::::::::::::::::::: + + ## Create a conflict To see how we can resolve conflicts, we must first create one. The file