generated from inSilecoInc/workshop_R_template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_02_basic_git.Rmd
1292 lines (811 loc) · 18.7 KB
/
_02_basic_git.Rmd
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
---
title: "Git Basics"
output:
xaringan::moon_reader:
css: [default, rd.css, rd-font.css, "hygge"]
lib_dir: assets
seal: false
nature:
highlightStyle: dracula
countIncrementalSlides: false
beforeInit: "macros.js"
---
# Git Basics
```{r setup_02, include = FALSE}
source("_setup.R")
# knitr::clean_cache(TRUE)
htmltools::tagList(
xaringanExtra::use_clipboard(
button_text = "<i class=\"fa fa-clipboard\"></i>",
success_text = "<i class=\"fa fa-check\" style=\"color: #37abc8\"></i>",
),
rmarkdown::html_dependency_font_awesome()
)
xaringanExtra::use_scribble()
```
---
# Prerequisite: `r gt()`
## `r gt()` installed
```{bash}
git --version
```
---
# Prerequisite: a terminal `r lc()`
### All users
- [R Studio](https://support.rstudio.com/hc/en-us/articles/115010737148-Using-the-RStudio-Terminal-in-the-RStudio-IDE)
--
### Windows
- [Cygwin](https://www.cygwin.com/) / [Windows 10 bash shell](https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/)
--
### Perso
- I work on [Debian](https://www.debian.org/) / my Desktop environment is [Gnome](https://www.gnome.org/)
- I use [gnome-terminal](https://help.gnome.org/users/gnome-terminal/stable/) with [Z shell](https://en.wikipedia.org/wiki/Z_shell) and a configuration framework named [prezto](https://github.com/sorin-ionescu/prezto)
---
class: inverse, center, middle
# Your local `r gt()` repo
![:custom_hr]()
## Set your local `r gt()` repository up.
---
# Initiate your repository: `git init`
```sh
$ mkdir proj1
$ cd proj1
$ git init
#> Initialized empty Git repository in /path/to/proj1/.git/
```
---
# Initiate your repository: `git init`
## What just happened?
### The database `.git` has been created
```sh
$ tree .git
#> .git
#> ├── config
#> ├── description
#> ├── HEAD
#> ├── hooks
#> │ └── README.sample
#> ├── info
#> │ └── exclude
#> ├── objects
#> │ ├── info
#> │ └── pack
#> └── refs
#> ├── heads
#> └── tags
```
---
# Inspect your repository: `git status`
## Use `git status`!
--
```sh
$ git status
#> On branch main
#> No commits yet
#> nothing to commit (create/copy files and use "git add" to track)
```
--
- `r nf()` `man git status`, `git status --help`, `git status -h`
---
# `r lc()` Let's do this
## Initiate a repo and check it.
---
class: inverse, center, middle
# Basic workflow
![:custom_hr]()
---
# Basic workflow
### 1. Initiate the repository (`git init`)
--
### 2. Edit your files
--
### 3. Stage the modifications to be committed (`git add`)
--
### 4. Create a new commit object (`git commit`)
--
### 5. Go back to step 2.
---
# Edit a file and check the repo
```sh
$ echo "# Our great project" > README.md
```
```sh
$ git status
#> On branch main
#> No commits yet
#> Untracked files:
#> (use "git add <file>..." to include in what will be committed)
#> README.md
#> nothing added to commit but untracked files present (use "git add" to track)
```
--
```sh
$ git status -s
#> ?? README.md
```
- `r nf()` ` git status -s` = `git status --short`
---
# Track a file: `git add`
- See https://github.com/git-guides/git-add
```sh
$ git add README.md
```
--
<br>
## README.md `r ar()` Staging area
--
### README.md `untracked` `r ar()` README.md `staged`
---
# Track a file: `git add`
```sh
$ git status
#> On branch main
#>
#> No commits yet
#>
#> Changes to be committed:
#> (use "git rm --cached <file>..." to unstage)
#> new file: README.md
```
--
```sh
$ git status -s
#> A README.md
```
---
# What happened?
- `README.md` has been added to the staging area (a.k.a. "the index");
- The file `README.md` is now a *blob* in `.git`;
- To ensure integrity, `r gt()` has computed a [SHA-1](https://en.wikipedia.org/wiki/SHA-1) for it;
--
```sh
git ls-tree -r HEAD
#> 100644 blob 1f18e72276e6dda33cca0065b1f8dd377c72c8c6 README.md
```
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_1.png)]
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_2.png)]
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_3.png)]
---
# Edit and check again
```sh
$ echo "This is a great project" >> README.md
```
--
### 1. In staging area: **"#Our great project"** ready to be committed
### 2. Workspace: README.md is modified **"+ This is a great project"**
---
# Edit and check again
```sh
$ git status
#> On branch main
#>
#> No commits yet
#>
#> Changes to be committed:
#> (use "git rm --cached <file>..." to unstage)
#> new file: README.md
#>
#> Changes not staged for commit:
#> (use "git add <file>..." to update what will be committed)
#> (use "git restore <file>..." to discard changes in working directory)
#> modified: README.md
```
```sh
$ git status -s
AM README.md
```
---
# `r lc()` Let's do this
## Edit and check a file.
---
# Create a first commit: `git commit`
```sh
$ git add README.md
```
--
```sh
$ git commit -m "Add our README"
#> [main (root-commit) 618b684] Add our README
#> 1 file changed, 2 insertions(+)
#> create mode 100644 README.md
```
--
```sh
$ git status
#> On branch main
#> nothing to commit, working tree clean
```
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_3.png)]
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_4.png)]
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_5.png)]
---
# `r lc()` Let's do this
## Create a commit.
---
# Initiate, stage & commit with `r rp()`
- Use package [`gert`](https://CRAN.R-project.org/package=gert)
```{R init, eval = FALSE}
if (!file.exists("proj1")) dir.create("proj1")
setwd("proj1")
library(gert)
git_init()
writeLines("# Our great project\n This is a great project", "README.md")
git_add("README.md")
git_status()
git_commit("Add our README")
```
---
# Initiate your repository with `r rp()`
## With `gert`
- `git init` `r ar()` `git_init()`
- `git add` `r ar()` `git_status()`
- `git commit` `r ar()` `git_commit()`
## Use `r gt()` from `r rp()`
- `system()` and `system2()` to pass any command
---
# One more commit
```sh
$ echo "seq_nrow <- function(x) seq_len(NROW(x))" > seq_nrow.R
$ git add seq_nrow.R
$ git commit -m "Add seq_nrow()"
#> [main dd28a77] Add seq_nrow()
#> 1 file changed, 1 insertion(+)
#> create mode 100644 seq_nrow.R
```
???
more than one file at the time.
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_6.png)]
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_7.png)]
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_8.png)]
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_9a.png)]
---
# How `r gt()` works
.center[![:scale 92%](img/part2/ct_9b.png)]
---
# One more commit
```sh
$ echo "Cool seq_nrow() function" >> README.md
$ git status -s
#> M README.md
$ git add README.md
$ git commit -m "Improve the documentation"
#> [main 58d5363] Improve the documentation
#> 1 file changed, 1 insertion(+)
```
???
do this one with commit without -m
and explain that you can use an editor
---
# `r lc()` Let's do this
## Create two more commits.
## Do not use option `-m` for the second one and add a body.
---
# How to create a good commit?
### Hard to say!
--
### Some rules of thumb!
- 1 commit = one goal
- 1 commit = a digestible amount of code (no minimal amount of code)
---
# How to write a good commit message?
--
### It may depend on the project!
--
### Good habits
- https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
- https://cbea.ms/git-commit/
- Separate subject from body with a blank line
- Limit the subject line to 50 characters
- Capitalize the subject line
- Do not end the subject line with a period
- Use the imperative mood in the subject line
- Wrap the body at 72 characters
- Use the body to explain what and why vs. how
- https://gitmoji.dev/
???
may be one more commit with body
---
# Basic workflow
- https://ndpsoftware.com/git-cheatsheet.html#loc=workspace
---
class: inverse, center, middle
# Navigate your history
![:custom_hr]()
log and checkout/switch
---
# Check the history `git log`
## Use `git log`! (see [Pro Git](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History))
```sh
$ git log
#> commit 8c43aea1ba20ddaccd3c469b7928f532d88275fc (HEAD -> main)
#> Author: Kevin Cazelles <[email protected]>
#> Date: Tue Jan 18 21:07:36 2022 -0500
#>
#> Improve the documentation
#>
#> commit 7ab0cfb54a54bd732991a4998be0c36d84021452
#> Author: Kevin Cazelles <[email protected]>
#> Date: Tue Jan 18 21:06:57 2022 -0500
#>
#> Add seq_nrow()
#>
#> commit cbb5a9e2aafb7ad16a8e5dc8f682718946d83292
#> Author: Kevin Cazelles <[email protected]>
#> Date: Tue Jan 18 21:06:30 2022 -0500
#>
#> Add our README
```
???
a lot of options
---
# Check the history `git log`
## `git log` has a lot of options
```sh
$ git log --oneline
#> 8c43aea (HEAD -> main) Improve the documentation
#> 7ab0cfb Add seq_nrow()
#> cbb5a9e Add our README
```
--
```sh
$ git log --pretty=format:"%h - %an, %ar : %s"
#> 8c43aea - Kevin Cazelles, 3 minutes ago : Improve the documentation
#> 7ab0cfb - Kevin Cazelles, 4 minutes ago : Add seq_nrow()
#> cbb5a9e - Kevin Cazelles, 4 minutes ago : Add our README
```
- see [Pro Git](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History)
---
# Navigate your history
```sh
$ git log --oneline
#> 8c43aea (HEAD -> main) Improve the documentation
#> 7ab0cfb Add seq_nrow()
#> cbb5a9e Add our README
```
```sh
$ tree
#> .
#> ├── README.md
#> └── seq_nrow.R
```
---
# Navigate your history: `git checkout`
```sh
$ git checkout cbb5a9e
#> Note: switching to 'cbb5a9e'.
#>
#> 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 cbb5a9e Add our README
```
---
# Navigate your history
```sh
$ tree
#> .
#> └── README.md
```
```sh
$ git status
#> HEAD detached at cbb5a9e
#> nothing to commit, working tree clean
```
---
# Navigate your history: `git switch`
```sh
git switch -
```
--
<br>
## `git switch` or `git checkout`?
--
### `git checkout` does a lot! v2.23 introduces `git switch` and `git restore` to have aliases of subsets of what it does (see e.g. [this link](https://tanzu.vmware.com/developer/blog/git-switch-and-restore-an-improved-user-experience/))
---
# Navigate your history: `git switch`
```sh
$ git checkout cbb5a9e
```
or (and **preferred**)
```sh
$ git switch -d cbb5a9e
```
or
```sh
$ git checkout HEAD~2
```
or
```sh
$ git switch -d HEAD~2
```
- https://stackoverflow.com/questions/2221658/whats-the-difference-between-head-and-head-in-git
---
# What happened?
.center[![:scale 100%](img/part2/sp_4.png)]
---
# What happened?
.center[![:scale 100%](img/part2/sp_6.png)]
---
# `r lc()` Let's do this
## Navigate our history.
---
# What happened?
```sh
git switch -d cbb5a9e
```
--
- `r gt()` has pulled out the snapshot from `.git`.
- The HEAD is now detached:
- you can still commit but it won't replace the commits you've done;
- branch will be on a different commit history;
- so far it won't have any impact and won't really be part of your work;
- if want to include it in your "regular" history, you can branch.
???
good transition
---
class: inverse, center, middle
# Branching
![:custom_hr]()
## `r gt()` killer feature!
???
Any question so far
---
# Branching
## 1. Branch, branch and branch!
--
## 2. Use `git branch` a lot!
<br>
--
### Branching allows you to create divergent history ...
--
### ... which is exactly what you need for collaboration.
---
# Branching
```sh
$ git branch topicA
$ git switch topicA
#> Switched to branch 'topicA'
```
--
### with one line
```sh
$ git switch -c topicA
#> Switched to branch 'topicA'
```
---
# Branching
```sh
$ git status
#> On branch topicA
#> nothing to commit, working tree clean
```
--
```sh
$ git log --oneline
#> cb1eba0 (HEAD -> topicA, main) Improve the documentation
#> 630c33e Add seq_nrow()
#> 0f2ea9b Add our README
```
---
# A commit on the new branch
```sh
$ echo "seq_ncol <- function(x) seq_len(NCOL(x))" > seq_ncol.R
$ git add seq_ncol.R
$ git commit -m "Add seq_ncol()"
```
---
# A commit on the new branch
```sh
$ git log --oneline
#> 97832ed (HEAD -> topicA) Add seq_ncol()
#> cb1eba0 (main) Improve the documentation
#> 630c33e Add seq_nrow()
#> 0f2ea9b Add our README
```
---
# What happened?
### A new pointer, `topicA`, has been created.
--
### The `HEAD` now points to `topicA`.
--
### A new commit has been created, its parent is the last commit on `main`.
---
# How `r gt()` works
.center[![:scale 90%](img/part2/com_1.png)]
---
# How `r gt()` works
.center[![:scale 90%](img/part2/com_2.png)]
---
# How `r gt()` works
.center[![:scale 90%](img/part2/com_3.png)]
---
# How `r gt()` works
.center[![:scale 90%](img/part2/com_4.png)]
---
# How `r gt()` works
.center[![:scale 90%](img/part2/com_5.png)]
---
# How `r gt()` works
.center[![:scale 90%](img/part2/br_1.png)]
---
# How `r gt()` works - branch
.center[![:scale 90%](img/part2/br_2.png)]
---
# How `r gt()` works - switch
.center[![:scale 90%](img/part2/br_3.png)]
---
# How `r gt()` works - commit
.center[![:scale 90%](img/part2/br_4.png)]
---
# Merging `git merge`
```sh
$ git switch main
$ git merge topicA
#> Updating 64ee016..aadf541
#> Fast-forward
#> seq_ncol.R | 1 +
#> 1 file changed, 1 insertion(+)
#> create mode 100644 seq_ncol.R
```
---
# How `r gt()` works - commit
.center[![:scale 90%](img/part2/br_4.png)]
---
# How `r gt()` works - switch
.center[![:scale 90%](img/part2/br_5.png)]
---
# How `r gt()` works - merge (fast forward)
.center[![:scale 90%](img/part2/br_6.png)]
???
now let's use references (refs) for pointers and objects for objets
---
# Not always as simple!
.center[![:scale 90%](img/part2/br2_1.png)]
---
# How `r gt()` works - commit
.center[![:scale 90%](img/part2/br2_2.png)]
---
# How `r gt()` works - merge
.center[![:scale 90%](img/part2/br2_3.png)]
---
# Merge commit
```sh
$ git switch -d HEAD~2
$ git switch -c topicB
```
--
```sh
$ echo "one more line" >> README.md
$ git add README.md
$ git commit -m "Improve documentation"
```
--
```sh
$ git switch main
```
--
```sh
$ git merge topicB
#> Auto-merging README.md
#> CONFLICT (content): Merge conflict in README.md
#> Automatic merge failed; fix conflicts and then commit the result.
```
### `r tr()` Interactive merge `r tr()`
---
# Interactive merging
```sh
# check what to do
$ git status