|
| 1 | +--- |
| 2 | +title: "Branching" |
| 3 | +teaching: 10 |
| 4 | +exercises: 10 |
| 5 | +questions: |
| 6 | +- "What is a branch?" |
| 7 | +- "How can I merge changes from another branch?" |
| 8 | +objectives: |
| 9 | +- "Know what branches are and why you would use them" |
| 10 | +- "Understand how to merge branches" |
| 11 | +- "Understand how to resolve conflicts during a merge" |
| 12 | +keypoints: |
| 13 | +- "`git branch` creates a new branch" |
| 14 | +- "Use feature branches for new ideas and fixes, before merging into `master`" |
| 15 | +- merging does not delete any branches |
| 16 | +--- |
| 17 | + |
| 18 | +### What is a branch? |
| 19 | + |
| 20 | +You might have noticed the term *branch* in status messages: |
| 21 | + |
| 22 | +~~~ |
| 23 | +$ git status |
| 24 | +~~~ |
| 25 | +{: .language-bash} |
| 26 | +~~~ |
| 27 | +On branch master |
| 28 | +nothing to commit (working directory clean) |
| 29 | +~~~ |
| 30 | +{: .output} |
| 31 | + |
| 32 | +and when we wanted to get back to our most recent version of the repository, we |
| 33 | +used `git checkout master`. |
| 34 | + |
| 35 | +Not only can our repository store the changes made to files and directories, it |
| 36 | +can store multiple sets of these, which we can use and edit and update in |
| 37 | +parallel. Each of these sets, or parallel instances, is termed a `branch` and |
| 38 | +`master` is Git's default branch. |
| 39 | + |
| 40 | +A new branch can be created from any commit. Branches can also be *merged* |
| 41 | +together. |
| 42 | + |
| 43 | +### Why are branches useful? |
| 44 | +Suppose we've developed some software and now we want to |
| 45 | +try out some new ideas but we're not sure yet whether we'll keep them. We |
| 46 | +can then create a branch 'feature1' and keep our `master` branch clean. When |
| 47 | +we're done developing the feature and we are sure that we want to include it |
| 48 | +in our program, we can merge the feature branch with the `master` branch. |
| 49 | +This keeps all the work-in-progress separate from the `master` branch, which |
| 50 | +contains tested, working code. |
| 51 | + |
| 52 | +When we merge our feature branch with master git creates a new commit which |
| 53 | +contains merged files from master and feature1. After the merge we can continue |
| 54 | +developing. **The merged branch is not deleted.** We can continue developing (and |
| 55 | +making commits) in feature1 as well. |
| 56 | + |
| 57 | + |
| 58 | +### Branching in practice |
| 59 | + |
| 60 | +One of our colleagues wants to contribute to the paper but is not quite sure |
| 61 | +if it will actually make a publication. So it will be safer to create a branch |
| 62 | +and carry on working on this "experimental" version of the paper in a branch |
| 63 | +rather than in the master. |
| 64 | + |
| 65 | +~~~ |
| 66 | +$ git checkout -b paperWJohn |
| 67 | +~~~ |
| 68 | +{: .language-bash} |
| 69 | +~~~ |
| 70 | +Switched to a new branch 'paperWJohn' |
| 71 | +~~~ |
| 72 | +{: .output} |
| 73 | + |
| 74 | +We're going to change the title of the paper and update the author list (adding John Smith). |
| 75 | +However, before we get started it's a good practice to check that we're working |
| 76 | +on the right branch. |
| 77 | + |
| 78 | +~~~ |
| 79 | +$ git branch # Double check which branch we are working on |
| 80 | +~~~ |
| 81 | +{: .language-bash} |
| 82 | +~~~ |
| 83 | + master |
| 84 | +* paperWJohn |
| 85 | +~~~ |
| 86 | +{: .output} |
| 87 | + |
| 88 | +The * indicates which branch we're currently in. Now let's make the changes to the paper. |
| 89 | + |
| 90 | +~~~ |
| 91 | +$ vim journal.md # Change title and add co-author |
| 92 | +$ git add journal.md |
| 93 | +$ git commit # "Modify title and add John as co-author" |
| 94 | +~~~ |
| 95 | +{: .language-bash} |
| 96 | + |
| 97 | +If we now want to work in our `master` branch. We can switch back by using: |
| 98 | + |
| 99 | +~~~ |
| 100 | +$ git checkout master |
| 101 | +~~~ |
| 102 | +{: .language-bash} |
| 103 | +~~~ |
| 104 | +Switched to branch 'master' |
| 105 | +~~~ |
| 106 | +{: .output} |
0 commit comments