-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2.3-branches_merging.qmd
248 lines (165 loc) · 7.76 KB
/
2.3-branches_merging.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
---
title: "Intro to `git`"
subtitle: "Block 2.3: Branches & Merging"
---
# Alternate Realities: <br> `git branches` {background-color="black" background-opacity="0.7" background-image="images/backgrounds/suganth-V1YEEItoOTE-unsplash.jpg"}
::: attribution
Background: https://unsplash.com/photos/V1YEEItoOTE
:::
## What are git branches? 🌳
- Branches allow you to have different versions of your code within the same repository next to each other
- You can switch back and forth between branches
- In the end you can merge your changes back into your `main` branch
:::{.callout-note}
The default branch used to be called `master`, this was more-or-less recently changed to `main`, but not all repositories have changed yet.
:::
## What are git branches? 🌲 {background-color="#5cbcfc"}
![Example of a repository history with branching](images/screenshots/learngitbranching/branched.png)
## Creating new branches
- There are two ways to create new branches
- `git checkout -b my-branch` ✅
- Create a new branch and immediately check it out
- `git branch my-branch` ❌
- Create a new branch, but **don't** check it out
```bash
git checkout -b my-branch
# Switched to a new branch 'my-branch'
```
## Working on branches
- So far you have always been working on a single branch: `main`
- Nothing changes when working with multiple branches, you just have to keep an eye on your current branch
```bash
git status
# On branch my-branch
# nothing to commit, working tree clean
```
## Switching branches
You can switch branches with `git checkout` (as we've already done earlier)
```bash
git checkout my-branch
```
:::{.callout-tip}
There's no more `-b` when the branch already exists
:::
:::{.callout-note}
You can also use `git switch my-branch`, but this command is still experimental right now and may change.
:::
## Bringig it all together: Merging
- You can combine two different branches by merging them with each other
- `git merge my-branch`
- You will merge the branch that you name in the command *into* the one you are currently on
- In most cases you will want to be on the `main` branch when using `git merge` (but not always!)
- Merging will create a new "merge" commit
## Bringig it all together: Merging
:::{.callout-note}
Only branches with a shared history can be merged, but this should (almost) always be the case.
:::
## Visual Example of Merging {background-color="#5cbcfc"}
![Example of a merged repository history](images/screenshots/learngitbranching/merged.png)
# Demo: [learngitbranching.js.org](https://learngitbranching.js.org/?NODEMO?) {background-color="black" background-opacity="0.7" background-image="images/backgrounds/berlin-demo-govid-loAFGkJxYzgxu3NKnZ.gif"}
::: attribution
Background: https://giphy.com/gifs/berlin-demo-govid-loAFGkJxYzgxu3NKnZ
:::
## Practical: Branches and merging {background-color="black"}
1. Go back to your Terminal in `git-exercise`
2. Create a new branch called `new-feature`
- Make sure to also checkout that branch, you can check whether you're on it with `git status`
3. Add a commit on that new branch
4. Go back to `main`
6. Merge the `new-feature` branch into `main`
7. Repeat all the above steps (with a different branch name & commit) using a GUI
# The Ugly Side of `git`: Merge Conflicts {background-color="black" background-opacity="0.5" background-image="images/backgrounds/git-merge-cFkiFMDg3iFoI.gif"}
## What are Merge Conflicts?
- `git` is very smart in the way it combines changes from two branches
- **But** there's not always a clear solution
- If git doesn't know how to merge the two branches, we get a *merge conflict*
- Merge conflicts have to be manually resolved (by us)
## How to Get a Merge Conflict 🧨
And how to avoid it.
- Merge conflcits occur when there are edits to the same file (and at the same location) on two different branches
- If you merge your branches / edits before making more changes, you can avoid conflicts
## Merge Conflicts: Illustration (1)
![Starting situation, each box corresponds to the file's contents in a commit](images/drawings/merge-conflict-start.svg)
## Merge Conflicts: Illustration (2)
![The merge conflict arises because both change the same line](images/drawings/merge-conflict-end.svg)
## Merge Conflicts: Illustration (3)
![We could've avoided the merge conflict, by merging main into feature before doing the change on feature](images/drawings/merge-conflict-avoid.svg)
## Resolving Merge Conflicts
- To resolve a merge conflict, we will have to pick one of the two versions
- Once a solution is picked for every conflict you can commit the solution and the merge continues / finishes
- Picking a solution is easiest to do by using GUI tools
- Both Sourcetree and VSCode have great UIs for this
# DEMO: Resolving merge conflicts in VS Code and SourceTree
# *Practical:* Creating & Resolving a Merge Conflict {background-color="black"}
## Practical: Setup {background-color="black"}
1. Create a new file called `guessing_conflict.txt`
2. Write the "Question" with the letters A to D into the file, with empty lines in between e.g.
```text
Question A
Question B
Question C
Question D
```
## Practical: Setup {background-color="black"}
3. Create a new branch called `guesses` and check it out
- Double check whether you checked out the correct branch with `git status`
4. We're done with the preparations
## Practical: Guessing Game {background-color="black"}
Write your guess below the line with the matching letter.
a) How many liters of Beer have been drunken at the Oktoberfest in Munich in 2019? 🍻
b) How many people live in Karlsruhe? (Exactly) 🏘️
c) What percentage of Americans asked in a survey in 2017, indicated that they believe in Astrology? 🌟
d) What percentage (same sample as C), indicated that they believe in psychics? 🔮
## Practical: Guessing Game Answers {background-color="black"}
5. Check out the `main` branch again
6. Write down the actual answers below the letters in the now empty file again
```text
Question A 🍻 Liters of beer [^1]
7,850,200
Question B 🏘️ People in Karlsruhe
313,092
Question C 🌟 % believing in Astrology [^2]
29%
Question D 🔮 % believing in Psychics [^2]
41%
```
[^1]: https://opendata.muenchen.de/tr/dataset/oktoberfest
[^2]: https://www.pewresearch.org/fact-tank/2018/10/01/new-age-beliefs-common-among-both-religious-and-nonreligious-americans/
https://opendata.muenchen.de/tr/dataset/oktoberfest
## Practical: The Actual Conflcit {background-color="black"}
7. Use `git merge` to merge the `guesses` branch into your `main` branch
8. Explore the files in the resulting merge conflict
9. Resolve the merge conflict
## 🎉 Bonus Practical: *learngitbranching* {background-color="black"}
1. Go to https://learngitbranching.js.org/
2. Finish Lessons 1, 2 and 3 on the website
## 🎉 Bonus: Rebasing - The Alternative to Merging
- You can also combine two branches by rebasing them (instead of merging)
- When rebasing, all extra commits on your current branch will be moved to the target branch
- This alters the affected commits ‼️
- Leads to a single history
- Use with caution
```bash
# You call git rebase e.g. on the feature branch
git checkout feature
```
::: attribution
More Info: <https://www.atlassian.com/git/tutorials/merging-vs-rebasing>
:::
## 🎉 Rebasing: The Alternative to Merging {background-color="#5cbcfc"}
:::: {.columns}
::: {.column .fragment width="50%"}
Merging
![](images/screenshots/learngitbranching/merged.png){width=65%}
:::
::: {.column .fragment width="50%"}
Rebasing
![](images/screenshots/learngitbranching/rebased.png){width=65%}
:::
::::
## *End of Block* 🎉 {background-color="black" background-opacity="0.4" background-image="images/backgrounds/dolomites.jpg"}
:::{.r-fit-text}
Any Questions?
:::
[[🏡 Back to Overview]](./index.html)
[[⏩️ Next Block]](./3.1-what_is_github.html)