forked from sahirbhatnagar/git4ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
2112 lines (1404 loc) · 43.7 KB
/
index.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: GitHub for Data Scientists without the Terminal
output:
html_document:
toc: true
toc_float: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, tidy = FALSE, cache = FALSE, warning = FALSE,
message = FALSE)
knitr::opts_knit$set(eval.after = 'fig.cap')
```
# Introduction
In this tutorial you will learn how to get started with version control using
[git](https://git-scm.com/) and [GitHub](https://github.com/). The main goal
here is to provide a step-by-step introduction to GitHub, with detailed
screenshots, so that you become familiar with its main functionalities.
## Who
This tutorial is intended for grad students and academics who use
[`R`](https://cran.r-project.org/) but are unfamiliar with the _command line_
or _terminal_. I assume nothing about the computer science skills of the user, but do
assume basic knowledge of `R` and `RStudio`.
## What
The outline is provided in the floating table of contents. You will learn the essential
concepts and terminology of version control, Git, GitHub and GitHub desktop. This
tutorial follows a _learn-by-doing_ approach.
## Why
Familiarity with GitHub has become an indispensible tool for anyone working
with data. Sharing code, writing software for your statistical method,
producing techincal reports and creating websites have become essential
skills to have in the rapidly growing field of data science.
Other answers can be found
[here](https://www.quora.com/Should-I-learn-Git-when-I-just-start-programming),
[here](http://stackoverflow.com/questions/2658/getting-started-with-version-control)
and [here](http://stat545.com/git01_git-install.html#but-first-why).
## How
Each of the topics covered are separated by chapters that should be followed
sequentially. Within each chapter, there are a series of steps that you need
to complete. Each step starts with some instructions followed by a screenshot.
## Pre-requisites
Chapters 1-3 have no pre-requisites in terms of software. Chapters 4-8 require a working
installation of [`R`](https://cran.r-project.org/) and
[`RStudio`](https://www.rstudio.com/products/rstudio/download/preview/).
## What this isn't
It is _not_ a comprehensive tutorial of all the intracacies of Git. I skip over
many fine details, because the main goal of this tutorial is an introduction
to essential concepts and terminology of version control, Git, and GitHub.
It covers a variety of topics that could each be its own book. There are a plethora of
online resources available for everything covered here but you can't Google something
if you don't know what you're looking for in the first place.
## Related Work
There are several more advanced and comprehensive online resources available for
learning git and github for data science oriented people including:
- [Stat 545 at UBC by Jenny Bryan](http://stat545.com/git00_index.html)
- [Advanced R by Hadley Wickham](http://r-pkgs.had.co.nz/git.html)
The main difference here is that we don't use the terminal (or command line) and
provide screenshots for every step.
*************
*************
*************
# Chapter 1: Installing Git {.tabset .tabset-fade .tabset-pills}
*************
## Introduction
Git is to GitHub, what R is to RStudio. In other words Git is the software that
does all the work behind the scenes, and GitHub a user interface that makes its
easier to communicate with Git (and adds functionality as well). In this chapter
we will download and install Git.
_Note: the screenshots provided here are from a Windows operating system, however it will be similar on a Mac or Linux._
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
## Step 1
[Download Git](https://git-scm.com/downloads)
*************

*************
*************
*************
*************
*************
## Step 2
Once the download has completed, click on the `Git-2.7.4 64-bit.exe` file
(`.dmg` on a Mac, or `.deb` on Linux). _Note: the version you download might be different than what I've shown here, but that's ok_
*************

*************
*************
*************
*************
*************
## Step 3
Once you have read the GNU General Public License (this is not required
to continue) click on `Next`.
*************

*************
*************
*************
*************
*************
## Step 4
You need to select where you want Git installed. I have chosen the default
location `Program Files`, but you can change this if you like by clicking on
the `Browse...` button. Once you have chosen a location click `Next`.
*************

*************
*************
*************
*************
*************
## Step 5
Select the components you want to install. Ensure that at least the boxes
shown in the screenshot below have been checked. Click `Next`.
*************

*************
*************
*************
*************
*************
## Step 6
This step is to select where you want the shortcut location to be stored. I
have chosen the default. Then click `Next`.
*************

*************
*************
*************
*************
*************
## Step 7
Git can be used from the command line also. Selecting the second option allows
you this flexibility for when you become familiar with Git.
_Note: you might see different options on a Mac, if you don't know which option to choose, select the default_
*************

*************
*************
*************
*************
*************
## Step 8
Select the (recommended) first option and click `Next`.
_Note: you might see different options on a Mac, if you don't know which option to choose, select the default_
*************

*************
*************
*************
*************
*************
## Step 9
Select the (recommended) first option and click `Next`.
_Note: you might see different options on a Mac, if you don't know which option to choose, select the default_
*************

*************
*************
*************
*************
*************
## Step 10
Ensure that at least the `Enable Git Credential Manager` box is checked, and click `Next`.
*************

*************
*************
*************
*************
*************
## Step 11
You should see now see the following installation screen.
*************

*************
*************
*************
*************
*************
## Step 12
The following screen will appear once the Git setup has successfully completed.
Click on `Finish`. Well done, you have installed Git on your system. Proceed
to Chapter 2 to signup for a GitHub account.
*************

*************
*************
*************
*************
*************
# Chapter 2: Signup for a GitHub account and `hello world` tutorial {.tabset .tabset-fade .tabset-pills}
*************
## Introduction
In this short Chapter, you will signup for a GitHub account. GitHub is like
your online portfolio of code. It has a plethora of great features for creating
websites, project pages and collaborating with others. Again GitHub is an
interface to the version control system called Git. Other options
include [Bitbucket](https://bitbucket.org/) and [GitLab](https://about.gitlab.com/).
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
## Step 1
Go to [https://github.com/](https://github.com/).
*************

*************
*************
*************
*************
*************
## Step 2
The longest step in this chapter is choosing your username. Think about it
carefully; it will be how you are recognized on GitHub i.e., your github website
address will be github.com/username. Once you have chosen a username, enter a
valid email address and password, and click on the `Sign up for GitHub` button.
*************

*************
*************
*************
*************
*************
## Step 3
Choose the free plan (default) and click on the `Finish sign up` button.
*************

*************
*************
*************
*************
*************
## Step 4
Well done. You now have a GitHub account. Complete the `Hello World` guide
which will walk you through some functionalities of GitHub. Click on
the `Let's get started!` button.
*************

