generated from jtr13/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit.html
1157 lines (1096 loc) · 99.9 KB
/
git.html
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 4 Git | Reproducible Analytical Pipelines - Master’s of Data Science</title>
<meta name="description" content="Chapter 4 Git | Reproducible Analytical Pipelines - Master’s of Data Science" />
<meta name="generator" content="bookdown 0.29 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 4 Git | Reproducible Analytical Pipelines - Master’s of Data Science" />
<meta property="og:type" content="book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 4 Git | Reproducible Analytical Pipelines - Master’s of Data Science" />
<meta name="author" content="Bruno Rodrigues" />
<meta name="date" content="2022-11-19" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="a-primer-on-functional-programming.html"/>
<link rel="next" href="package-development.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">RAP4MADS</a></li>
<li class="divider"></li>
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> Introduction</a>
<ul>
<li class="chapter" data-level="1.1" data-path="index.html"><a href="index.html#schedule"><i class="fa fa-check"></i><b>1.1</b> Schedule</a></li>
<li class="chapter" data-level="1.2" data-path="index.html"><a href="index.html#reproducible-analytical-pipelines"><i class="fa fa-check"></i><b>1.2</b> Reproducible analytical pipelines?</a></li>
<li class="chapter" data-level="1.3" data-path="index.html"><a href="index.html#data-products"><i class="fa fa-check"></i><b>1.3</b> Data products?</a></li>
<li class="chapter" data-level="1.4" data-path="index.html"><a href="index.html#machine-learning"><i class="fa fa-check"></i><b>1.4</b> Machine learning?</a></li>
<li class="chapter" data-level="1.5" data-path="index.html"><a href="index.html#why-r-why-not-insert-your-favourite-programming-language"><i class="fa fa-check"></i><b>1.5</b> Why R? Why not [insert your favourite programming language]</a></li>
<li class="chapter" data-level="1.6" data-path="index.html"><a href="index.html#pre-requisites"><i class="fa fa-check"></i><b>1.6</b> Pre-requisites</a></li>
<li class="chapter" data-level="1.7" data-path="index.html"><a href="index.html#grading"><i class="fa fa-check"></i><b>1.7</b> Grading</a></li>
<li class="chapter" data-level="1.8" data-path="index.html"><a href="index.html#jargon"><i class="fa fa-check"></i><b>1.8</b> Jargon</a></li>
<li class="chapter" data-level="1.9" data-path="index.html"><a href="index.html#further-reading"><i class="fa fa-check"></i><b>1.9</b> Further reading</a></li>
<li class="chapter" data-level="1.10" data-path="index.html"><a href="index.html#license"><i class="fa fa-check"></i><b>1.10</b> License</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="introduction-to-r.html"><a href="introduction-to-r.html"><i class="fa fa-check"></i><b>2</b> Introduction to R</a>
<ul>
<li class="chapter" data-level="2.1" data-path="introduction-to-r.html"><a href="introduction-to-r.html#reading-in-data-with-r"><i class="fa fa-check"></i><b>2.1</b> Reading in data with R</a></li>
<li class="chapter" data-level="2.2" data-path="introduction-to-r.html"><a href="introduction-to-r.html#a-little-aside-on-pipes"><i class="fa fa-check"></i><b>2.2</b> A little aside on pipes</a></li>
<li class="chapter" data-level="2.3" data-path="introduction-to-r.html"><a href="introduction-to-r.html#exploring-and-cleaning-data-with-r"><i class="fa fa-check"></i><b>2.3</b> Exploring and cleaning data with R</a></li>
<li class="chapter" data-level="2.4" data-path="introduction-to-r.html"><a href="introduction-to-r.html#data-visualization"><i class="fa fa-check"></i><b>2.4</b> Data visualization</a></li>
<li class="chapter" data-level="2.5" data-path="introduction-to-r.html"><a href="introduction-to-r.html#further-reading-1"><i class="fa fa-check"></i><b>2.5</b> Further reading</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="a-primer-on-functional-programming.html"><a href="a-primer-on-functional-programming.html"><i class="fa fa-check"></i><b>3</b> A primer on functional programming</a>
<ul>
<li class="chapter" data-level="3.1" data-path="a-primer-on-functional-programming.html"><a href="a-primer-on-functional-programming.html#introduction-1"><i class="fa fa-check"></i><b>3.1</b> Introduction</a></li>
<li class="chapter" data-level="3.2" data-path="a-primer-on-functional-programming.html"><a href="a-primer-on-functional-programming.html#defining-your-own-functions"><i class="fa fa-check"></i><b>3.2</b> Defining your own functions</a></li>
<li class="chapter" data-level="3.3" data-path="a-primer-on-functional-programming.html"><a href="a-primer-on-functional-programming.html#functional-programming"><i class="fa fa-check"></i><b>3.3</b> Functional programming</a></li>
<li class="chapter" data-level="3.4" data-path="a-primer-on-functional-programming.html"><a href="a-primer-on-functional-programming.html#further-reading-2"><i class="fa fa-check"></i><b>3.4</b> Further reading</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="git.html"><a href="git.html"><i class="fa fa-check"></i><b>4</b> Git</a>
<ul>
<li class="chapter" data-level="4.1" data-path="git.html"><a href="git.html#introduction-2"><i class="fa fa-check"></i><b>4.1</b> Introduction</a></li>
<li class="chapter" data-level="4.2" data-path="git.html"><a href="git.html#installing-git"><i class="fa fa-check"></i><b>4.2</b> Installing Git</a></li>
<li class="chapter" data-level="4.3" data-path="git.html"><a href="git.html#setting-up-a-repo"><i class="fa fa-check"></i><b>4.3</b> Setting up a repo</a></li>
<li class="chapter" data-level="4.4" data-path="git.html"><a href="git.html#cloning-the-repository-onto-your-computer"><i class="fa fa-check"></i><b>4.4</b> Cloning the repository onto your computer</a></li>
<li class="chapter" data-level="4.5" data-path="git.html"><a href="git.html#your-first-commit"><i class="fa fa-check"></i><b>4.5</b> Your first commit</a></li>
<li class="chapter" data-level="4.6" data-path="git.html"><a href="git.html#collaborating"><i class="fa fa-check"></i><b>4.6</b> Collaborating</a></li>
<li class="chapter" data-level="4.7" data-path="git.html"><a href="git.html#branches"><i class="fa fa-check"></i><b>4.7</b> Branches</a></li>
<li class="chapter" data-level="4.8" data-path="git.html"><a href="git.html#contributing-to-someone-elses-repository"><i class="fa fa-check"></i><b>4.8</b> Contributing to someone else’s repository</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="package-development.html"><a href="package-development.html"><i class="fa fa-check"></i><b>5</b> Package development</a>
<ul>
<li class="chapter" data-level="5.1" data-path="package-development.html"><a href="package-development.html#introduction-3"><i class="fa fa-check"></i><b>5.1</b> Introduction</a></li>
<li class="chapter" data-level="5.2" data-path="package-development.html"><a href="package-development.html#getting-started"><i class="fa fa-check"></i><b>5.2</b> Getting started</a></li>
<li class="chapter" data-level="5.3" data-path="package-development.html"><a href="package-development.html#adding-functions"><i class="fa fa-check"></i><b>5.3</b> Adding functions</a>
<ul>
<li class="chapter" data-level="5.3.1" data-path="package-development.html"><a href="package-development.html#functions-dependencies"><i class="fa fa-check"></i><b>5.3.1</b> Functions dependencies</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="package-development.html"><a href="package-development.html#documentation"><i class="fa fa-check"></i><b>5.4</b> Documentation</a>
<ul>
<li class="chapter" data-level="5.4.1" data-path="package-development.html"><a href="package-development.html#documenting-functions"><i class="fa fa-check"></i><b>5.4.1</b> Documenting functions</a></li>
<li class="chapter" data-level="5.4.2" data-path="package-development.html"><a href="package-development.html#documenting-the-package"><i class="fa fa-check"></i><b>5.4.2</b> Documenting the package</a></li>
<li class="chapter" data-level="5.4.3" data-path="package-development.html"><a href="package-development.html#checking-your-package"><i class="fa fa-check"></i><b>5.4.3</b> Checking your package</a></li>
<li class="chapter" data-level="5.4.4" data-path="package-development.html"><a href="package-development.html#installing-your-package"><i class="fa fa-check"></i><b>5.4.4</b> Installing your package</a></li>
</ul></li>
<li class="chapter" data-level="5.5" data-path="package-development.html"><a href="package-development.html#further-reading-3"><i class="fa fa-check"></i><b>5.5</b> Further reading</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="unit-tests.html"><a href="unit-tests.html"><i class="fa fa-check"></i><b>6</b> Unit tests</a>
<ul>
<li class="chapter" data-level="6.1" data-path="unit-tests.html"><a href="unit-tests.html#introduction-4"><i class="fa fa-check"></i><b>6.1</b> Introduction</a></li>
<li class="chapter" data-level="6.2" data-path="unit-tests.html"><a href="unit-tests.html#testing-your-package"><i class="fa fa-check"></i><b>6.2</b> Testing your package</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="unit-tests.html"><a href="unit-tests.html#is-the-function-returning-an-expected-value-for-a-given-input"><i class="fa fa-check"></i><b>6.2.1</b> Is the function returning an expected value for a given input?</a></li>
<li class="chapter" data-level="6.2.2" data-path="unit-tests.html"><a href="unit-tests.html#can-the-function-deal-with-all-kinds-of-input"><i class="fa fa-check"></i><b>6.2.2</b> Can the function deal with all kinds of input?</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="unit-tests.html"><a href="unit-tests.html#back-to-developing-again"><i class="fa fa-check"></i><b>6.3</b> Back to developing again</a></li>
<li class="chapter" data-level="6.4" data-path="unit-tests.html"><a href="unit-tests.html#and-back-to-testing"><i class="fa fa-check"></i><b>6.4</b> And back to testing</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="setting-up-pipelines-with-targets.html"><a href="setting-up-pipelines-with-targets.html"><i class="fa fa-check"></i><b>7</b> Setting up pipelines with <code>{targets}</code></a>
<ul>
<li class="chapter" data-level="7.1" data-path="setting-up-pipelines-with-targets.html"><a href="setting-up-pipelines-with-targets.html#introduction-5"><i class="fa fa-check"></i><b>7.1</b> Introduction</a></li>
<li class="chapter" data-level="7.2" data-path="setting-up-pipelines-with-targets.html"><a href="setting-up-pipelines-with-targets.html#build-automation-with-r"><i class="fa fa-check"></i><b>7.2</b> Build automation with R</a></li>
<li class="chapter" data-level="7.3" data-path="setting-up-pipelines-with-targets.html"><a href="setting-up-pipelines-with-targets.html#an-aside-on-renv"><i class="fa fa-check"></i><b>7.3</b> An aside on <code>{renv}</code></a></li>
<li class="chapter" data-level="7.4" data-path="setting-up-pipelines-with-targets.html"><a href="setting-up-pipelines-with-targets.html#our-actual-first-pipeline"><i class="fa fa-check"></i><b>7.4</b> Our actual first pipeline</a></li>
<li class="chapter" data-level="7.5" data-path="setting-up-pipelines-with-targets.html"><a href="setting-up-pipelines-with-targets.html#running-someone-elses-pipeline"><i class="fa fa-check"></i><b>7.5</b> Running someone else’s pipeline</a></li>
<li class="chapter" data-level="7.6" data-path="setting-up-pipelines-with-targets.html"><a href="setting-up-pipelines-with-targets.html#running-any-code-with-older-packages"><i class="fa fa-check"></i><b>7.6</b> Running any code with older packages</a></li>
<li class="chapter" data-level="7.7" data-path="setting-up-pipelines-with-targets.html"><a href="setting-up-pipelines-with-targets.html#why-we-need-more"><i class="fa fa-check"></i><b>7.7</b> Why we need more</a></li>
<li class="chapter" data-level="7.8" data-path="setting-up-pipelines-with-targets.html"><a href="setting-up-pipelines-with-targets.html#further-reading-4"><i class="fa fa-check"></i><b>7.8</b> Further reading</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="data-products-1.html"><a href="data-products-1.html"><i class="fa fa-check"></i><b>8</b> Data products</a>
<ul>
<li class="chapter" data-level="8.1" data-path="data-products-1.html"><a href="data-products-1.html#introduction-6"><i class="fa fa-check"></i><b>8.1</b> Introduction</a></li>
<li class="chapter" data-level="8.2" data-path="data-products-1.html"><a href="data-products-1.html#a-first-taste-of-quarto"><i class="fa fa-check"></i><b>8.2</b> A first taste of Quarto</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="data-products-1.html"><a href="data-products-1.html#python-and-julia-code-chunks"><i class="fa fa-check"></i><b>8.2.1</b> Python and Julia code chunks</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="data-products-1.html"><a href="data-products-1.html#other-output-formats"><i class="fa fa-check"></i><b>8.3</b> Other output formats</a>
<ul>
<li class="chapter" data-level="8.3.1" data-path="data-products-1.html"><a href="data-products-1.html#word"><i class="fa fa-check"></i><b>8.3.1</b> Word</a></li>
<li class="chapter" data-level="8.3.2" data-path="data-products-1.html"><a href="data-products-1.html#presentations"><i class="fa fa-check"></i><b>8.3.2</b> Presentations</a></li>
<li class="chapter" data-level="8.3.3" data-path="data-products-1.html"><a href="data-products-1.html#pdf"><i class="fa fa-check"></i><b>8.3.3</b> PDF</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="data-products-1.html"><a href="data-products-1.html#interactive-web-applications-with-shiny"><i class="fa fa-check"></i><b>8.4</b> Interactive web applications with {shiny}</a>
<ul>
<li class="chapter" data-level="8.4.1" data-path="data-products-1.html"><a href="data-products-1.html#the-basic-structure-of-a-shiny-app"><i class="fa fa-check"></i><b>8.4.1</b> The basic structure of a Shiny app</a></li>
<li class="chapter" data-level="8.4.2" data-path="data-products-1.html"><a href="data-products-1.html#slightly-more-advanced-shiny"><i class="fa fa-check"></i><b>8.4.2</b> Slightly more advanced shiny</a></li>
<li class="chapter" data-level="8.4.3" data-path="data-products-1.html"><a href="data-products-1.html#basic-optimization-of-shiny-apps"><i class="fa fa-check"></i><b>8.4.3</b> Basic optimization of Shiny apps</a></li>
<li class="chapter" data-level="8.4.4" data-path="data-products-1.html"><a href="data-products-1.html#deploying-your-shiny-app"><i class="fa fa-check"></i><b>8.4.4</b> Deploying your shiny app</a></li>
<li class="chapter" data-level="8.4.5" data-path="data-products-1.html"><a href="data-products-1.html#references"><i class="fa fa-check"></i><b>8.4.5</b> References</a></li>
</ul></li>
<li class="chapter" data-level="8.5" data-path="data-products-1.html"><a href="data-products-1.html#how-to-build-data-products-using-targets"><i class="fa fa-check"></i><b>8.5</b> How to build data products using <code>{targets}</code></a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html"><i class="fa fa-check"></i><b>9</b> Self-contained RAPs with Docker</a>
<ul>
<li class="chapter" data-level="9.1" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html#introduction-7"><i class="fa fa-check"></i><b>9.1</b> Introduction</a></li>
<li class="chapter" data-level="9.2" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html#installing-docker"><i class="fa fa-check"></i><b>9.2</b> Installing Docker</a></li>
<li class="chapter" data-level="9.3" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html#the-rocker-project"><i class="fa fa-check"></i><b>9.3</b> The Rocker Project</a></li>
<li class="chapter" data-level="9.4" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html#docker-essentials"><i class="fa fa-check"></i><b>9.4</b> Docker essentials</a></li>
<li class="chapter" data-level="9.5" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html#making-our-own-images-and-run-some-code"><i class="fa fa-check"></i><b>9.5</b> Making our own images and run some code</a></li>
<li class="chapter" data-level="9.6" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html#reproducibility-with-docker"><i class="fa fa-check"></i><b>9.6</b> Reproducibility with Docker</a></li>
<li class="chapter" data-level="9.7" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html#building-a-truly-reproducible-pipeline"><i class="fa fa-check"></i><b>9.7</b> Building a truly reproducible pipeline</a></li>
<li class="chapter" data-level="9.8" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html#one-last-thing"><i class="fa fa-check"></i><b>9.8</b> One last thing</a></li>
<li class="chapter" data-level="9.9" data-path="self-contained-raps-with-docker.html"><a href="self-contained-raps-with-docker.html#further-reading-5"><i class="fa fa-check"></i><b>9.9</b> Further reading</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="very-basic-cicd-with-github-actions.html"><a href="very-basic-cicd-with-github-actions.html"><i class="fa fa-check"></i><b>10</b> (Very) basic CI/CD with Github Actions</a>
<ul>
<li class="chapter" data-level="10.1" data-path="very-basic-cicd-with-github-actions.html"><a href="very-basic-cicd-with-github-actions.html#introduction-8"><i class="fa fa-check"></i><b>10.1</b> Introduction</a></li>
<li class="chapter" data-level="10.2" data-path="very-basic-cicd-with-github-actions.html"><a href="very-basic-cicd-with-github-actions.html#getting-your-repo-ready-for-github-actions"><i class="fa fa-check"></i><b>10.2</b> Getting your repo ready for Github Actions</a></li>
<li class="chapter" data-level="10.3" data-path="very-basic-cicd-with-github-actions.html"><a href="very-basic-cicd-with-github-actions.html#further-reading-6"><i class="fa fa-check"></i><b>10.3</b> Further reading</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="what-else-should-you-learn.html"><a href="what-else-should-you-learn.html"><i class="fa fa-check"></i><b>11</b> What else should you learn?</a>
<ul>
<li class="chapter" data-level="11.1" data-path="what-else-should-you-learn.html"><a href="what-else-should-you-learn.html#touch-typing"><i class="fa fa-check"></i><b>11.1</b> Touch typing</a></li>
<li class="chapter" data-level="11.2" data-path="what-else-should-you-learn.html"><a href="what-else-should-you-learn.html#vim"><i class="fa fa-check"></i><b>11.2</b> Vim</a></li>
<li class="chapter" data-level="11.3" data-path="what-else-should-you-learn.html"><a href="what-else-should-you-learn.html#statistical-modeling"><i class="fa fa-check"></i><b>11.3</b> Statistical modeling</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="conclusion.html"><a href="conclusion.html"><i class="fa fa-check"></i><b>12</b> Conclusion</a>
<ul>
<li class="chapter" data-level="12.1" data-path="conclusion.html"><a href="conclusion.html#why-bother"><i class="fa fa-check"></i><b>12.1</b> Why bother</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/b-rodrigues/rap4mads" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Reproducible Analytical Pipelines - Master’s of Data Science</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="git" class="section level1 hasAnchor" number="4">
<h1><span class="header-section-number">Chapter 4</span> Git<a href="git.html#git" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<div style="text-align:center;">
<p><img src="img/git_header.png" width="512" /></p>
</div>
<p>What you’ll have learned by the end of the chapter: basics of working alone, and collaboration, using Git.</p>
<div id="introduction-2" class="section level2 hasAnchor" number="4.1">
<h2><span class="header-section-number">4.1</span> Introduction<a href="git.html#introduction-2" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Git is a software for version control. Version control is absolutely essential in software
engineering, or when setting up a RAP. If you don’t install a version control system such as Git,
don’t even start trying to set up a RAP. But what does a version control system like Git actually
do? The basic workflow of Git is as follows: you start by setting up a repository for a project. On
your computer, this is nothing more than a folder with your scripts in it. However, if you’re using
Git to keep track of what’s inside that folder, there will be a hidden <code>.git</code> folder with a bunch
of files in it. You can forget about that folder, this is for Git’s own internal needs. What
matters, is that when you make changes to your files, you can first <em>commit</em> these changes, and
then push them back to a repository. Collaborators can copy this repository and synchronize their
files saved on their computers with your changes. Your collaborators can then also work on the
files, then commit and push the changes to the repository as well.</p>
<p>You can then pull back these changes onto your computer, add more code, commit, push, etc…
Git makes it easy to collaborate on projects either with other people, or with future
you. It is possible to roll back to previous versions of your code base, you can create new
branches of your project to test new features (without affecting the main branch of your code),
collaborators can submit patches that you can review and merge, and and and…</p>
<p>In my experience, learning git is one of the most difficult things there is for students.
And this is because Git solves a complex problem, and there is no easy way to solve
a complex problem. But I would however say that Git is not unnescessarily complex. So
buckle up, because this chapter is not going to be easy.</p>
<p>Git is incredibly powerful, and absolutely essential in our line of work, it is simply not p
ossible to not know at least some basics of Git. And this is what we’re going to do,
learn the basics, it’ll keep us plenty busy already.</p>
<p>But for now, let’s pause for a brief moment and watch <a href="https://www.youtube.com/watch?v=2ReR1YJrNOM">this video</a>
that explains in 2 minutes the general idea of Git.</p>
<p>Let’s get started.</p>
<p>You might have heard of <a href="https://github.com">github.com</a>: this is a website that
allows programmers to set up repositories on which they can host their code. The way to interact
with github.com is via Git; but there are many other website like github.com, such as
<a href="https://gitlab.com">gitlab.com</a> and <a href="https://bitbucket.com">bitbucket.com</a>.</p>
<p>For this course, you should create an account on <a href="https://github.com">github.com</a>. This should be
easy enough. Then you should install Git on your computer.</p>
</div>
<div id="installing-git" class="section level2 hasAnchor" number="4.2">
<h2><span class="header-section-number">4.2</span> Installing Git<a href="git.html#installing-git" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Installing Git is not hard; it installs like any piece of software on your computer. If you’re
running a Linux distribution, chances are you already have Git installed. To check if it’s already
installed on a Linux system, open a terminal and type <code>which git</code>. If a path gets returned, like
<code>usr/bin/gin</code>, congratulations, it’s installed, if the command returns nothing you’ll have to
install it. On Ubuntu, type <code>sudo apt-get install git</code> and just wait a bit. If you’re using macOS
or Windows, you will need to install it manually. For Windows, download the installer from
<a href="https://gitforwindows.org/">here</a>, and for macOS from <a href="https://git-scm.com/download/mac">here</a>;
you’ll see that there are several ways of installing it on macOS, if you’ve never heard of homebrew
or macports then install the binary package from
<a href="here">https://sourceforge.net/projects/git-osx-installer/</a>.</p>
</div>
<div id="setting-up-a-repo" class="section level2 hasAnchor" number="4.3">
<h2><span class="header-section-number">4.3</span> Setting up a repo<a href="git.html#setting-up-a-repo" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Ok so now that Git is installed, we can actually start using it. First, let’s start by creating
a new repository on github.com. As I’ve mentioned in the introductory paragraph, Git will allow
you to interact with github.com, and you’ll see in what ways soon enough. For now, login to
your github.com account, and create a new repository by clicking on the ‘plus’ sign in the top
right corner of your profile page and then choose ‘New repository’:</p>
<div style="text-align:center;">
<p><img src="img/github_1.png" width="702" /></p>
</div>
<p>In the next screen, choose a nice name for your repository and ignore the other options, they’re
not important for now. Then click on ‘Create repository’:</p>
<div style="text-align:center;">
<p><img src="img/github_2.png" width="416" /></p>
</div>
<p>Ok, we’re almost done with the easy part. The next screen tells us we can start interacting with the
repository. For this, we’re first going to click on ‘README’:</p>
<div style="text-align:center;">
<p><img src="img/github_3.png" width="601" /></p>
</div>
<p>This will add a <code>README</code> file that we can also edit from github.com directly:</p>
<div style="text-align:center;">
<p><img src="img/github_4.png" width="612" /></p>
</div>
<p>Add some lines to the file, and then click on ‘Commit new file’. You’ll end up on the main page of
your freshly created repository. We are now done with setting up the repository on github.com. We
can now <em>clone</em> the repository onto our machines. For this, click on ‘Code’, then ‘SSH’ and then
on the copy icon:</p>
<div style="text-align:center;">
<p><img src="img/github_5.png" width="659" /></p>
</div>
<p>Now, to make things easier on you, we’re going to use Rstudio as an interface
for Git. But you should know that Git can be used independently from a terminal
application on Linux or macOS, or from Git Bash on Windows, and you should
definitely get familiar with the Linux/macOS command line at some point if you wish
to become a data scientist. This is because most servers, if not all, that you are
going to interact with in your career are running some flavour of Linux. But since
the Linux command line is outside the scope of this course, we’ll use Rstudio instead
(well, we’ll use it as much as we can, because at some point it won’t be enough and
have to use the terminal instead anyways…).</p>
</div>
<div id="cloning-the-repository-onto-your-computer" class="section level2 hasAnchor" number="4.4">
<h2><span class="header-section-number">4.4</span> Cloning the repository onto your computer<a href="git.html#cloning-the-repository-onto-your-computer" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Start Rstudio and click on ‘new project’ and then ‘Version Control’:</p>
<div style="text-align:center;">
<p><img src="img/github_6.png" width="432" /></p>
</div>
<p>Then choose ‘Git’:</p>
<div style="text-align:center;">
<p><img src="img/github_7.png" width="324" /></p>
</div>
<p>Then paste the link from before into the ‘Repository URL’ field, the ‘project directory name’
will fill out automatically, choose where to save the repository in your computer, click on
‘Open in new session’ and then on ‘Create Project’:</p>
<div style="text-align:center;">
<p><img src="img/github_8.png" width="325" /></p>
</div>
<p>A new Rstudio window should open. There are several things that you should pay attention to now:</p>
<div style="text-align:center;">
<p><img src="img/github_9.png" width="666" /></p>
</div>
<p>Icon (1) indicates that this project is <em>git-enabled</em> so to speak. (2) shows you that Rstudio is open
inside the <code>example_repo</code> (or whatever you named your repo to) project, and (3) shows you the actual
repository that was downloaded from github.com at the path you chose before. You will also see the
<code>README</code> file that we created before.</p>
</div>
<div id="your-first-commit" class="section level2 hasAnchor" number="4.5">
<h2><span class="header-section-number">4.5</span> Your first commit<a href="git.html#your-first-commit" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Let’s now create a simple script and add some lines of code to it, and save it. Check out the <code>Git</code>
tab now, you should see your script there, alongside a <code>?</code> icon:</p>
<div style="text-align:center;">
<p><img src="img/github_10.png" width="762" /></p>
</div>
<p>We are now ready to commit the file, but first let’s check out what actually changed. If you click
on <code>Diff</code>, a new window will open with the different files that changed since last time:</p>
<div style="text-align:center;">
<p><img src="img/github_11.png" width="517" /></p>
</div>
<p>Icon (1) shows you the list of files that changed. We only created the file called <code>my_script.R</code>, but two
other files are listed as well. These files are automatically generated when starting a new project.
<code>.gitignore</code> lists files and folders that Git should not track, meaning, any change that will affect
these files will be ignored by Git. This means that these files will also not be uploaded to
github.com when committing. The file ending with the <code>.Rproj</code> extension is a RStudio specific file,
which simply defines some variables that help RStudio start your project. What matters here is
that the files you changed are listed, and that you saved them. You can double check that you actually
correctly saved your files by looking at (2), which lists the lines that were added (added lines
will be highlighted in green, deleted lines in red). In (3) you can write a commit message. This
message should be informative enough that a coworker, or future you, can read through them and have
a rough idea of what changed. Best practice is to commit often and early, and try to have one commit
per change (per file for example, or per function within that file) that you make.
Let’s write something like: “Started project: first graph done” as the commit message. We’re almost done: now
let’s stage the files for this commit. This means that we can choose which files should actually
be included in this commit. You can only stage one file, several files, or all files. Since this is
our first commit, let’s stage everything we’ve got, by simply clicking on the checkboxes below the
column <code>Staged</code> in (1).</p>
<div style="text-align:center;">
<p><img src="img/github_12.png" width="518" /></p>
</div>
<p>The status of the files now changed: they’ve been added for this commit. We can now click on the
<code>Commit</code> button. Now these changes have been committed there are no unstaged files anymore. We have
two options at this point: we can continue working, and then do another commit, or we can push
our changes to github.com. Committing without pushing does not make our changes available to our
colleagues, but because we committed them, we can recover our changes. For example, if I continue
working on my file and remove some lines by mistake, I can recover them (I’ll show you how to do
this later on). But it is a much better idea to push our commit now. This makes our changes
available to colleagues (who need to pull the changes from github.com) and should our computer
spontaneously combust, at least or work is now securely saved on github.com. So let’s <code>Push</code>:</p>
<div style="text-align:center;">
<p><img src="img/github_13.png" width="458" /></p>
</div>
<p>Ooooooops! Something’s wrong! Apparently, we do not have access rights to the repo? This can sound
weird, because after all, we created the repo with our account and then cloned it. So what’s going
on? Well, remember that anyone can clone a public repository, but only authorized people can
push changes to it. So at this stage, the Git software (that we’re using through RStudio) has
no clue who you are. Git simply doesn’t know that your the <em>admin</em> of the repository. You need to
provide a way for Git to know by logging in. And the way you login is through a so-called ssh key.</p>
<p>Now if you thought that Git was confusing, I’m sorry to say that what’s coming confuses students
in general even more. Ok so what’s a ssh key, and why does Git need it? <em>An</em> ssh key is actually
a misnomer, because we should really be talking about a pair of keys. The idea is that you
generated two files on the computer that you need to access github.com from. One of these keys
will be a public key, the other a private key. The private key will be a file usually called
<code>id_rsa</code> without any extension, while the public key will be called the same, but with a <code>.pub</code>
extension, so <code>id_rsa.pub</code> (we will generate these two files using RStudio in a bit).
What you do is that you give the public key to github.com, but you keep your private key on
your machine. Never, ever, upload or share your private key with anyone! It’s called private for
a reason. Once github.com has your public key, each time you want to push to github.com,
what happens is that the public key is checked against your private key. If they match, github.com
knows that you are the person you claim to be, and will allow you to push to the repository. If not
you will get the error from before.</p>
<p>So let’s now generate an ssh key pair. For this, go to <code>Tools > Global Options > Git/Svn</code>, and then
click on the <code>Create RSA Key...</code></p>
<div style="text-align:center;">
<p><img src="img/github_14.png" width="505" /></p>
</div>
<p>Icon (1) shows you the path where the keys will be saved. This is only useful if
you have reasons to worry that your private key might be compromised, but without physical access
to your machine, an attacker would have a lot of trouble retrieving it (if you keep your OS updated…).
Finally click on <code>Create</code>:</p>
<div style="text-align:center;">
<p><img src="img/github_15.png" width="300" /></p>
</div>
<p>Ok so now that you have generated these keys, let’s copy the public key in
our clipboard (because we need to paste the key into github.com). You should be able to find this
key from RStudio. Go back to <code>Tools > Global Options > Git/Svn</code>, and then click on <code>View public key</code>:</p>
<div style="text-align:center;">
<p><img src="img/github_18.png" width="418" /></p>
</div>
<p>A new window will open showing you your public key. You can now copy and paste it into github.com.
For this, first go to your profile, then <code>Settings</code> then <code>SSH and GPG keys</code>:</p>
<div style="text-align:center;">
<p><img src="img/github_16.png" width="691" /></p>
</div>
<p>Then, on the new screen click on <code>New SSH key</code>:</p>
<div style="text-align:center;">
<p><img src="img/github_17.png" width="440" /></p>
</div>
<p>You can now add your key. Add a title, for example <code>home</code> for your home
computer, or <code>work</code> for your work laptop. Paste the key from RStudio
into the field (2), and then click on <code>Add SSH key</code>:</p>
<div style="text-align:center;">
<p><img src="img/github_19.png" width="697" /></p>
</div>
<p>Ok, now that github.com has your public key, you can now push your commits
without any error. Go back to RStudio, to the <code>Git</code> tab and click on
<code>Push</code>:</p>
<div style="text-align:center;">
<p><img src="img/github_20.png" width="405" /></p>
</div>
<p>A new window will open, this time showing you that the upload went through:</p>
<div style="text-align:center;">
<p><img src="img/github_21.png" width="514" /></p>
</div>
<p>You will need to add one public key per computer you use on github.com.
In the past, it was possible to push your commits by providing a password
each time. This was not secure enough however, so now the only way to
to push commits is via ssh key pairs. This concept is quite important:
whatever service you use, even if your company has a private Git server
instance, you will need to provide the public key to the central server.
All of this ssh key pair business IS NOT specific to github.com, so
make sure that you understand this well, because sooner rather later,
you will need to provide another public key, either because you work from
several computers or because the your first job will have it’s own
Git instance.</p>
<p>Ok so now you have an account on github.com, and know how to set up a
repo and push code to it. This is already quite useful, because it allows
you and future you to collaborate. What I mean by this is that if in two
or three months you need to go back to some previous version of your
code this is now possible. Let’s try it out; change the file by adding
some lines to it, commit your changes and push again. Remember to use
a commit message that explain what you did. Once you’re done, go back
to the <code>Git</code> tab of Rstudio, and click on the <code>History</code> button (the
icon is a clock):</p>
<div style="text-align:center;">
<p><img src="img/github_22.png" width="969" /></p>
</div>
<p>As you can see from the picture above, clicking on <code>History</code> shows every commit since the beginning
of the repo. It also shows you who pushed that particular commit, and when. For now, you will only
see your name. At (1) you see the lines I’ve added. These are reflected, in green, in the <code>History</code>
window. If I had removed some lines, these would have been highlighted in red in the same window.
(4) shows you the only commit history. There’s not much for now, but for projects that have been
ongoing for some time, this can get quite long! Finally, (5) shows many interesting details. As
before, who pushed the commit, when, the commit message (under <strong>Subject</strong>), and finally the
<strong>SHA</strong>. This is a unique sequence of characters that identifies the commit. If you select another
commit, you will notice that its SHA is different:</p>
<div style="text-align:center;">
<p><img src="img/github_23.png" width="586" /></p>
</div>
<p>The SHA identifier (called a <em>hash</em>) is what we’re going to use to revert to a previous state of
the code base. But because this is a bit advanced, there is no way of doing it from RStudio. You
will need to open a terminal and use Git from there. On Windows, go to the folder of your project,
right-click on some white space and select <code>Git Bash Here</code>:</p>
<div style="text-align:center;">
<p><img src="img/github_24.png" width="340" /></p>
</div>
<p>A similar approach can be used for most Linux distributions (but simply open
a terminal, Git Bash is Windows only), and you can apparently do something
similar on macOS, but first need to active the required service
as <a href="https://ladedu.com/how-to-open-a-terminal-window-at-any-folder-from-finder-in-macos/#How-to-Open-Mac-Terminal-from-Finder-in-the-Current-Folder">explained here</a>.
You can also simply open a terminal and navigate to the right folder using
<code>cd</code>.<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a></p>
<p>Once the terminal is opened, follow along but by adapting the paths to your
computer:</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb30-1"><a href="git.html#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="co"># The first line changes the working directory to my github repo on my computer</span></span>
<span id="cb30-2"><a href="git.html#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="co"># If you did not open the terminal inside the folder as explained above, you need</span></span>
<span id="cb30-3"><a href="git.html#cb30-3" aria-hidden="true" tabindex="-1"></a><span class="co"># adapt the path.</span></span>
<span id="cb30-4"><a href="git.html#cb30-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-5"><a href="git.html#cb30-5" aria-hidden="true" tabindex="-1"></a><span class="bu">cd</span> ~/six_to/example_repo <span class="co"># example_repo is the folder where I cloned the repo</span></span>
<span id="cb30-6"><a href="git.html#cb30-6" aria-hidden="true" tabindex="-1"></a><span class="fu">ls</span> <span class="co"># List all the files in the directory</span></span></code></pre></div>
<pre><code>## example_repo.Rproj
## my_script.R
## new_script.R
## README.md</code></pre>
<p>Listing the files inside the folder confirms that I’m in the right spot. Something
else you could do here is try out some git commands, for example, <code>git log</code>:</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb32-1"><a href="git.html#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> log</span></code></pre></div>
<pre><code>## commit bd7daf0dafb12c0a19ba65f85b54834a02f7d150
## Author: Bruno Rodrigues <bruno@brodrigues.co>
## Date: Mon Oct 17 14:38:59 2022 +0200
##
## added some more lines
##
## commit 95c26ed4dffd8fc40503f25ddc11af7de5c586c0
## Author: Bruno Rodrigues <bruno@brodrigues.co>
## Date: Sat Oct 15 12:52:43 2022 +0200
##
## Started project: first graph done
##
## commit d9cff70ff71241ed8514cb65d97e669b0bbdf0f6
## Author: Bruno Rodrigues <brodriguesco@protonmail.com>
## Date: Thu Oct 13 22:12:06 2022 +0200
##
## Create README.md
</code></pre>
<p><code>git log</code> returns the same stuff as the <code>History</code> button of the <code>Git</code> pane inside
RStudio. You see the commit hash, the name of the author and when the commit was
pushed. At this stage, we have two options. We could “go back in time”, but just look
around, and then go back to where the repository stands currently. Or we could essentially go back in time, and
stay there, meaning, we actually revert the code base back. Let’s try the first
option, let’s just take a look around at the code base at a particular point in time.
Copy the hash of a previous commit. With the hash in your clipboard, use the <code>git checkout</code>
command to go back to this commit:</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb34-1"><a href="git.html#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> checkout 95c26ed4dffd8f</span></code></pre></div>
<p>You will see an output similar to this:</p>
<pre><code>Note: switching to '95c26ed4dffd8f'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 95c26ed Started project: first graph done
</code></pre>
<p>When checking out a commit, you are in <em>detached HEAD</em> state. I won’t go into specifics, but what
this means is that anything you do here, won’t get saved, unless you specifically create a new
branch for it. A Git repository is composed of branches. The branche you’re currently working
on should be called <em>main</em> or <em>master</em>. You can create new branches, and continue working
on these other branches, without affecting the <em>master</em> branch. This allows to explore
new ideas and experiment. If this turns out to be fruitful, you can merge the experimental
branch back into <em>master</em>. We are not going to explore branches in this course, so you’ll
have to read about it on your own. But don’t worry, branches are not that difficult to grok.</p>
<p>Take a look at the script file now, you will see that the lines you added are now missing (the
following line only works on Linux, macOS, or inside a Git Bash terminal on Windows. <code>cat</code> is a
command line program that prints the contents of a text file to a terminal):</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb36-1"><a href="git.html#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> my_script.R</span></code></pre></div>
<p>Once you’re done taking your tour, go back to the main (or master) branch by running:</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb37-1"><a href="git.html#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> checkout main</span></code></pre></div>
<p>Ok, so how do we actually go back to a previous state? For this, use <code>git revert</code>.
But unlike <code>git checkout</code>, you don’t use the hash of the commit you want to go back to.
Instead, you need to use the hash of the commit you want to “cancel”. For example,
imagine that my commit history looks like this:</p>
<pre><code>## commit bd7daf0dafb12c0a19ba65f85b54834a02f7d150
## Author: Bruno Rodrigues <bruno@brodrigues.co>
## Date: Mon Oct 17 14:38:59 2022 +0200
##
## added some more lines
##
## commit 95c26ed4dffd8fc40503f25ddc11af7de5c586c0
## Author: Bruno Rodrigues <bruno@brodrigues.co>
## Date: Sat Oct 15 12:52:43 2022 +0200
##
## Started project: first graph done
##
## commit d9cff70ff71241ed8514cb65d97e669b0bbdf0f6
## Author: Bruno Rodrigues <brodriguesco@protonmail.com>
## Date: Thu Oct 13 22:12:06 2022 +0200
##
## Create README.md
</code></pre>
<p>and let’s suppose I want to go back to commit <code>95c26ed4dffd8fc</code>
(so my second commit). What I need to do is essentially cancel
commit <code>bd7daf0dafb1</code>, which comes after commit <code>95c26ed4dffd8fc</code>
(look at the dates: commit <code>95c26ed4dffd8fc</code> was made on October 15th
and commit <code>bd7daf0dafb1</code> was made on October 17th).
So I need to revert commit <code>bd7daf0dafb1</code>. And that’s what we’re going
to do:</p>
<div class="sourceCode" id="cb39"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb39-1"><a href="git.html#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> revert bd7daf0dafb1</span></code></pre></div>
<p>This opens a text editor inside your terminal. Here you can add a commit
message or just keep the one that was added by default. Let’s just keep
it and quit the text editor. Unfortunately, this is not very use friendly,
but to quit the editor type <code>:q</code>.
(The editor that was opened is vim, a very powerful terminal editor, but
with a very steep learning curve.)
Now you’re back inside your terminal. Type <code>git log</code> and you will see a new
commit (that you have yet to push), which essentially cancels the commit
<code>bd7daf0dafb1</code>. You can now push this; for pushing this one, let’s stay
inside the terminal and use the following command:</p>
<div class="sourceCode" id="cb40"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb40-1"><a href="git.html#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> push origin main</span></code></pre></div>
<p><code>origin main</code>: <code>origin</code> here refers to the remote repository, so to github.com,
and <code>main</code> to the main branch.</p>
<p>Ok, we’re doing with the basics. Let’s now see how we can contribute to some
repository.</p>
</div>
<div id="collaborating" class="section level2 hasAnchor" number="4.6">
<h2><span class="header-section-number">4.6</span> Collaborating<a href="git.html#collaborating" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Github (and similar services) allow you to collaborate with people. There are
two ways of achieving this. You can invite people to work with you on the same
project, by giving them writing rights to the repository. This is what we are
going to cover in this section. The other way to collaborate is to let strangers
fork your repository (make a copy of it on github.com); they can then work on
their copy of the project independently from you. If they want to submit patches
to you, they can do so by doing a so-called <em>pull request</em>. This workflow is quite
different from what we’ll see here and will be discussed in the next section.</p>
<p>So for this section you will need to form teams of at least 2 people. One of you will
invite the other to collaborate by going on github.com and then following the instructions
in the picture below:</p>
<div style="text-align:center;">
<p><img src="img/github_25.png" width="825" /></p>
</div>
<p>Type the username of your colleague to find him/her. In my case I’m inviting my
good friend David Solito:</p>
<div style="text-align:center;">
<p><img src="img/github_26.png" width="590" /></p>
</div>
<p>David now essentially owns the repository as well! So he can contribute to it, just like me.
Now, let’s suppose that I continue working on my end, and don’t coordinate with David.
After all, this is a post-covid world, so David might be working asynchronously from home,
and maybe he lives in an entire different time zone completely! What’s important to realize, is that
unlike other ways of collaborating online (for example with an office suite), you do not
need to coordinate to collaborate with Git.</p>
<p>The file should look like this (yours might be different, it doesn’t matter):</p>
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="git.html#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(mtcars)</span>
<span id="cb41-2"><a href="git.html#cb41-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-3"><a href="git.html#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(mtcars<span class="sc">$</span>mpg, mtcars<span class="sc">$</span>hp)</span></code></pre></div>
<p>I’m going to change it to this:</p>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="git.html#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb42-2"><a href="git.html#cb42-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-3"><a href="git.html#cb42-3" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(mtcars)</span>
<span id="cb42-4"><a href="git.html#cb42-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-5"><a href="git.html#cb42-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> mtcars) <span class="sc">+</span></span>
<span id="cb42-6"><a href="git.html#cb42-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">y =</span> hp, <span class="at">x =</span> mpg))</span></code></pre></div>
<p><img src="bookdownproj_files/figure-html/unnamed-chunk-55-1.png" width="672" /></p>
<p>The only thing I did was change from the base plotting functions to <code>{ggplot2}</code>.
Since you guys formed groups, please work independently on the repository. Go crazy, change
some lines, add lines, remove lines, or add new files with new things. Just work as normal,
and commit and push your changes and see what happens.</p>
<p>So let’s commit and push. You can do it from RStudio or from the command line/Git Bash.
This is what I’ll be doing from now on, but feel free to continue using Git through RStudio:</p>
<div class="sourceCode" id="cb43"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb43-1"><a href="git.html#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> add . <span class="co"># This adds every file I've changed to this next commit</span></span>
<span id="cb43-2"><a href="git.html#cb43-2" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> commit <span class="at">-am</span> <span class="st">"Remade plot with ggplot2"</span> <span class="co"># git commit is the command to create the commit. The -am flag means: 'a' stands for all, as in 'adding all files to the commit', so it's actually redundant with the previous line, but I use it out of habit, and 'm' specifies that we want to add a message</span></span>
<span id="cb43-3"><a href="git.html#cb43-3" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> push origin main <span class="co"># This pushes the commit to the repository on github.com</span></span></code></pre></div>
<p>And this is what happens:</p>
<pre><code>➤ git push origin main
To github.com:b-rodrigues/example_repo.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:b-rodrigues/example_repo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.</code></pre>
<p>What this all means is that David already pushed some changes while I was working on the
project as well. It says so very cleary <em>Updates were rejected because the remote contains
work that you do not have locally.</em>
Git tells us that we first need to pull (download, if you will) the changes to our own computer
to integrate the changes, and then we can push again.</p>
<p>At this point, if we want, we can first go to github.com and see the commit history there to
see what David did. Go to your repo, and click on the commit history icon:</p>
<div style="text-align:center;">
<p><img src="img/github_27.png" width="892" /></p>
</div>
<p>Doing so will list the commit history, as currently on github.com:</p>
<div style="text-align:center;">
<p><img src="img/github_28.png" width="910" /></p>
</div>
<p>While I was working, David pushed 2 commits to the repository. If you compare to your local history,
using <code>git log</code> you will see that these commits are not there, but instead, however many
commits you did (this will not be the case for all of you; whoever of you pushed first will not
see any difference between the local and remote repository). Let’s see how it looks for me:</p>
<div class="sourceCode" id="cb45"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb45-1"><a href="git.html#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> log</span></code></pre></div>
<pre><code>commit d2ab909fc679a5661fc3c49c7ac549a2764c539e (HEAD -> main)
Author: Bruno Rodrigues <bruno@brodrigues.co>
Date: Tue Oct 18 09:28:10 2022 +0200
Remade plot with ggplot2
commit e66c68cc8b58831004d1c9433b2223503d718e1c (origin/main, origin/HEAD)
Author: Bruno Rodrigues <bruno@brodrigues.co>
Date: Mon Oct 17 17:33:33 2022 +0200
Revert "added some more lines"
This reverts commit bd7daf0dafb12c0a19ba65f85b54834a02f7d150.
commit bd7daf0dafb12c0a19ba65f85b54834a02f7d150
Author: Bruno Rodrigues <bruno@brodrigues.co>
Date: Mon Oct 17 14:38:59 2022 +0200
added some more lines
commit 95c26ed4dffd8fc40503f25ddc11af7de5c586c0
Author: Bruno Rodrigues <bruno@brodrigues.co>
Date: Sat Oct 15 12:52:43 2022 +0200
Started project: first graph done
commit d9cff70ff71241ed8514cb65d97e669b0bbdf0f6
Author: Bruno Rodrigues <brodriguesco@protonmail.com>
Date: Thu Oct 13 22:12:06 2022 +0200
Create README.md
</code></pre>
<p>Yep, so none of David’s commits in sight. Let me do what Git told me to do: let’s pull,
or download, David’s commits locally:</p>
<div class="sourceCode" id="cb47"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb47-1"><a href="git.html#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> pull <span class="at">--rebase</span> </span></code></pre></div>
<p><code>--rebase</code> is a flag that keeps the commit history linear. There are many different ways
you can pull changes, but for our purposes we can focus on <code>--rebase</code>. The other strategies
are more advanced, and you might want at some point to take a look at them.</p>
<p>Once <code>git pull --rebase</code> is done, we get the following message:</p>
<pre><code>Auto-merging my_script.R
CONFLICT (content): Merge conflict in my_script.R
error: could not apply d2ab909... Remade plot with ggplot2
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply d2ab909... Remade plot with ggplot2
</code></pre>
<p>Once again, it is important to read what Git is telling us. There is a merge conflict
in the <code>my_script.R</code> file. Let’s open it, and see what’s going on:</p>
<div style="text-align:center;">
<p><img src="img/github_29.png" width="648" /></p>
</div>
<p>We can see two things: the lines that David changed in (1), and the lines I’ve added
in (2). This happened because we changed the same lines. Had I added lines instead of
changing lines that were already there, the merge would have happened automatically,
because there would not have been any conflict. In this case however, Git does not know
how to solve the issue: do we keep David’s changes, or mine? Actually, we need to keep both.
I’ll keep my version of plot that uses <code>{ggplot2}</code>, but will also keep what David added:
he replaced the <code>hp</code> variable by <code>cyl</code>, and added a linear regression as well. Since this
seems sensible to me, I will adapt the script in a way that gracefully merges both
contributions. So the file looks like this now:</p>
<div style="text-align:center;">
<p><img src="img/github_30.png" width="620" /></p>
</div>
<p>We can now save, and continue following the hints from Git, namely, adding the changed
file to the next commit and then use <code>git rebase --continue</code>:</p>
<div class="sourceCode" id="cb49"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb49-1"><a href="git.html#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> add my_script.R</span>
<span id="cb49-2"><a href="git.html#cb49-2" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> rebase <span class="at">--continue</span></span></code></pre></div>
<p>This will once again open the editor in your terminal. Simply close it with <code>:q</code>. Let’s now push:</p>
<div class="sourceCode" id="cb50"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb50-1"><a href="git.html#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> push origin main</span></code></pre></div>
<p>and we’re done! Let’s go back to github.com to see the commit history. You can click on the
hash to see the details of how the file changed (you can do so from RStudio as well):</p>
<div style="text-align:center;">
<p><img src="img/github_31.png" width="891" /></p>
</div>
<p>In green, you see lines that were added, and in red, lines that were removed. The lines
where the linear model was defined are not impacted, because David wrote them at the bottom
of the script, and I did not write anything there:</p>
<div style="text-align:center;">
<p><img src="img/github_32.png" width="920" /></p>
</div>
</div>
<div id="branches" class="section level2 hasAnchor" number="4.7">
<h2><span class="header-section-number">4.7</span> Branches<a href="git.html#branches" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>It is possible to create new branches and continue working on these branches without impacting
the code in the main branch. This is useful if you want to experiment and explore new ideas.
The <code>main</code> or <code>master</code> branch can thus be used only to have code that is ready to get shipped
and distributed, while you can keep working on a development branch. Let’s create a branch called
<code>dev</code> by using the <code>git checkout</code> command, and let’s also add the <code>-b</code> flag to immediately switch
to it:</p>
<div class="sourceCode" id="cb51"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb51-1"><a href="git.html#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git checkout <span class="at">-b</span> dev </span>
<span id="cb51-2"><a href="git.html#cb51-2" aria-hidden="true" tabindex="-1"></a><span class="ex">Switched</span> to a new branch <span class="st">'dev'</span></span></code></pre></div>
<p>It is possible to list the existing branches using <code>git branch</code>:</p>
<div class="sourceCode" id="cb52"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb52-1"><a href="git.html#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git branch</span>
<span id="cb52-2"><a href="git.html#cb52-2" aria-hidden="true" tabindex="-1"></a><span class="ex">*</span> dev</span>
<span id="cb52-3"><a href="git.html#cb52-3" aria-hidden="true" tabindex="-1"></a><span class="ex">main</span></span></code></pre></div>
<p>As a little aside, if you’re working inside a terminal instead of RStudio or another GUI
application, it might be a good idea to configure your terminal a little bit to do two things:</p>
<ul>
<li>change the branch you’re currently on</li>
<li>show if some files got changed.</li>
</ul>
<p>If you want to keep it simple, following <a href="https://thucnc.medium.com/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745">this tutorial</a>
should be enough. If you want something more fancy,
use <a href="https://www.pragmaticlinux.com/2022/06/show-the-git-branch-in-your-bash-terminal-prompt/">this other tutorial</a>.
I have not followed either, so I don’t know if they work, but by the looks of it they should, and it should work on both Linux
and macOS I believe. If these don’t work, just google for “showing git branch in terminal”. This is entirely optional, and
you can use <code>git branch</code> to check which branch you’re currently working on.</p>
<p>Ok so now that we are on the <code>dev</code> branch, let’s change the files a little bit. Change some lines, then commit, then add some
new files and commit again. Then push to <code>dev</code> using:</p>
<div class="sourceCode" id="cb53"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb53-1"><a href="git.html#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git push origin dev</span></code></pre></div>
<p>This is what you should see on github.com after all is done:</p>
<p><img src="img/github_33.png" width="822" /></p>
<p>The video below shows you how you can switch between branches and check
the commit history of both:</p>
<div style="text-align:center;">
<p><video width="640" height="480" controls>
<source src="img/github_branches.mp4" type="video/mp4">
</video></p>
</div>
<p>Let’s suppose that we are happy with our experiments on the <code>dev</code> branch, and are
ready to add them to the <code>master</code> or <code>main</code> branch. For this, checkout the main branch:</p>
<div class="sourceCode" id="cb54"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb54-1"><a href="git.html#cb54-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git checkout main</span></code></pre></div>
<p>You can now pull from <code>dev</code>. This will update your local <code>main</code> branch with the changes from <code>dev</code>.
Depending on what changes you introduced, you might need to solve some conflicts. Try to use the <code>rebase</code>
strategy, and then solve the conflict. In my case, the merge didn’t cause an issue:</p>
<div class="sourceCode" id="cb55"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb55-1"><a href="git.html#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git pull origin dev</span>
<span id="cb55-2"><a href="git.html#cb55-2" aria-hidden="true" tabindex="-1"></a><span class="ex">From</span> github.com:b-rodrigues/example_repo</span>
<span id="cb55-3"><a href="git.html#cb55-3" aria-hidden="true" tabindex="-1"></a> <span class="ex">*</span> branch dev <span class="at">-</span><span class="op">></span> FETCH_HEAD</span>
<span id="cb55-4"><a href="git.html#cb55-4" aria-hidden="true" tabindex="-1"></a><span class="ex">Updating</span> a9a417f..8b2f04f</span>
<span id="cb55-5"><a href="git.html#cb55-5" aria-hidden="true" tabindex="-1"></a><span class="ex">Fast-forward</span></span>
<span id="cb55-6"><a href="git.html#cb55-6" aria-hidden="true" tabindex="-1"></a> <span class="ex">my_script.R</span> <span class="kw">|</span> <span class="ex">8</span> +++-----</span>
<span id="cb55-7"><a href="git.html#cb55-7" aria-hidden="true" tabindex="-1"></a> <span class="ex">new_script.R</span> <span class="kw">|</span> <span class="ex">1</span> +</span>
<span id="cb55-8"><a href="git.html#cb55-8" aria-hidden="true" tabindex="-1"></a> <span class="ex">2</span> files changed, 4 insertions<span class="er">(</span><span class="ex">+</span><span class="kw">)</span><span class="ex">,</span> 5 deletions<span class="er">(</span><span class="ex">-</span><span class="kw">)</span></span>
<span id="cb55-9"><a href="git.html#cb55-9" aria-hidden="true" tabindex="-1"></a> <span class="ex">create</span> mode 100644 new_script.R</span></code></pre></div>
<p>Now if you run git status, this is what you’ll see:</p>
<div class="sourceCode" id="cb56"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb56-1"><a href="git.html#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git status</span>
<span id="cb56-2"><a href="git.html#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="ex">On</span> branch main</span>
<span id="cb56-3"><a href="git.html#cb56-3" aria-hidden="true" tabindex="-1"></a><span class="ex">Your</span> branch and <span class="st">'origin/main'</span> have diverged,</span>
<span id="cb56-4"><a href="git.html#cb56-4" aria-hidden="true" tabindex="-1"></a><span class="ex">and</span> have 2 and 2 different commits each, respectively.</span>
<span id="cb56-5"><a href="git.html#cb56-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">(</span><span class="ex">use</span> <span class="st">"git pull"</span> to merge the remote branch into yours<span class="kw">)</span></span></code></pre></div>
<p>Now, remember that I’ve pulled from <code>dev</code> into <code>main</code>. But <code>git status</code> complains that the remote
<code>main</code> and local <code>main</code> branches have diverged. In these situations, git suggests to pull. This time
we’re pulling from <code>main</code>:</p>
<div class="sourceCode" id="cb57"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb57-1"><a href="git.html#cb57-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git pull</span></code></pre></div>
<p>This will likely result in the following message:</p>
<div class="sourceCode" id="cb58"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb58-1"><a href="git.html#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> You have divergent branches and need to specify how to reconcile them.</span>
<span id="cb58-2"><a href="git.html#cb58-2" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> You can do so by running one of the following commands sometime before</span>
<span id="cb58-3"><a href="git.html#cb58-3" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> your next pull:</span>
<span id="cb58-4"><a href="git.html#cb58-4" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> </span>
<span id="cb58-5"><a href="git.html#cb58-5" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> git config pull.rebase false <span class="co"># merge</span></span>
<span id="cb58-6"><a href="git.html#cb58-6" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> git config pull.rebase true <span class="co"># rebase</span></span>
<span id="cb58-7"><a href="git.html#cb58-7" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> git config pull.ff only <span class="co"># fast-forward only</span></span>
<span id="cb58-8"><a href="git.html#cb58-8" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> </span>
<span id="cb58-9"><a href="git.html#cb58-9" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> You can replace <span class="st">"git config"</span> with <span class="st">"git config --global"</span> to set a default</span>
<span id="cb58-10"><a href="git.html#cb58-10" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> preference for all repositories. You can also pass <span class="at">--rebase,</span> <span class="at">--no-rebase,</span></span>
<span id="cb58-11"><a href="git.html#cb58-11" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> or <span class="at">--ff-only</span> on the command line to override the configured default per</span>
<span id="cb58-12"><a href="git.html#cb58-12" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> invocation.</span>
<span id="cb58-13"><a href="git.html#cb58-13" aria-hidden="true" tabindex="-1"></a><span class="ex">fatal:</span> Need to specify how to reconcile divergent branches.</span></code></pre></div>
<p>Because there are conflicts, I need to specify how the pulling should be done. For this,
I’m using once again the <code>rebase</code> flag:</p>
<div class="sourceCode" id="cb59"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb59-1"><a href="git.html#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git pull <span class="at">--rebase</span></span>
<span id="cb59-2"><a href="git.html#cb59-2" aria-hidden="true" tabindex="-1"></a><span class="ex">Auto-merging</span> my_script.R</span>
<span id="cb59-3"><a href="git.html#cb59-3" aria-hidden="true" tabindex="-1"></a><span class="ex">CONFLICT</span> <span class="er">(</span><span class="ex">content</span><span class="kw">)</span><span class="bu">:</span> Merge conflict in my_script.R</span>
<span id="cb59-4"><a href="git.html#cb59-4" aria-hidden="true" tabindex="-1"></a><span class="ex">error:</span> could not apply b240566... lm <span class="at">-</span><span class="op">></span> rf</span>
<span id="cb59-5"><a href="git.html#cb59-5" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> Resolve all conflicts manually, mark them as resolved with</span>
<span id="cb59-6"><a href="git.html#cb59-6" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> <span class="st">"git add/rm <conflicted_files>"</span>, then run <span class="st">"git rebase --continue"</span>.</span>
<span id="cb59-7"><a href="git.html#cb59-7" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> You can instead skip this commit: run <span class="st">"git rebase --skip"</span>.</span>
<span id="cb59-8"><a href="git.html#cb59-8" aria-hidden="true" tabindex="-1"></a><span class="ex">hint:</span> To abort and get back to the state before <span class="st">"git rebase"</span>, run <span class="st">"git rebase --abort"</span>.</span>
<span id="cb59-9"><a href="git.html#cb59-9" aria-hidden="true" tabindex="-1"></a><span class="ex">Could</span> not apply b240566... lm <span class="at">-</span><span class="op">></span> rf</span></code></pre></div>
<p>So now I have conflicts. This is how the <code>my_script.R</code> file looks like:</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="git.html#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb60-2"><a href="git.html#cb60-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(randomForest)</span>
<span id="cb60-3"><a href="git.html#cb60-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-4"><a href="git.html#cb60-4" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(mtcars)</span>
<span id="cb60-5"><a href="git.html#cb60-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-6"><a href="git.html#cb60-6" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> mtcars) <span class="sc">+</span></span>
<span id="cb60-7"><a href="git.html#cb60-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">y =</span> cyl, <span class="at">x =</span> mpg))</span>
<span id="cb60-8"><a href="git.html#cb60-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-9"><a href="git.html#cb60-9" aria-hidden="true" tabindex="-1"></a>rf <span class="ot"><-</span> <span class="fu">randomForest</span>(hp <span class="sc">~</span> mpg, <span class="at">data =</span> mtcars)</span>
<span id="cb60-10"><a href="git.html#cb60-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-11"><a href="git.html#cb60-11" aria-hidden="true" tabindex="-1"></a><span class="sc"><</span><span class="er"><<<<<<</span> HEAD</span>
<span id="cb60-12"><a href="git.html#cb60-12" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(iris)</span>
<span id="cb60-13"><a href="git.html#cb60-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-14"><a href="git.html#cb60-14" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(iris)</span>
<span id="cb60-15"><a href="git.html#cb60-15" aria-hidden="true" tabindex="-1"></a><span class="sc">==</span><span class="er">=====</span></span>
<span id="cb60-16"><a href="git.html#cb60-16" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(rf)</span>
<span id="cb60-17"><a href="git.html#cb60-17" aria-hidden="true" tabindex="-1"></a><span class="sc">></span><span class="er">>>>>>></span> <span class="fu">b240566</span> (lm <span class="ot">-></span> rf)</span></code></pre></div>
<p>I need to solve the conflicts, and will do so by keeping the following lines:</p>
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb61-1"><a href="git.html#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb61-2"><a href="git.html#cb61-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(randomForest)</span>
<span id="cb61-3"><a href="git.html#cb61-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-4"><a href="git.html#cb61-4" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(mtcars)</span>
<span id="cb61-5"><a href="git.html#cb61-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-6"><a href="git.html#cb61-6" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> mtcars) <span class="sc">+</span></span>
<span id="cb61-7"><a href="git.html#cb61-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">y =</span> cyl, <span class="at">x =</span> mpg))</span>
<span id="cb61-8"><a href="git.html#cb61-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-9"><a href="git.html#cb61-9" aria-hidden="true" tabindex="-1"></a>rf <span class="ot"><-</span> <span class="fu">randomForest</span>(hp <span class="sc">~</span> mpg, <span class="at">data =</span> mtcars)</span>
<span id="cb61-10"><a href="git.html#cb61-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-11"><a href="git.html#cb61-11" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(rf)</span></code></pre></div>
<p>Let’s save the script, and call <code>git rebase --continue</code>. You might see something like this:</p>
<div class="sourceCode" id="cb62"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb62-1"><a href="git.html#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git rebase <span class="at">--continue</span></span>
<span id="cb62-2"><a href="git.html#cb62-2" aria-hidden="true" tabindex="-1"></a><span class="ex">[detached</span> HEAD 929f4ab] lm <span class="at">-</span><span class="op">></span> rf</span>
<span id="cb62-3"><a href="git.html#cb62-3" aria-hidden="true" tabindex="-1"></a> <span class="ex">1</span> file changed, 4 insertions<span class="er">(</span><span class="ex">+</span><span class="kw">)</span><span class="ex">,</span> 9 deletions<span class="er">(</span><span class="ex">-</span><span class="kw">)</span></span>
<span id="cb62-4"><a href="git.html#cb62-4" aria-hidden="true" tabindex="-1"></a><span class="ex">Auto-merging</span> new_script.R</span>
<span id="cb62-5"><a href="git.html#cb62-5" aria-hidden="true" tabindex="-1"></a><span class="ex">CONFLICT</span> <span class="er">(</span><span class="ex">add/add</span><span class="kw">)</span><span class="bu">:</span> Merge conflict in new_script.R</span>
<span id="cb62-6"><a href="git.html#cb62-6" aria-hidden="true" tabindex="-1"></a><span class="ex">error:</span> could not apply 8b2f04f... new file</span></code></pre></div>
<p>There’s another conflict: this time, this is because of the commit <code>8b2f04f</code>, where I added a new file.
This one is easy to solve: I simply want to keep this file, so I simply keep track of it with
<code>git add new_script.R</code> and then, once again, call <code>git rebase --continue</code>:</p>
<div class="sourceCode" id="cb63"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb63-1"><a href="git.html#cb63-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git rebase <span class="at">--continue</span></span>
<span id="cb63-2"><a href="git.html#cb63-2" aria-hidden="true" tabindex="-1"></a><span class="ex">[detached</span> HEAD 20c04f8] new file</span>
<span id="cb63-3"><a href="git.html#cb63-3" aria-hidden="true" tabindex="-1"></a> <span class="ex">1</span> file changed, 4 insertions<span class="er">(</span><span class="ex">+</span><span class="kw">)</span></span>
<span id="cb63-4"><a href="git.html#cb63-4" aria-hidden="true" tabindex="-1"></a><span class="ex">Successfully</span> rebased and updated refs/heads/main.</span></code></pre></div>
<p>I’m now done and can push to main:</p>
<div class="sourceCode" id="cb64"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb64-1"><a href="git.html#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git push origin main</span>
<span id="cb64-2"><a href="git.html#cb64-2" aria-hidden="true" tabindex="-1"></a><span class="ex">Enumerating</span> objects: 9, done.</span>
<span id="cb64-3"><a href="git.html#cb64-3" aria-hidden="true" tabindex="-1"></a><span class="ex">Counting</span> objects: 100% <span class="er">(</span><span class="ex">9/9</span><span class="kw">)</span><span class="ex">,</span> done.</span>
<span id="cb64-4"><a href="git.html#cb64-4" aria-hidden="true" tabindex="-1"></a><span class="ex">Delta</span> compression using up to 12 threads</span>
<span id="cb64-5"><a href="git.html#cb64-5" aria-hidden="true" tabindex="-1"></a><span class="ex">Compressing</span> objects: 100% <span class="er">(</span><span class="ex">6/6</span><span class="kw">)</span><span class="ex">,</span> done.</span>
<span id="cb64-6"><a href="git.html#cb64-6" aria-hidden="true" tabindex="-1"></a><span class="ex">Writing</span> objects: 100% <span class="er">(</span><span class="ex">6/6</span><span class="kw">)</span><span class="ex">,</span> 660 bytes <span class="kw">|</span> <span class="ex">660.00</span> KiB/s, done.</span>
<span id="cb64-7"><a href="git.html#cb64-7" aria-hidden="true" tabindex="-1"></a><span class="ex">Total</span> 6 <span class="er">(</span><span class="ex">delta</span> 2<span class="kw">)</span><span class="ex">,</span> reused 0 <span class="er">(</span><span class="ex">delta</span> 0<span class="kw">)</span><span class="ex">,</span> pack-reused 0</span>
<span id="cb64-8"><a href="git.html#cb64-8" aria-hidden="true" tabindex="-1"></a><span class="ex">remote:</span> Resolving deltas: 100% <span class="er">(</span><span class="ex">2/2</span><span class="kw">)</span><span class="ex">,</span> completed with 1 local object.</span>
<span id="cb64-9"><a href="git.html#cb64-9" aria-hidden="true" tabindex="-1"></a><span class="ex">To</span> github.com:b-rodrigues/example_repo.git</span>
<span id="cb64-10"><a href="git.html#cb64-10" aria-hidden="true" tabindex="-1"></a> <span class="ex">83691c2..20c04f8</span> main <span class="at">-</span><span class="op">></span> main</span></code></pre></div>
<p>There are other ways to achieve this. So let’s go back to <code>dev</code> and continue working:</p>
<div class="sourceCode" id="cb65"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb65-1"><a href="git.html#cb65-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git checkout dev</span></code></pre></div>
<p>Add some lines to <code>my_script.R</code> and then commit and push:</p>
<div class="sourceCode" id="cb66"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb66-1"><a href="git.html#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git add .</span>
<span id="cb66-2"><a href="git.html#cb66-2" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git commit <span class="at">-am</span> <span class="st">"more models"</span></span>
<span id="cb66-3"><a href="git.html#cb66-3" aria-hidden="true" tabindex="-1"></a><span class="ex">[dev</span> a0fa9fa] more models</span>
<span id="cb66-4"><a href="git.html#cb66-4" aria-hidden="true" tabindex="-1"></a> <span class="ex">1</span> file changed, 4 insertions<span class="er">(</span><span class="ex">+</span><span class="kw">)</span></span>
<span id="cb66-5"><a href="git.html#cb66-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb66-6"><a href="git.html#cb66-6" aria-hidden="true" tabindex="-1"></a><span class="ex">➤</span> git push origin dev</span>
<span id="cb66-7"><a href="git.html#cb66-7" aria-hidden="true" tabindex="-1"></a><span class="ex">Enumerating</span> objects: 5, done.</span>
<span id="cb66-8"><a href="git.html#cb66-8" aria-hidden="true" tabindex="-1"></a><span class="ex">Counting</span> objects: 100% <span class="er">(</span><span class="ex">5/5</span><span class="kw">)</span><span class="ex">,</span> done.</span>
<span id="cb66-9"><a href="git.html#cb66-9" aria-hidden="true" tabindex="-1"></a><span class="ex">Delta</span> compression using up to 12 threads</span>
<span id="cb66-10"><a href="git.html#cb66-10" aria-hidden="true" tabindex="-1"></a><span class="ex">Compressing</span> objects: 100% <span class="er">(</span><span class="ex">3/3</span><span class="kw">)</span><span class="ex">,</span> done.</span>