1
1
---
2
2
title : Rebasing
3
3
teaching : 15
4
- exercises : 0
4
+ exercises : 10
5
5
---
6
6
7
7
::::::::::::::::::::::::::::::::::::::: objectives
8
8
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 ` .
10
11
11
12
::::::::::::::::::::::::::::::::::::::::::::::::::
12
13
@@ -56,7 +57,7 @@ $ nano plot_lfric.py
56
57
$ cat plot_lfric.py
57
58
```
58
59
59
- ``` output
60
+ ``` python
60
61
# pretnd there is code here!
61
62
```
62
63
@@ -78,7 +79,7 @@ $ nano plot_lfric.py
78
79
$ cat plot_lfric.py
79
80
```
80
81
81
- ``` output
82
+ ``` python
82
83
# pretnd there is code here!
83
84
# more Python
84
85
```
@@ -99,7 +100,7 @@ $ nano plot_lfric.py
99
100
$ cat plot_lfric.py
100
101
```
101
102
102
- ``` output
103
+ ``` python
103
104
# pretend there is code here!
104
105
# more Python
105
106
```
@@ -170,7 +171,7 @@ three feature branch commits which is `ed14d18`
170
171
from the log above:
171
172
172
173
``` bash
173
- git rebase -i ed14d18
174
+ $ git rebase -i ed14d18
174
175
```
175
176
176
177
``` output
@@ -230,61 +231,64 @@ $ git rebase --abort
230
231
231
232
:::
232
233
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:
236
236
237
237
``` 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
240
239
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
241
241
```
242
242
243
- Save the and close the file.
243
+ Save and close the file.
244
244
245
245
``` 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)$
254
255
```
255
256
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.
260
262
261
263
Opening up the file now shows:
262
264
263
265
``` 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
265
268
```
266
269
267
- ```
268
- <<<<<<< HEAD
270
+ ``` python
269
271
# pretnd there is code here!
270
- =======
271
- # pretend there is code here!
272
- # more Python
273
- >>>>>>> 0bb7871
274
272
```
275
273
276
274
Modify and save the file so that it reads:
277
275
278
- ```
276
+ ``` python
279
277
# pretend there is code here!
280
278
```
281
279
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:
284
281
285
282
``` 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
288
292
```
289
293
290
294
For the last few commands we have included the
@@ -293,15 +297,13 @@ provided by the `git-prompt.sh` script
293
297
you set up in the [ git-novice lesson] ( https://www.astropython.com/git-novice/index.html#git-autocomplete ) .
294
298
You can see the prompt clearly states we are
295
299
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.
297
301
298
- We now see the following output:
302
+ Now we have amended the commit we can
303
+ continue with the rebase:
299
304
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
305
307
Auto-merging plot_lfric.py
306
308
CONFLICT (content): Merge conflict in plot_lfric.py
307
309
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".
310
312
hint: You can instead skip this commit: run " git rebase --skip" .
311
313
hint: To abort and get back to the state before " git rebase" , run " git rebase --abort" .
312
314
Could not apply 7053de7... Extends the LFRic data plotting script to plot 2D fields
315
+ [git-training-demo]:(add_plot_script * +| REBASE 2/3)$
313
316
```
314
317
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:
318
322
319
323
``` 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
322
326
```
323
327
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
325
340
# pretend there is code here!
326
341
# more Python
327
342
```
328
343
329
344
Add the file and continue with the rebase:
330
345
331
346
``` 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
334
349
```
335
350
336
351
``` output
337
352
[detached HEAD d7def6a] Extends the LFRic data plotting script to plot 2D fields
338
353
1 file changed, 1 insertion(+)
339
354
Successfully rebased and updated refs/heads/add_plot_script.
355
+ [git-training-demo]:(add_plot_script)$
340
356
```
341
357
342
358
We have now successfully rebased our feature branch.
@@ -374,6 +390,21 @@ output above, on the feature branch.
374
390
375
391
## Updating a Branch
376
392
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
+
377
408
You can also use rebase to move your changes to
378
409
branch off the ` HEAD ` of ` main ` .
379
410
While we rebased our feature branch another
@@ -407,7 +438,15 @@ gitGraph
407
438
commit id: 'd7def6a'
408
439
```
409
440
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:
411
450
412
451
``` bash
413
452
$ git rebase main
@@ -475,35 +514,63 @@ to check yourself.
475
514
476
515
::::::::::::::::::::::::::::::::::::::: challenge
477
516
478
- ## Modifying Commits and their Messages
517
+ ## Modifying Commit Messages
479
518
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.
484
520
485
521
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?
490
524
491
525
::::::::::::::: solution
492
526
493
527
## Solution
494
528
495
- 1 . Edit the contents of a commit
529
+ Edit the commit message:
496
530
531
+ ``` text
532
+ # r, reword <commit> = use commit, but edit the commit message
497
533
```
498
- # e, edit <commit> = use commit, but stop for amending
499
- ```
500
534
501
- 2 . Edit the commit message
535
+ :::::::::::::::::::::::::
536
+
537
+ ::::::::::::::::::::::::::::::::::::::::::::::::::
538
+
539
+ ::::::::::::::::::::::::::::::::::::::: challenge
540
+
541
+ ## Re-ordering Commits
502
542
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
503
550
```
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
505
566
```
506
567
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
+
507
574
:::::::::::::::::::::::::
508
575
509
576
::::::::::::::::::::::::::::::::::::::::::::::::::
0 commit comments