-
Notifications
You must be signed in to change notification settings - Fork 1
/
Github_intro.html
1571 lines (1454 loc) · 63.7 KB
/
Github_intro.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>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Version control with Git and GitHub</title>
<script src="site_libs/header-attrs-2.29/header-attrs.js"></script>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/flatly.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/jqueryui-1.13.2/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/font-awesome-6.4.2/css/all.min.css" rel="stylesheet" />
<link href="site_libs/font-awesome-6.4.2/css/v4-shims.min.css" rel="stylesheet" />
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-162377463-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-162377463-1');
</script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<link rel="stylesheet" href="custom.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark the anchor link active (and if it's in a dropdown, also mark that active)
var dropdown = menuAnchor.closest('li.dropdown');
if (window.bootstrap) { // Bootstrap 4+
menuAnchor.addClass('active');
dropdown.find('> .dropdown-toggle').addClass('active');
} else { // Bootstrap 3
menuAnchor.parent().addClass('active');
dropdown.addClass('active');
}
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before, .tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "\e259";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "\e258";
font-family: 'Glyphicons Halflings';
border: none;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
.tocify-subheader {
display: inline;
}
.tocify-subheader .tocify-item {
font-size: 0.95em;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-bs-toggle="collapse" data-target="#navbar" data-bs-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">QUADstatR</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="FAQ.html">
<span class="fa fa-question"></span>
FAQ
</a>
</li>
<li>
<a href="setup.html">
<span class="fa fa-cog"></span>
Setup
</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<span class="fa fa-book"></span>
R Book
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="https://alexd106.github.io/Rbook">
<span class="fa fa-firefox"></span>
Web book
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://github.com/alexd106/Rbook/raw/master/docs/Rbook.pdf">
<span class="fa fa-file-pdf"></span>
PDF book
</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<span class="fa fa-university"></span>
Learn R
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="howto.html">
<span class="fa fa-tv"></span>
How-to videos
</a>
</li>
<li class="divider"></li>
<li>
<a href="lectures.html">
<span class="fa fa-book"></span>
Lecture slides
</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<span class="fa fa-file-contract"></span>
Exercises
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="exercises.html">
<span class="fa fa-folder"></span>
Excercises
</a>
</li>
<li class="divider"></li>
<li>
<a href="exercise_solutions.html">
<span class="fa fa-folder"></span>
Exercise solutions
</a>
</li>
</ul>
</li>
<li>
<a href="data.html">
<span class="fa fa-download"></span>
Data
</a>
</li>
<li>
<a href="Tutorials.html">
<span class="fa fa-desktop"></span>
Tutorials
</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<span class="fa fa-question-circle"></span>
Info
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="syllabus.html">
<span class="fa fa-graduation-cap"></span>
Syllabus
</a>
</li>
<li class="divider"></li>
<li>
<a href="People.html">
<span class="fa fa-user-friends"></span>
People
</a>
</li>
<li class="divider"></li>
<li>
<a href="resources.html">
<span class="fa fa-book"></span>
Resources
</a>
</li>
<li>
<a href="https://forms.gle/T8z1BSUUQiyJ2e8z8">
<span class="fa fa-commenting"></span>
Feedback
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.quadrat.ac.uk/">
<span class="fa fa-chrome"></span>
QUADRAT
</a>
</li>
<li>
<a href="https://superdtp.st-andrews.ac.uk/">
<span class="fa fa-chrome"></span>
SUPER
</a>
</li>
<li>
<a href="People.html">
<span class="fa fa-envelope fa-lg"></span>
Contact
</a>
</li>
<li>
<a href="http://github.com/alexd106">
<span class="fa fa-github fa-lg"></span>
Source code
</a>
</li>
<li>
<a href="https://twitter.com/QUADRATdtp">
<span class="fa fa-twitter fa-lg"></span>
Twitter
</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Version control with Git and GitHub</h1>
</div>
<p> </p>
<p>This short(ish!) tutorial will introduce you to the basics of using a
version control system to keep track of all your important PhD documents
and facilitate collaboration with colleagues and the wider world. The
tutorial will focus on using the software ‘Git’ in combination with the
web-based hosting service ‘GitHub’. By the end of the tutorial, you will
be able to install and configure Git and GitHub on your computer and
setup and work with a version controlled project in RStudio. We won’t be
covering more advanced topics such as branching, forking and pull
requests in much detail but I do give an overview <a
href="#collab">later</a> on in the tutorial.</p>
<p>I estimate that this tutorial should take you roughly 1.5 to 2.5
hours to complete in one sitting (to be honest I have no idea really so
this is just a guess), but feel free to dip in and out over a longer
period if that suits you better.</p>
<p>Just a few notes of caution. In this tutorial we’ll be using RStudio
to interface with Git as it gives you a nice friendly graphical user
interface which generally makes life a little bit easier (and who
doesn’t want that?). However, one downside to using RStudio with Git is
that RStudio only provides pretty basic Git functionality through its
menu system. That’s fine for most of what we’ll be doing during this
tutorial (although I will introduce a few Git commands as we go along)
but if you really want to benefit from using Git’s power you will need
to <a href="#resources">learn</a> some Git commands and syntax. This
leads me on to my next point. I’m not going to lie, Git can become a
little bewildering and frustrating when you first start using it. This
is mostly due to the terminology and liberal use of jargon associated
with Git, but there’s no hiding the fact that it’s quite easy to get
yourself and your Git repository into a pickle. Therefore, I have tried
hard to keep things as straight forward as I can during this tutorial
and as a result I do occasionally show you a couple of very ‘un-Git’
ways of doing things (mostly about reverting to previous versions of
documents). Don’t get hung up about this, there’s no shame to using
these low tech solutions and if it works then it works. Lastly, GitHub
was not designed to host very large files and will warn you if you try
to add files greater than 50 MB and block you adding files greater than
100 MB. If your project involves using large file sizes there are a <a
href="https://help.github.com/en/github/managing-large-files/configuring-git-large-file-storage">few
solutions</a> but I find the easiest is to host these files elsewhere
(Googledrive, Dropbox etc) and create a link to them in a README file or
R markdown document on Github.</p>
<p> </p>
<div id="what-is-version-control" class="section level2">
<h2>What is version control?</h2>
<p> </p>
<p>A <a href="https://en.wikipedia.org/wiki/Version_control">Version
Control System</a> (VCS) keeps a record of all the changes you make to
your files that make up a particular project and allows you to revert to
previous versions of files if you need to. To put it another way, if you
muck things up or accidentally lose important files you can easily roll
back to a previous stage in your project to sort things out. Version
control was originally designed for collaborative software development,
but it’s equally useful for scientific research and collaborations
(although admittedly a lot of the terms, jargon and functionality are
focused on the software development side). There are many different
version control systems currently available, but we’ we’ll focus on
using <em>Git</em>, because it’s free and open source and it integrates
nicely with RStudio. This means that its can easily become part of your
usual workflow with minimal additional overhead.</p>
<p> </p>
</div>
<div id="why-use-version-control" class="section level2">
<h2>Why use version control?</h2>
<p> </p>
<p>So why should you worry about version control? Well, first of all it
helps avoid this (familiar?) situation when you’re working on a
project</p>
<p> </p>
<div class="figure" style="text-align: center">
<img src="images/Messy_folder.PNG" alt="You need version control" width="75%" />
<p class="caption">
You need version control
</p>
</div>
<p> </p>
<p>usually arising from this (familiar?) scenario</p>
<p> </p>
<p><img src="images/final_doc.gif" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>Version control automatically takes care of keeping a record of all
the versions of a particular file and allows you to revert back to
previous versions if you need to. Version control also helps you
(especially the future you) keep track of all your files in a single
place and it helps others (especially collaborators) review, contribute
to and reuse your work through the GitHub website. Lastly, your files
are always available from anywhere and on any computer, all you need is
an internet connection.</p>
<p> </p>
</div>
<div id="what-is-git-and-github" class="section level2">
<h2>What is Git and GitHub?</h2>
<p> </p>
<p><strong>Git</strong> is a version control system originally developed
by <a href="https://en.wikipedia.org/wiki/Linus_Torvalds">Linus
Torvalds</a> that lets you track changes to a set of files. These files
can be any type of file including the menagerie of files that typically
make up a data orientated project (.pdf, .Rmd, .docx, .txt, .jpg etc)
although plain text files work the best. All the files that make up a
project is called a <strong>repository</strong> (or just
<strong>repo</strong>).</p>
<p><strong>GitHub</strong> is a web-based hosting service for Git
repositories which allows you to create a remote copy of your local
version-controlled project. This can be used as a backup or archive of
your project or make it accessible to you and to your colleagues so you
can work collaboratively.</p>
<p>At the start of a project we typically (but not always) create a
<strong>remote</strong> repository on GitHub, then
<strong>clone</strong> (think of this as copying) this repository to our
<strong>local</strong> computer (the one in front of you). This cloning
is usually a one time event and you shouldn’t need to clone this
repository again unless you really muck things up. Once you have cloned
your repository you can then work locally on your project as usual,
creating and saving files for your data analysis (scripts, R markdown
documents, figures etc). Along the way you can take snapshots (called
<strong>commits</strong>) of these files after you’ve made important
changes. We can then <strong>push</strong> these changes to the remote
GitHub repository to make a backup or make available to our
collaborators. If other people are working on the same project
(<strong>repository</strong>), or maybe you’re working on a different
computer, you can <strong>pull</strong> any changes back to your local
repository so everything is synchronised.</p>
<p> </p>
<p><img src="images/Github_cartoon2.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
</div>
<div id="setup_git" class="section level2">
<h2>Getting started</h2>
<p> </p>
<p>This tutorial assumes that you have already installed the latest
versions of R and RStudio. If you haven’t done this yet you can find
instructions <a href="setup.html">here</a>.</p>
<p> </p>
<div id="install-git" class="section level3">
<h3>Install Git</h3>
<p> </p>
<p>To get started, you first need to install Git. If you’re lucky you
may already have Git installed (especially if you have a Mac or Linux
computer). You can check if you already have Git installed by clicking
on the Terminal tab in the Console window in RStudio and typing
<code>git --version</code> (the space after the <code>git</code> command
is important). If you see something that looks like
<code>git version 2.25.0</code> (the version number may be different on
your computer) then you already have Git installed (happy days). If you
get an error (something like <code>git: command not found</code>) this
means you don’t have Git installed (yet!).</p>
<p> </p>
<p><img src="images/git_version.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>You can also do this check outside RStudio by opening up a separate
Terminal if you want. On Windows go to the ‘Start menu’ and in the
search bar (or run box) type <code>cmd</code> and press enter. On a Mac
go to ‘Applications’ in Finder, click on the ‘Utilities’ folder and then
on the ‘Terminal’ program. On a Linux machine simply open the Terminal
(Ctrl+Alt+T often does it).</p>
<p>To install Git on a <strong>Windows</strong> computer we recommend
you download and install Git for Windows (also known as ‘Git Bash’). You
can find the download file and installation instructions <a
href="https://git-scm.com/downloads">here</a>.</p>
<p>For those of you using a <strong>Mac</strong> computer we recommend
you download Git from <a href="https://git-scm.com/downloads">here</a>
and install in the usual way (double click on the installer package once
downloaded). If you’ve previously installed Xcode on your Mac and want
to use a more up to date version of Git then you will need to follow a
few more steps documented <a
href="https://github.com/timcharper/git_osx_installer">here</a>. If
you’ve never heard of Xcode then don’t worry about it!</p>
<p>For those of you lucky enough to be working on a
<strong>Linux</strong> machine you can simply use your OS package
manager to install Git from the official repository. For Ubuntu Linux
(or variants of) open your Terminal and type</p>
<p> </p>
<pre class="markup"><code>sudo apt update
sudo apt install git</code></pre>
<p> </p>
<p>You will need administrative privileges to do this. For other
versions of Linux see <a
href="https://git-scm.com/download/linux">here</a> for further
installation instructions.</p>
<p>Whatever version of Git you’re installing, once the installation has
finished verify that the installation process has been successful by
running the command <code>git --version</code> in the Terminal tab in
RStudio (as described above). On some installations of Git (yes I’m
looking at you MS Windows) this may still produce an error as you will
also need to setup RStudio so it can find the Git executable (described
<a href="#rs_config">below</a>).</p>
<p> </p>
</div>
<div id="configure-git" class="section level3">
<h3>Configure Git</h3>
<p> </p>
<p>After installing Git, you need to configure it so you can use it.
Click on the Terminal tab in the Console window again and type the
following:</p>
<p> </p>
<pre><code>git config --global user.email 'you@youremail.com'
git config --global user.name 'Your Name'</code></pre>
<p> </p>
<p>substituting <code>'Your Name'</code> for your actual name and
<code>'you@youremail.com'</code> with your email address. We recommend
you use your University email address as you will also use this address
when you register for your GitHub account (coming up in a bit).</p>
<p>If this was successful, you should see no error messages from these
commands. To verify that you have successfully configured Git type the
following into the Terminal</p>
<p> </p>
<pre><code>git config --global --list</code></pre>
<p> </p>
<p>You should see both your <code>user.name</code> and
<code>user.email</code> configured.</p>
<p> </p>
</div>
<div id="rs_config" class="section level3">
<h3>Configure RStudio</h3>
<p> </p>
<p>As you can see above, Git can be used from the command line, but it
also integrates well with RStudio, providing a friendly graphical user
interface. If you want to use RStudio’s Git integration (we recommend
you do - at least at the start), you need to check that the path to the
Git executable is specified correctly. In RStudio, go to the menu
<code>Tools</code> -> <code>Global Options</code> ->
<code>Git/SVN</code> and make sure that ‘Enable version control
interface for RStudio projects’ is ticked and that the ‘Git executable:’
path is correct for your installation. If it’s not correct hit the
<code>Browse...</code> button and navigate to where you installed git
and click on the executable file. You will need to restart RStudio after
doing this.</p>
<p> </p>
<p><img src="images/git_path.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
</div>
<div id="register-a-github-account" class="section level3">
<h3>Register a GitHub account</h3>
<p> </p>
<p>If all you want to do is to keep track of files and file versions on
your local computer then Git is sufficient. If however, you would like
to make an off-site copy of your project or make it available to your
collaborators then you will need a web-based hosting service for your
Git repositories. This is where GitHub comes into play (there are also
other services like <a href="https://about.gitlab.com/">GitLab</a>, <a
href="https://bitbucket.org/product">Bitbucket</a> and <a
href="https://savannah.gnu.org/">Savannah</a>). You can sign up for a
free account on GitHub <a
href="https://github.com/join?source=header-home">here</a>. You will
need to specify a username, an email address and a strong password. We
suggest that you use your University email address as this will also
allow you to apply for a free <a
href="https://help.github.com/en/github/teaching-and-learning-with-github-education/applying-for-an-educator-or-researcher-discount">educator
or researcher account</a> later on which gives you some useful <a
href="https://help.github.com/en/github/teaching-and-learning-with-github-education/about-github-education-for-educators-and-researchers">benefits</a>
(don’t worry about this now though). When it comes to choosing a
username we suggest you give this some thought. Choose a short(ish)
rather than a long username, use all lowercase and hyphenate if you want
to include multiple words, find a way of incorporating your actual name
and lastly, choose a username that you will feel comfortable revealing
to your future employer!</p>
<p>Next click on the ‘Select a plan’ (you may have to solve a simple
puzzle first to verify you’re human) and choose the ‘Free Plan’ option.
Github will send an email to the email address you supplied for you to
verify.</p>
<p>Once you’ve completed all those steps you should have both Git and
GitHub setup up ready for you to use (Finally!).</p>
<p> </p>
</div>
</div>
<div id="setting-up-a-project-in-rstudio" class="section level2">
<h2>Setting up a project in RStudio</h2>
<p> </p>
<p>Now that you’re all set up, let’s create your first version
controlled RStudio project. There are a couple of different approaches
you can use to do this. You can either setup a remote GitHub repository
first then connect an RStudio project to this repository (we’ll call
this Option 1). Another option is to setup a local repository first and
then link a remote GitHub repository to this repository (Option 2). You
can also connect an existing project to a GitHub repository but we won’t
cover this here. I suggest that if you’re completely new to Git and
GitHub then use Option 1 as this approach sets up your local Git
repository nicely and you can <strong>push</strong> and
<strong>pull</strong> immediately. Option 2 requires a little more work
and therefore there are more opportunities to go wrong. We will cover
both of these options below.</p>
<p> </p>
<div id="opt1" class="section level3">
<h3>Option 1 - GitHub first</h3>
<p> </p>
<p>To use the GitHub first approach you will first need to create a
<strong>repository (repo)</strong> on GitHub. Go to your <a
href="https://github.com/">GitHub page</a> and sign in if necessary.
Click on the ‘Repositories’ tab at the top and then on the green ‘New’
button on the right</p>
<p> </p>
<p><img src="images/new_repo.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>Give your new repo a name (let’s call it <code>first_repo</code> for
this tutorial), select ‘Public’, tick on the ‘Initialize this repository
with a README’ (this is important) and then click on ‘Create repository’
(ignore the other options for now).</p>
<p> </p>
<p><img src="images/new_repo_opt.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>Your new GitHub repository will now be created. Notice the README has
been rendered in GitHub and is in markdown (.md) format (see the other
<a href="Rmarkdown_intro.html">tutorial</a> on R markdown if this
doesn’t mean anything to you). Next click on the green ‘Clone or
Download’ button and copy the <code>https//...</code> URL that pops up
for later (either highlight it all and copy or click on the copy to
clipboard icon to the right).</p>
<p> </p>
<p><img src="images/new_repo_clone.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>Ok, we now switch our attention to RStudio. In RStudio click on the
<code>File</code> -> <code>New Project</code> menu. In the pop up
window select <code>Version Control</code>.</p>
<p> </p>
<p><img src="images/proj_setup1.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>Now paste the the URL you previously copied from GitHub into the
<code>Repository URL:</code> box. This should then automatically fill
out the <code>Project Directory Name:</code> section with the correct
repository name (it’s important that the name of this directory has the
same name as the repository you created in GitHub). You can then select
where you want to create this directory by clicking on the
<code>Browse</code> button opposite the
<code>Create project as a subdirectory of:</code> option. Navigate to
where you want the directory created and click OK. I also tick the
<code>Open in new session</code> option.</p>
<p> </p>
<p><img src="images/proj_setup2.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>RStudio will now create a new directory with the same name as your
repository on your local computer and will then <strong>clone</strong>
your remote repository to this directory. The directory will contain
three new files; <code>first_repo.Rproj</code> (or whatever you called
your repository), <code>README.md</code> and <code>.gitignore</code>.
You can check this out in the <code>Files</code> tab usually in the
bottom right pane in RStudio. You will also have a <code>Git</code> tab
in the top right pane with two files listed (we will come to this later
on in the tutorial). That’s it for Option 1, you now have a remote
GitHub repository set up and this is linked to your local repository
managed by RStudio. Any changes you make to files in this directory will
be version controlled by Git.</p>
<p> </p>
<p><img src="images/proj_setup3.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
</div>
<div id="opt2" class="section level3">
<h3>Option 2 - RStudio first</h3>
<p> </p>
<p>An alternative approach is to create a local RStudio project first
and then link to a remote Github repository. As I mentioned before, this
option is more involved than Option 1 so feel free to skip this now and
come back later to it if you’re interested. This option is also useful
if you just want to create a local RStudio project linked to a local Git
repository (i.e. no GitHub involved). If you want to do this then just
follow the instructions below omitting the GitHub bit.</p>
<p> </p>
<p>In RStudio click on the <code>File</code> ->
<code>New Project</code> menu and select the <code>New Directory</code>
option.</p>
<p> </p>
<p><img src="images/new_proj_RS1.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>In the pop up window select the <code>New Project</code> option</p>
<p> </p>
<p><img src="images/proj_setup_RS.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>In the New Project window specify a <code>Directory name</code>
(choose <code>second_repo</code> for this tutorial) and select where you
would like to create this directory on you computer (click the
<code>Browse</code> button). Make sure the
<code>Create a git repository</code> option is ticked</p>
<p> </p>
<p><img src="images/new_proj_RS2.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>This will create a version controlled directory called
<code>second_repo</code> on your computer that contains two files,
<code>second_repo.Rproj</code> and <code>.gitignore</code> (there might
also be a <code>.Rhistory</code> file but ignore this). You can check
this by looking in the <code>Files</code> tab in RStudio (usually in the
bottom right pane).</p>
<p> </p>
<p><img src="images/new_proj_RS3.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>OK, before we go on to create a repository on GitHub we need to do
one more thing - we need to place our <code>second_repo.Rproj</code> and
<code>.gitignore</code>files under version control. Unfortunately we
haven’t covered this in detail yet so just follow the next few
instructions (blindly!) and we’ll revisit them in the <a
href="#use_git">Using Git</a> section of this tutorial.</p>
<p>To get our two files under version control click on the ‘Git’ tab
which is usually in the top tight pane in RStudio</p>
<p> </p>
<p><img src="images/new_proj_RS4.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>You can see that both files are listed. Next, tick the boxes under
the ‘Staged’ column for both files and then click on the ‘Commit’
button.</p>
<p> </p>
<p><img src="images/new_proj_RS5.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>This will take you to the ‘Review Changes’ window. Type in the commit
message ‘First commit’ in the ‘Commit message’ window and click on the
‘Commit’ button. A new window will appear with some messages which you
can ignore for now. Click ‘Close’ to close this window and also close
the ‘Review Changes’ window. The two files should now have disappeared
from the Git pane in RStudio indicating a successful commit.</p>
<p> </p>
<p><img src="images/new_proj_RS6.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>OK, that’s those two files now under version control. Now we need to
create a new repository on GitHub. In your browser go to your <a
href="https://github.com/">GitHub page</a> and sign in if necessary.
Click on the ‘Repositories’ tab and then click on the green ‘New’ button
on the right. Give your new repo the name <code>second_repo</code> (the
same as your version controlled directory name) and select ‘Public’.
This time <strong>do not</strong> tick the ‘Initialize this repository
with a README’ (this is important) and then click on ‘Create
repository’.</p>
<p> </p>
<p><img src="images/new_proj_RS7.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>This will take you to a Quick setup page which provides you with some
code for various situations. The code we are interested in is the code
under
<code>...or push an existing repository from the command line</code>
heading.</p>
<p> </p>
<p><img src="images/new_proj_RS8.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>Highlight and copy the first line of code (note: yours will be
slightly different as it will include your GitHub username not mine)</p>
<p><code>git remote add origin https://github.com/alexd106/second_repo.git</code></p>
<p>Switch to RStudio, click on the ‘Terminal’ tab and paste the command
into the Terminal. Now go back to GitHub and copy the second line of
code</p>
<p><code>git push -u origin master</code></p>
<p>and paste this into the Terminal in RStudio. You should see something
like this</p>
<p> </p>
<p><img src="images/new_prog_RS9.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>If you take a look at your repo back on GitHub (click on the
<code>/second_repo</code> link at the top) you will see the
<code>second_repo.Rproj</code> and <code>.gitignore</code> files have
now been <strong>pushed</strong> to GitHub from your local
repository.</p>
<p> </p>
<p><img src="images/new_proj_RS10.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>The last thing we need to do is create and add a
<strong>README</strong> file to your repository. A README file describes
your project and is written using the same Markdown language you learned
in the R markdown <a href="Rmarkdown_intro.html">tutorial</a>. A good
README file makes it easy for others (or the future you!) to use your
code and reproduce your project. You can create a README file in RStudio
or in GitHub. Let’s use the second option.</p>
<p>In your repository on GitHub click on the green
<code>Add a README</code> button.</p>
<p> </p>
<p><img src="images/new_proj_RS11.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>Now write a short description of your project in the
<code><> Edit new file</code> section and then click on the green
<code>Commit new file</code> button.</p>
<p> </p>
<p><img src="images/new_proj_RS12.png" width="60%" style="display: block; margin: auto;" /></p>
<p> </p>
<p>You should now see the <code>README.md</code> file listed in your
repository. It won’t actually exist on your computer yet as you will
need to <strong>pull</strong> these changes back to your local
repository, but more about that in the next section.</p>
<p> </p>
<p>Whether you followed Option 1 or Option 2 (or both) you have now
successfully setup a version controlled RStudio project (and associated