Skip to content

Commit a7a75b7

Browse files
committed
content: cleaned up branches and merging
1 parent db7c3e7 commit a7a75b7

File tree

3 files changed

+117
-66
lines changed

3 files changed

+117
-66
lines changed

src/content/lessons/advanced-git-features.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ resources: {
99

1010
<!-- TODO: tags -->
1111

12+
<!-- TODO: reflog -->
13+
14+
<!-- # selects the commit master was -->
15+
<!-- # on at the given time -->
16+
<!-- master@{5.days.ago} -->
1217
<!-- TODO: protocols -->

src/content/lessons/git-branches.md

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ tags: ['git']
66
resources: {
77
'Git Branching Documentation': 'https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell',
88
'Interactive Git Branching': 'https://learngitbranching.js.org/',
9+
'Referencing commits docs': 'https://git-scm.com/docs/gitrevisions',
910
}
1011
---
1112

@@ -78,6 +79,24 @@ git switch -c feature-user-profile
7879

7980
---
8081

82+
### Branch Naming Conventions
83+
84+
```bash
85+
# Good branch names
86+
feature/user-authentication
87+
bugfix/login-timeout
88+
hotfix/security-patch
89+
docs/api-documentation
90+
refactor/database-queries
91+
92+
# Poor branch names
93+
fix
94+
temp
95+
john-branch
96+
new-feature
97+
```
98+
---
99+
81100
## Switching Between Branches
82101

83102
```bash
@@ -155,39 +174,68 @@ Until now we have been looking at linear histories. But often branches will dive
155174
156175
---
157176

158-
## Addressing commits
177+
## Viewing divergent trees
159178

160-
There are many ways to get to a commit.
179+
`log` provides a couple of options for viewing commit ranges:
161180

162-
<!-- TODO: write this -->
181+
```bash
182+
# shows all commits on my_branch that
183+
# are NOT on master
184+
git log --oneline --graph main..my_branch
163185

186+
# shows all commits on both main and my_branch
187+
# but NOT the shared ancestry
188+
git log --oneline --graph main...my_branch
189+
```
164190

165191
---
166192

167-
## Detached HEAD
193+
![example-history](https://git-scm.com/book/en/v2/images/double-dot.png)
168194

169-
If HEAD points directly at a commit, git will inform you that
170-
you are in a **detached HEAD** state.
195+
```bash
196+
git log --oneline --graph master..experimental
197+
# shows C and D
198+
```
199+
200+
---
201+
202+
![example-history](https://git-scm.com/book/en/v2/images/double-dot.png)
171203

172-
<!-- TODO: write this -->
204+
```bash
205+
git log --oneline --graph master...experimental
206+
# shows E, F, C and D
207+
```
173208

174209
---
175210

176-
## Branch Naming Conventions
211+
## Referencing commits
212+
213+
There are many ways to get to a commit.
214+
![example-history](https://git-scm.com/book/en/v2/images/double-dot.png)
177215

178216
```bash
179-
# Good branch names
180-
feature/user-authentication
181-
bugfix/login-timeout
182-
hotfix/security-patch
183-
docs/api-documentation
184-
refactor/database-queries
217+
master^ # E
218+
master~2 # B
219+
B^ # A
220+
B~1 # A
221+
^B # everything except A and B
222+
```
185223

186-
# Poor branch names
187-
fix
188-
temp
189-
john-branch
190-
new-feature
224+
---
225+
226+
## Detached HEAD
227+
228+
If HEAD points directly at a commit, git will inform you that
229+
you are in a **detached HEAD** state.
230+
231+
```bash
232+
git checkout <commit>
233+
# prints:
234+
# Note: switching to '<commit>'.
235+
#
236+
# You are in 'detached HEAD' state.
237+
# You can look around, make experimental changes
238+
# ...
191239
```
192240

193241
<bonus-content>
@@ -367,7 +415,7 @@ git fetch --prune
367415
1. **Create** branch from up-to-date main
368416
2. **Work** on feature with regular commits
369417
3. **Test** thoroughly before merging
370-
4. **Create Pull Request** for code review
418+
4. **Request** code review
371419
5. **Merge** to main after approval
372420
6. **Delete** feature branch after merge
373421
7. **Update** local main branch

src/content/lessons/merging.md

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,25 @@ resources: {
1111

1212
Merging combines the work from different branches back together.
1313

14+
This creates a merge commit. Merge commits are special because they
15+
have more than 1 parent commit.
16+
17+
---
18+
1419
Before merge:
1520
![example-before-merge](https://git-scm.com/book/en/v2/images/basic-merging-1.png)
1621

22+
> Notice that master and iss53 have commits
23+
> exclusive to each side
24+
1725
---
1826

1927
After merge:
2028
![example-after-merge](https://git-scm.com/book/en/v2/images/basic-merging-2.png)
2129

30+
> Notice that commit C6 has 2 parent commits:
31+
> C5 and C4
32+
2233
---
2334

2435
## Listing merged / unmerged branches
@@ -35,19 +46,8 @@ git branch --no-merged
3546

3647
## Fast-Forward Merge
3748

38-
When the target branch hasn't changed since the feature branch was created:
39-
40-
```
41-
Before merge:
42-
main: A---B---C
43-
\
44-
feature: D---E
45-
46-
After merge:
47-
main: A---B---C---D---E
48-
```
49-
50-
---
49+
When the target branch hasn't changed since the feature branch was created.
50+
No merge commit is created. The history remains linear.
5151

5252
```bash
5353
# Switch to main branch
@@ -64,26 +64,12 @@ git merge main feature-branch
6464

6565
## Real Merge
6666

67-
When both branches have new commits:
68-
69-
```
70-
Before merge:
71-
main: A---B---C---F
72-
\
73-
feature: D---E
74-
75-
After merge:
76-
main: A---B---C---F---G
77-
\ /
78-
feature: D---E---/
79-
```
80-
81-
---
67+
When both branches have new commits a merge commit is created:
8268

8369
```bash
8470
git checkout main
8571
git merge feature-branch
86-
# Creates merge commit G
72+
# Creates a new merge commit
8773
```
8874

8975
<bonus-content>
@@ -118,8 +104,9 @@ git log --oneline --graph
118104

119105
Conflicts occur when the same lines in the same files are changed differently on two branches.
120106

121-
Git is usually smart enough to resolve trivial conflicts automatically,
122-
but when it is not clear what the outcome should be - you need to resolve manually.
107+
Git is usually smart enough to resolve trivial conflicts automatically.
108+
109+
But when it is not clear what the outcome should be - you need to resolve manually.
123110

124111
---
125112

@@ -141,9 +128,9 @@ git merge main feature-greeting
141128

142129
---
143130

144-
### Conflict Resolution
131+
## Conflict Resolution
145132

146-
When a conflict occurs, Git modifies the file to show both versions:
133+
When a conflict occurs, Git **modifies** the file to show both versions:
147134

148135
```
149136
<<<<<<< HEAD
@@ -153,13 +140,15 @@ Hello World from feature
153140
>>>>>>> feature-greeting
154141
```
155142

143+
Git will also prevent you from committing until the conflict is resolved.
144+
156145
---
157146

158-
To resolve:
147+
To resolve, edit the files so the correct version of the content is present. Then `add` and `commit`:
159148

160149
```bash
161150
# 1. Edit the file to choose the version you want
162-
echo "Hello World - combined version" > greeting.txt
151+
nvim # btw
163152

164153
# 2. Stage the resolved file
165154
git add greeting.txt
@@ -172,13 +161,17 @@ git commit -m "merge: resolve greeting conflict"
172161

173162
### Conflict Resolution Tools
174163

175-
```bash
176-
# Use visual merge tool
177-
git mergetool
164+
There are many tools that allow for easier view of conflicts.
165+
166+
Git can be configured to use a specific `mergetool`
178167

168+
```bash
179169
# Abort the merge and start over
180170
git merge --abort
181171

172+
# Use visual merge tool
173+
git mergetool
174+
182175
# Show conflicts in different format
183176
git diff --name-only --diff-filter=U
184177
```
@@ -187,7 +180,7 @@ git diff --name-only --diff-filter=U
187180

188181
## Rebasing
189182

190-
Rebasing rewrites history to create a cleaner, linear timeline:
183+
Rebasing **rewrites history** to create a cleaner, linear commit history:
191184

192185
```bash
193186
# Instead of merge, use rebase
@@ -198,8 +191,8 @@ git rebase main
198191
git rebase -i HEAD~3
199192
```
200193

201-
**When to use**: Cleaning up feature branch before merging
202-
**When NOT to use**: On shared/public branches
194+
- **Use when**: Cleaning up feature branch before merging
195+
- **DONT'T USE**: On shared/public branches
203196

204197
---
205198

@@ -233,17 +226,22 @@ git checkout original-branch
233226
git stash pop
234227
```
235228

236-
---
229+
<bonus-content>
237230

238-
## Abort / Continue options
231+
<pop-quiz data-answer-id="3">
239232

240-
<!-- TODO: write this -->
233+
### When does a merge cause a conflict?
241234

242-
<bonus-content>
235+
- two cars want to drive in the same lane
236+
- two repositories have the same name
237+
- two coworkers reach for the same soda can
238+
- two branches being merged have diverging content
239+
240+
</pop-quiz>
243241

244242
<home-work>
245243

246-
### Assignment 2: Remote Collaboration Simulation
244+
### Remote Collaboration Simulation
247245

248246
1. Create a repository on GitHub
249247
2. Clone it to two different directories (simulating two developers)

0 commit comments

Comments
 (0)