*************
*************
*************
*************
*************
## Step 5
Complete the exercises and move on to Chapter 3: Installing GitHub Desktop.
*************

*************
*************
*************
*************
*************
# Chapter 3: Installing GitHub Desktop {.tabset .tabset-fade .tabset-pills}
*************
## Introduction
Traditionally, version control with Git is accessed through the command line or
terminal. GitHub Desktop is a software program that makes it easier to use Git
functions without having to use the command line. It also allows you to
communicate with your GitHub website (github.com/username). Don't worry if the
differences between Git, GitHub and GitHub Desktop are not clear to you yet.
You will have a better understanding once you have completed this tutorial.
_Note: in all the screenshots that follow, my username is shown, however you should be entering your username, password and email address created in Chapter 2._
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
## Step 1
Go to [https://desktop.github.com/](https://desktop.github.com/) and click
on `Download GitHub Desktop`.
_Note: GitHub desktop is only available for Windows and Mac. If you are running Linux I recommend [GitKraken](https://www.gitkraken.com/)._
*************

*************
*************
*************
*************
*************
## Step 2
Once the program has finished downloading, click on `GitHubSetup.exe` (or `.dmg` on a Mac).
*************

*************
*************
*************
*************
*************
## Step 3
Click on `Install`.
*************

*************
*************
*************
*************
*************
## Step 4
You should see this installation screen.
*************

*************
*************
*************
*************
*************
## Step 5
Once installed, open up the program and login using the GitHub username and
password you created in Chapter 2 and click on `Log in`.
*************

*************
*************
*************
*************
*************
## Step 6
This information is used to identify the person that made the changes to your
code. Leave the default values and click on `Continue`.
*************

*************
*************
*************
*************
*************
## Step 7
You should see this screen, since you haven't created any local repositories yet.
> What is a repository? The purpose of Git is to manage a project, or a set of files, as they change over time. Git stores this information in a data structure called a repository^[[https://www.sbf5.com/~cduan/technical/git/git-1.shtml](https://www.sbf5.com/~cduan/technical/git/git-1.shtml)].
*************

*************
*************
*************
*************
*************
## Step 8
You should now be at this screen.
*************

*************
*************
*************
*************
*************
## Step 9
Click on the <img src="img/rcode/rcode0.png" alt="Drawing" style="width: 50px;"/> button in the top left corner. Your username should appear
with a list of your repositories that are currently saved in your online GitHub
account. To be able to have a local copy of this repository (by local I mean on
your computer hard drive) click on the `Clone` tab and then
the `Clone hello-world` button (I am assuming that you completed the `Hello World`
tutorial in Step 5 of Chapter 2).
*************

*************
*************
*************
*************
*************
## Step 10
Choose where you want to save a local copy of the `Hello World` repository and click `OK`.
*************

*************
*************
*************
*************
*************
## Step 11
You should now see the following contents in your GitHub desktop program.
*************

*************
*************
*************
*************
*************
## Step 12
Using your computer's file explorer (e.g. windows explorer or mac finder),
locate the local GitHub repository. If you successfully cloned your repository
you will see a `hello-world` folder with a `README.md` file in it, which is
the same one you created during the `Hello World` exercise in Chapter 2.
*************

*************
*************
*************
*************
*************
## Step 13
Before moving on to Chapter 4, verify that the GitHub Desktop has added an
SSH key for you. An SSH key is used to establish a secure connection between
your computer and the online GitHub server. On the far top right hand side of
your online GitHub account click on the <img src="img/desktop/desktop0.png" alt="Drawing" style="width: 50px;"/> icon and navigate to `Settings`.
*************

*************
*************
*************
*************
*************
## Step 14
You should see one entry in the `SSH keys` panel. Well done. You are now ready
to version control some `R` code in Chapter 4.
*************

*************
*************
*************
*************
*************
# Chapter 4: Version control `R` code using an example of PCA {.tabset .tabset-fade .tabset-pills}
*************
## Introduction
In this chapter we will learn how to version control `R` code using an example
of Principal Component Analysis.
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
*************
## Step 1
Create a local (meaning on your computer) repository by clickling the <img src="img/rcode/rcode0.png" alt="Drawing" style="width: 50px;"/>
button in the top left corner of GitHub Desktop, and select the `Create` tab.
Name the repository `pcaCars` and select where you want this repository stored
on your computer. Leave the `Git ignore` value at its default (we will ignore what
this is for now). Click on <img src="img/rcode/rcode0000.png" alt="Drawing" style="width: 125px;"/>.
*************

*************
*************
*************
*************
*************
## Step 2
You should see the following in your GitHub Desktop. A repository called
`pcaCars` has been created locally on your computer, and it contains two
text files that were automatically created by the software. You can click on them
to see their contents. The most important of the two is the `.gitignore` file.
This text file allows you to control what you want to version control within the
`pcaCars` repository.
*************

*************
*************
*************
*************
*************
## Step 3
We now want to publish this repository to the remote (i.e. github.com/username).
Simply click on the <img src="img/rcode/rcode00.png" alt="Drawing" style="width: 75px;"/>
button in the top right hand corner. Add a description
and click on <img src="img/rcode/rcode000.png" alt="Drawing" style="width: 125px;"/>.
*************

*************
*************
*************
*************
*************
## Step 4
Head over to your online github account (e.g. [https://github.com/git4ds](https://github.com/git4ds)).
You should see the `pcaCars` repository along with the description you entered in the
previous step.
*************

*************
*************
*************
*************
*************
## Step 5
Click on the `pcaCars` repository and you will see the `.gitattributes` and
`.gitignore` files which are the same ones you have in your local repository.
*************

*************
*************
*************
*************
*************
## Step 6
Open RStudio, navigate to the `pcaCars` repository and set it as your working
directory using the `setwd()` function
*************

*************
*************
*************
*************
*************
## Step 7
Save the following code in an R script called `pca.R`
```{r eval=FALSE}
# cor = TRUE indicates that PCA is performed on
# standardized data (mean = 0, variance = 1)
pcaCars <- princomp(mtcars, cor = TRUE)
# view objects stored in pcaCars
names(pcaCars)
# proportion of variance explained
summary(pcaCars)
# scree plot
plot(pcaCars, type = "l")
```
*************

*************
*************
*************
*************
*************
## Step 8
Go back to GitHub Desktop. You will see the `pca.R` file appear. Click on the
checkbox to the left of it, and you will see all the additions you have made
to the file.
*************

*************
*************
*************
*************
*************
## Step 9
On the bottom left hand side, enter a summary of the changes you have made to
the repository and an (optional) description. Then click on <img src="img/rcode/rcode00000.png" alt="Drawing" style="width: 125px;"/>.
This is essentially telling Git to record the changes you have made and store them
in a branch. We will learn about branches in Chapter 5.
*************

*************
*************
*************
*************
*************
## Step 10
You should see the following screen. You should notice that in the rectangular
black box, underneath the <img src="img/rcode/rcode000000.png" alt="Drawing" style="width: 75px;"/>
button in the top right hand corner of GitHub Desktop,
a timeline. As you commit additional changes, this timeline will grow. Each circle represents
a snapshot of the repository at the time of the commit. This is the power of version controlling with
Git. You can see what changes you have made, and even revert back to snapshot you want.
*************

*************
*************
*************
*************
*************
## Step 11
Go to the `pcaCars` repository in your online GitHub account. Do you see the
file you just created called `pca.R` ? _Why not?_ Because the commit you made was local
to your computer. In order to see these changes online, you must `push` your local
changes to the `remote`.
*************

*************
*************
*************
*************
*************
## Step 12
Go to GitHub Desktop and click on the <img src="img/rcode/rcode000000.png" alt="Drawing" style="width: 75px;"/>
button in the top right hand corner.
*************

*************
*************
*************
*************
*************
## Step 13
You should now see your local changes _pushed_ to your online repository.
*************

*************
*************
*************
*************
*************
## Step 14
Let's make a change to the `pca.R` script. Instead of a scree plot, we want
a bar plot of the variance explained for each component:
```{r, eval=FALSE}
# bar plot
plot(pcaCars)
```
Your script should now match what is shown in the screenshot below.
*************

*************
*************
*************
*************
*************
## Step 15
Go to GitHub Desktop and click on the `pca.R` file. You will see that
Git automatically recognizes the changes you have made. Highlighted in red
is what has been removed from the file, and in green is what was added.
*************

*************
*************
*************
*************
*************
## Step 16
Describe the change you have made and click on <img src="img/rcode/rcode00000.png" alt="Drawing" style="width: 125px;"/>
*************

*************
*************
*************
*************
*************
## Step 17
_Push_ your local changes to the remote repository by clicking on the <img src="img/rcode/rcode000000.png" alt="Drawing" style="width: 75px;"/>
button. You can view the different commits you have made in GitHub Desktop by clicking on
the grey circles in the timeline located in the rectangular black box.
*************
