Skip to content

Commit e3cd0be

Browse files
Adds in the pre-commit and rebase episode updates (#3)
* Adds instructions for instructors on how to set up the git-training-demo repo * Adds in the optional episode 09 on pre-commit hooks.
1 parent 7f1847f commit e3cd0be

File tree

6 files changed

+325
-67
lines changed

6 files changed

+325
-67
lines changed

config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ episodes:
6969
- 07-history.md
7070
- 08-rebase.md
7171
- End.md
72-
72+
- 09-pre-commit.md
7373

7474
# Information for Learners
7575
learners:

episodes/08-rebase.md

Lines changed: 133 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
---
22
title: Rebasing
33
teaching: 15
4-
exercises: 0
4+
exercises: 10
55
---
66

77
::::::::::::::::::::::::::::::::::::::: objectives
88

9-
- Rebase a feature branch.
9+
- Rebase a feature branch to clean up its history.
10+
- Use rebase to update a feature branch to the `HEAD` of `main`.
1011

1112
::::::::::::::::::::::::::::::::::::::::::::::::::
1213

@@ -56,7 +57,7 @@ $ nano plot_lfric.py
5657
$ cat plot_lfric.py
5758
```
5859

59-
```output
60+
```python
6061
# pretnd there is code here!
6162
```
6263

@@ -78,7 +79,7 @@ $ nano plot_lfric.py
7879
$ cat plot_lfric.py
7980
```
8081

81-
```output
82+
```python
8283
# pretnd there is code here!
8384
# more Python
8485
```
@@ -99,7 +100,7 @@ $ nano plot_lfric.py
99100
$ cat plot_lfric.py
100101
```
101102

102-
```output
103+
```python
103104
# pretend there is code here!
104105
# more Python
105106
```
@@ -170,7 +171,7 @@ three feature branch commits which is `ed14d18`
170171
from the log above:
171172

172173
```bash
173-
git rebase -i ed14d18
174+
$ git rebase -i ed14d18
174175
```
175176

176177
```output
@@ -230,61 +231,64 @@ $ git rebase --abort
230231

231232
:::
232233

233-
We are going to move our spelling fix up one
234-
line and use the `fixup` keyword to merge
235-
those changes with the previous commit:
234+
We are going to drop our spelling fix and
235+
amend our first commit to fix the spelling:
236236

237237
```output
238-
pick 64ac261 Adds in a Python script to plot LFRic data
239-
fixup 0bb7871 Fixes the axis labels spelling in the LFRic data plotting script
238+
edit 64ac261 Adds in a Python script to plot LFRic data
240239
pick 7053de7 Extends the LFRic data plotting script to plot 2D fields
240+
drop 0bb7871 Fixes the axis labels spelling in the LFRic data plotting script
241241
```
242242

243-
Save the and close the file.
243+
Save and close the file.
244244

245245
```output
246-
Auto-merging plot_lfric.py
247-
CONFLICT (content): Merge conflict in plot_lfric.py
248-
error: could not apply 0bb7871... Fixes the axis labels spelling in the LFRic data plotting script
249-
hint: Resolve all conflicts manually, mark them as resolved with
250-
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
251-
hint: You can instead skip this commit: run "git rebase --skip".
252-
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
253-
Could not apply 0bb7871... Fixes the axis labels spelling in the LFRic data plotting script
246+
Stopped at 64ac261... Adds in a Python script to plot LFRic data
247+
You can amend the commit now, with
248+
249+
git commit --amend
250+
251+
Once you are satisfied with your changes, run
252+
253+
git rebase --continue
254+
[git-training-demo]:(add_plot_script|REBASE 1/3)$
254255
```
255256

256-
The first commit adding the file and the spelling
257-
fix commit inevitable conflicted with each other
258-
since the file in the spelling fix commit also has
259-
the changes from the original second commit.
257+
The rebase has stopped at 64ac261, the commit
258+
which added in the `plot_lfric.py` file,
259+
because we asked to amend the commit.
260+
We can now open up the plotting script and
261+
make our changes.
260262

261263
Opening up the file now shows:
262264

263265
```bash
264-
[git-training-demo]:(add_plot_script *+|REBASE 2/3)$ nano plot_lfric.py
266+
[git-training-demo]:(add_plot_script *+|REBASE 1/3)$ nano plot_lfric.py
267+
[git-training-demo]:(add_plot_script *+|REBASE 1/3)$ cat plot_lfric.py
265268
```
266269

267-
```
268-
<<<<<<< HEAD
270+
```python
269271
# pretnd there is code here!
270-
=======
271-
# pretend there is code here!
272-
# more Python
273-
>>>>>>> 0bb7871
274272
```
275273

276274
Modify and save the file so that it reads:
277275

278-
```
276+
```python
279277
# pretend there is code here!
280278
```
281279

282-
This is the combination of our first and third commits.
283-
Add the file and continue rebasing:
280+
Add the file and confirm the amendment:
284281

285282
```bash
286-
[git-training-demo]:(add_plot_script *+|REBASE 2/3)$ git add plot_lfric.py
287-
[git-training-demo]:(add_plot_script +|REBASE 2/3)$ git rebase --continue
283+
[git-training-demo]:(add_plot_script *|REBASE 1/3)$ git add plot_lfric.py
284+
[git-training-demo]:(add_plot_script +|REBASE 1/3)$ git commit --amend
285+
```
286+
287+
```output
288+
[detached HEAD 76fd423] Adds in a Python script to plot LFRic data
289+
Date: Mon Nov 18 10:49:25 2024 +0000
290+
1 file changed, 1 insertion(+)
291+
create mode 100644 plot_lfric.py
288292
```
289293

290294
For the last few commands we have included the
@@ -293,15 +297,13 @@ provided by the `git-prompt.sh` script
293297
you set up in the [git-novice lesson](https://www.astropython.com/git-novice/index.html#git-autocomplete).
294298
You can see the prompt clearly states we are
295299
rebasing, `REBASE`, and that it is on
296-
line `2/3` of the file we edited earlier.
300+
line `1/3` of the rebase to-do file we edited earlier.
297301

298-
We now see the following output:
302+
Now we have amended the commit we can
303+
continue with the rebase:
299304

300-
```output
301-
[detached HEAD e56ed99] Adds in a Python script to plot LFRic data
302-
Date: Sat Nov 16 19:53:42 2024 +0000
303-
1 file changed, 1 insertion(+)
304-
create mode 100644 plot_lfric.py
305+
```bash
306+
[git-training-demo]:(add_plot_script|REBASE 1/3)$ git rebase --continue
305307
Auto-merging plot_lfric.py
306308
CONFLICT (content): Merge conflict in plot_lfric.py
307309
error: could not apply 7053de7... Extends the LFRic data plotting script to plot 2D fields
@@ -310,33 +312,47 @@ hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
310312
hint: You can instead skip this commit: run "git rebase --skip".
311313
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
312314
Could not apply 7053de7... Extends the LFRic data plotting script to plot 2D fields
315+
[git-training-demo]:(add_plot_script *+|REBASE 2/3)$
313316
```
314317

315-
The rebase has tried to apply the third commit
316-
which has resulted in another merge error.
317-
Open the file again to resolve the conflict:
318+
The rebase has tried to apply the second commit
319+
which still has the spelling mistake
320+
resulting in a merge error.
321+
Open the file again and resolve the conflict:
318322

319323
```bash
320-
[git-training-demo]:(add_plot_script *+|REBASE 3/3)$ nano plot_lfric.py
321-
[git-training-demo]:(add_plot_script *+|REBASE 3/3)$ cat plot_lfric.py
324+
[git-training-demo]:(add_plot_script *+|REBASE 2/3)$ nano plot_lfric.py
325+
[git-training-demo]:(add_plot_script *+|REBASE 2/3)$ cat plot_lfric.py
322326
```
323327

324-
```output
328+
```python
329+
<<<<<<< HEAD
330+
# pretend there is code here!
331+
=======
332+
# pretnd there is code here!
333+
# more Python
334+
>>>>>>> 3343f91
335+
```
336+
337+
Modify and save the file so that it reads:
338+
339+
```python
325340
# pretend there is code here!
326341
# more Python
327342
```
328343

329344
Add the file and continue with the rebase:
330345

331346
```bash
332-
[git-training-demo]:(add_plot_script *+|REBASE 3/3)$ git add plot_lfric.py
333-
[git-training-demo]:(add_plot_script +|REBASE 3/3)$ git rebase --continue
347+
[git-training-demo]:(add_plot_script *+|REBASE 2/3)$ git add plot_lfric.py
348+
[git-training-demo]:(add_plot_script +|REBASE 2/3)$ git rebase --continue
334349
```
335350

336351
```output
337352
[detached HEAD d7def6a] Extends the LFRic data plotting script to plot 2D fields
338353
1 file changed, 1 insertion(+)
339354
Successfully rebased and updated refs/heads/add_plot_script.
355+
[git-training-demo]:(add_plot_script)$
340356
```
341357

342358
We have now successfully rebased our feature branch.
@@ -374,6 +390,21 @@ output above, on the feature branch.
374390

375391
## Updating a Branch
376392

393+
::::::::::::::::::::::::::::::::::::: instructor
394+
395+
To ensure the `main` branch of the
396+
`git-training-demo` repository has
397+
moved forward one commit, the co-instructor
398+
should merge a PR making any change
399+
they like to the repository.
400+
401+
Or they can make a direct edit and merge
402+
to main by for example editing the `README.md`
403+
file and using their branch protection
404+
override.
405+
406+
::::::::::::::::::::::::::::::::::::::::::::::::
407+
377408
You can also use rebase to move your changes to
378409
branch off the `HEAD` of `main`.
379410
While we rebased our feature branch another
@@ -407,7 +438,15 @@ gitGraph
407438
commit id: 'd7def6a'
408439
```
409440

410-
Simply run:
441+
Go back to your fork on GitHub
442+
and click on the [**Sync fork**](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork) button.
443+
Then run `git pull` to fetch the changes:
444+
445+
```bash
446+
$ git pull origin main
447+
```
448+
449+
To rebase we run:
411450

412451
```bash
413452
$ git rebase main
@@ -475,35 +514,63 @@ to check yourself.
475514

476515
::::::::::::::::::::::::::::::::::::::: challenge
477516

478-
## Modifying Commits and their Messages
517+
## Modifying Commit Messages
479518

480-
`git rebase` can also be used to modify individual commits
481-
and their messages.
482-
So instead of creating a new commit to fix a typo you
483-
could use rebase to edit the commit directly.
519+
`git rebase` can also be used to modify commit messages.
484520

485521
Look again at the output of the interactive rebase
486-
in this lesson. Which keywords let you:
487-
488-
1. Edit the contents of a commit
489-
2. Edit the commit message
522+
in this lesson.
523+
Which keyword lets you edit the commit message?
490524

491525
::::::::::::::: solution
492526

493527
## Solution
494528

495-
1. Edit the contents of a commit
529+
Edit the commit message:
496530

531+
```text
532+
# r, reword <commit> = use commit, but edit the commit message
497533
```
498-
# e, edit <commit> = use commit, but stop for amending
499-
```
500534

501-
2. Edit the commit message
535+
:::::::::::::::::::::::::
536+
537+
::::::::::::::::::::::::::::::::::::::::::::::::::
538+
539+
::::::::::::::::::::::::::::::::::::::: challenge
540+
541+
## Re-ordering Commits
502542

543+
In the first example removing the spelling mistake
544+
we used the following keywords in our rebase to-do list:
545+
546+
```output
547+
edit 64ac261 Adds in a Python script to plot LFRic data
548+
pick 7053de7 Extends the LFRic data plotting script to plot 2D fields
549+
drop 0bb7871 Fixes the axis labels spelling in the LFRic data plotting script
503550
```
504-
# r, reword <commit> = use commit, but edit the commit message
551+
552+
You can also re-order the lines in this file
553+
to re-order commits.
554+
Look at the possible rebase options again.
555+
What other combination of keywords and/or
556+
line-reordering would have given us the same result?
557+
558+
::::::::::::::: solution
559+
560+
## Solution
561+
562+
```output
563+
pick 64ac261 Adds in a Python script to plot LFRic data
564+
fixup 0bb7871 Fixes the axis labels spelling in the LFRic data plotting script
565+
pick 7053de7 Extends the LFRic data plotting script to plot 2D fields
505566
```
506567

568+
This is one possibility.
569+
We move the last commit just underneath the first
570+
which introduced the mistake.
571+
We use the `fixup` keyword to fix this change up
572+
into the previous commit.
573+
507574
:::::::::::::::::::::::::
508575

509576
::::::::::::::::::::::::::::::::::::::::::::::::::

0 commit comments

Comments
 (0)