-
Notifications
You must be signed in to change notification settings - Fork 149
/
Friday Session 2. Github and Source Control Workflow.srt
executable file
·1494 lines (1190 loc) · 30.5 KB
/
Friday Session 2. Github and Source Control Workflow.srt
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
1
00:00:00,401 --> 00:00:04,803
[MUSIC]
2
00:00:04,872 --> 00:00:10,008
Stanford University.
>> Welcome everyone to
3
00:00:10,077 --> 00:00:14,613
our Friday section of CS193P.
I'm Junjie, one of your TAs.
4
00:00:14,682 --> 00:00:18,249
And today, I'm gonna
talk about the GitHub and
5
00:00:18,318 --> 00:00:23,221
the source control
workflow in Xcode 9. So,
6
00:00:23,290 --> 00:00:28,126
what is source control?
Like have you used like
7
00:00:28,195 --> 00:00:31,029
some source control
systems like GitHub?
8
00:00:31,098 --> 00:00:36,067
Yeah, so as a software
developer, you will like,
9
00:00:36,136 --> 00:00:40,672
almost like it's
a necessary skill set for
10
00:00:40,741 --> 00:00:43,442
you to learn how to use
source control systems.
11
00:00:43,510 --> 00:00:46,611
Like big companies like
Facebook and Google,
12
00:00:46,680 --> 00:00:50,381
they all have their own
source control systems. And
13
00:00:50,450 --> 00:00:54,385
it's very important for you to
master those skills before you
14
00:00:54,454 --> 00:00:58,223
really go into the industry.
So what is source control?
15
00:00:58,291 --> 00:01:03,328
Why should we matter?
Why should we care about it?
16
00:01:03,397 --> 00:01:07,933
It allows you to track your
code changes over time.
17
00:01:08,002 --> 00:01:11,370
You can revert your files
back to a previous state.
18
00:01:11,438 --> 00:01:16,107
You can compare different
versions of code side by side.
19
00:01:16,176 --> 00:01:18,476
And it also makes
it a lot easier for
20
00:01:18,545 --> 00:01:22,481
you to work with
other teammates. And
21
00:01:22,549 --> 00:01:26,884
like when bugs happen, it's
much easier to take it down.
22
00:01:26,953 --> 00:01:31,556
Today, our topic is one of the
biggest source control system
23
00:01:31,625 --> 00:01:36,361
in the world, and
it's built right into Xcode 9.
24
00:01:36,430 --> 00:01:39,697
Xcode 9 has done a lot of
great things to integrate
25
00:01:39,766 --> 00:01:44,670
GitHub to make it
very easy to use. So
26
00:01:44,738 --> 00:01:49,808
I'm gonna try to demo you some
of the basics in Xcode 9,
27
00:01:49,877 --> 00:01:54,446
how to use GitHub. Before
that, you should register
28
00:01:54,515 --> 00:01:59,284
an account in github.com.
The repository,
29
00:01:59,353 --> 00:02:04,690
the public repository or
a repo, is free. And, a repo
30
00:02:04,758 --> 00:02:08,693
is just like a code directory
for you to store the code.
31
00:02:08,762 --> 00:02:12,930
And if you register using
your Stanford email account,
32
00:02:12,999 --> 00:02:17,068
you can get the private
repository for free. I highly,
33
00:02:17,137 --> 00:02:21,072
highly encourage you to use
a private repo to manage
34
00:02:21,141 --> 00:02:24,342
all your assignments,
all your cost projects.
35
00:02:24,411 --> 00:02:28,547
And it's much easier for
you to collaborate with your
36
00:02:28,616 --> 00:02:31,716
teammates, and in-
>> Remind people not to make
37
00:02:31,785 --> 00:02:33,284
it public, though.
>> Yeah, yeah.
38
00:02:33,353 --> 00:02:34,252
>> [CROSSTALK] the public
39
00:02:34,320 --> 00:02:35,020
to private.
>> So
40
00:02:35,088 --> 00:02:39,658
that's why you wanna register
using your Stanford email.
41
00:02:39,727 --> 00:02:41,926
You would never like up,
upload
42
00:02:41,995 --> 00:02:46,164
your assignments onto GitHub
using a public repository cuz
43
00:02:46,233 --> 00:02:50,268
like you don't know who will
just download all the code and
44
00:02:50,337 --> 00:02:55,040
copy, copy out of no place.
So last Friday's session,
45
00:02:55,108 --> 00:02:59,344
Jason also mentioned that
there's a Pro Git ebook.
46
00:02:59,413 --> 00:03:03,181
That's really great for you to
learn some of the basics of
47
00:03:03,250 --> 00:03:06,584
GitHub and the source control,
some of the concepts.
48
00:03:06,653 --> 00:03:09,754
It's a really good
starting point for
49
00:03:09,823 --> 00:03:14,259
you to learn those skills.
I post a link here and
50
00:03:14,327 --> 00:03:19,697
you can like check it out if
you want. So there are three
51
00:03:19,766 --> 00:03:24,069
main components in like Xcode
9 Source Control Workflow, and
52
00:03:24,138 --> 00:03:27,839
I will go through
each of them briefly.
53
00:03:27,908 --> 00:03:32,410
The first one is pulling and
pushing. For pulling and
54
00:03:32,479 --> 00:03:35,647
pushing is like dealing
with your local changes and
55
00:03:35,716 --> 00:03:39,518
the changes in the remote
repository. So, for
56
00:03:39,586 --> 00:03:40,551
clone and pull,
57
00:03:40,620 --> 00:03:44,522
you can download all the code
from the remote repository to
58
00:03:44,591 --> 00:03:48,860
your own laptop. Xcode 9
actually make it really,
59
00:03:48,929 --> 00:03:53,298
really easy to download
a repository. You just click
60
00:03:53,366 --> 00:03:57,502
the Clone and Download button
and then open it in Xcode.
61
00:03:57,570 --> 00:04:00,571
And Xcode will download
all the changes for
62
00:04:00,640 --> 00:04:02,573
you. It's really nice. And
63
00:04:02,642 --> 00:04:07,112
I will show you how to do that
in a demo. And another way to
64
00:04:07,181 --> 00:04:12,650
do it is to copy out this
GitHub repository address.
65
00:04:12,719 --> 00:04:17,221
And I will also show you how
to do that in a demo. For
66
00:04:17,290 --> 00:04:20,925
the push, it's like you
make some local changes,
67
00:04:20,994 --> 00:04:24,563
you, and you want to push to
the remote so that you can
68
00:04:24,631 --> 00:04:30,469
edit it in another machine or
share it to your friends.
69
00:04:32,606 --> 00:04:37,509
So, every batch of code
changes is called a commit.
70
00:04:37,578 --> 00:04:41,679
The commit it's like
a snapshot of all the code
71
00:04:41,748 --> 00:04:46,784
changes and
also the description message.
72
00:04:46,853 --> 00:04:51,423
It has the author, description
message, and also the time.
73
00:04:51,491 --> 00:04:55,927
I highly encourage you to
make small commits instead of
74
00:04:55,996 --> 00:05:00,732
making a really big commit,
like only one commit for
75
00:05:00,801 --> 00:05:05,303
the repository. That's
like the final finish one.
76
00:05:05,372 --> 00:05:10,875
Because making small commits
makes it much easier to debug.
77
00:05:10,944 --> 00:05:14,279
The rule of thumb is one
feature at a time, and
78
00:05:14,348 --> 00:05:17,449
each commit should
be self-contained.
79
00:05:17,517 --> 00:05:21,653
When bugs happen, you can
easily revert to the previous
80
00:05:21,721 --> 00:05:27,225
state without affecting
the other files. So
81
00:05:27,293 --> 00:05:33,364
here's what the commit panel
looks like in Xcode 9.
82
00:05:33,433 --> 00:05:37,569
You get the commit message
here and also the author,
83
00:05:37,638 --> 00:05:40,304
the branches,
the tags and remotes.
84
00:05:40,373 --> 00:05:44,409
We will go through these
concepts in a demo. And
85
00:05:44,477 --> 00:05:48,113
here's the identifier,
it is the SHA identifier for
86
00:05:48,181 --> 00:05:53,051
this commit. The last
87
00:05:53,119 --> 00:05:57,221
concept I'm introducing
today is the branching. So
88
00:05:57,290 --> 00:06:04,061
each little commits, one after
one, is called a code branch.
89
00:06:04,130 --> 00:06:06,297
In the GitHub repo,
repository,
90
00:06:06,366 --> 00:06:10,435
the most important one is the
master branch. You would wanna
91
00:06:10,503 --> 00:06:13,772
always keep your master
branch clean, bug-free and
92
00:06:13,840 --> 00:06:18,710
stable. And when you want
to work on new features,
93
00:06:18,778 --> 00:06:23,347
you would like to branch
from the master. So
94
00:06:23,416 --> 00:06:28,119
Xcode 9 is really easy for
you to just right-click and
95
00:06:28,188 --> 00:06:32,523
create a new branch. The new
branch will have the same
96
00:06:32,592 --> 00:06:37,595
code as the master. But when
you keep developing your code,
97
00:06:37,664 --> 00:06:43,000
it won't affect the master
branch. And after you finish
98
00:06:43,069 --> 00:06:46,438
developing all your features,
you've unit tested your code,
99
00:06:46,507 --> 00:06:49,841
you can merge it back
to master branch.
100
00:06:49,910 --> 00:06:55,946
I will show you how to do the
branching in this demo. So,
101
00:06:56,015 --> 00:07:01,119
any questions so far? No,
102
00:07:01,187 --> 00:07:06,925
so let's go into the demo.
So let's pull out Xcode.
103
00:07:08,628 --> 00:07:11,896
So before we do the fancy
things in Xcode,
104
00:07:11,964 --> 00:07:15,833
we would like to set up the
GitHub account in Xcode. So
105
00:07:15,902 --> 00:07:21,372
the way to do it as click
on Xcode > Preferences
106
00:07:21,441 --> 00:07:26,077
> Accounts. When you
select the Add button,
107
00:07:26,145 --> 00:07:30,548
you can add GitHub Enterprise.
Enterprise is like,
108
00:07:30,617 --> 00:07:34,419
when you work for a company,
they will have the company
109
00:07:34,488 --> 00:07:38,590
GitHub account. So
I'm gonna select the GitHub.
110
00:07:38,658 --> 00:07:47,165
And I'm using my Stanford
email address. So
111
00:07:47,234 --> 00:07:51,369
we've already signed in. And
before we jump into the code,
112
00:07:51,437 --> 00:07:55,106
we would wanna make sure the
Source Control is setting up
113
00:07:55,175 --> 00:08:00,211
correctly. So, these options
is like, whether you
114
00:08:00,279 --> 00:08:05,850
want Xcode to download
the remote changes for you.
115
00:08:05,919 --> 00:08:11,322
So that you won't need to pull
the changes in command line.
116
00:08:11,391 --> 00:08:14,992
And when you click on the Git
button here, you can set up
117
00:08:15,061 --> 00:08:20,665
some global ignored files. If
you have used GitHub before,
118
00:08:20,734 --> 00:08:24,869
you know that there's
a gitignore file so
119
00:08:24,938 --> 00:08:31,041
that your files won't be
checked by the GitHub system.
120
00:08:31,110 --> 00:08:35,113
So you can set up some global
ignored files here. That means
121
00:08:35,181 --> 00:08:40,652
like .D_Store files won't
be checked by the GitHub. So
122
00:08:40,720 --> 00:08:45,790
let's start by creating
a new Xcode product.
123
00:08:49,529 --> 00:08:54,799
Let's do Single View,
and then GitTutorial.
124
00:08:59,305 --> 00:09:03,174
And it's basically the same
thing as we are doing when
125
00:09:03,243 --> 00:09:06,377
creating the new project for
our homework,
126
00:09:06,446 --> 00:09:10,815
but the difference is that we
wanna select this button. So
127
00:09:10,884 --> 00:09:13,951
this is the mysterious
Source Control button we never
128
00:09:14,020 --> 00:09:15,920
talk about in class.
129
00:09:15,989 --> 00:09:20,591
And this means that Xcode will
initialize a Git repository
130
00:09:20,660 --> 00:09:25,297
for you. So we would select
this button for this section.
131
00:09:26,399 --> 00:09:31,436
Hit Create, and
then we would have
132
00:09:31,504 --> 00:09:36,408
a Git repository, and
133
00:09:36,476 --> 00:09:43,582
we can inspect the Source
Control panel over here.
134
00:09:43,650 --> 00:09:47,485
It's on the top left,
and the second button.
135
00:09:50,056 --> 00:09:53,658
It shows that we are currently
at the master branch. And
136
00:09:53,726 --> 00:09:57,896
the initial commit is just
some free code given by Xcode
137
00:09:57,964 --> 00:10:02,166
that create, initialize,
and then push the code for
138
00:10:02,235 --> 00:10:07,539
you. So,
let's make some change.
139
00:10:09,076 --> 00:10:14,012
Maybe I will delete
this free code. And
140
00:10:14,080 --> 00:10:22,420
then add a print line.
141
00:10:22,489 --> 00:10:27,525
And once I've saved the file,
there's an M appeared here.
142
00:10:27,594 --> 00:10:31,429
That M stands for modified.
Basically, it means that your
143
00:10:31,497 --> 00:10:37,268
local repository have some
uncommitted changes. And
144
00:10:37,337 --> 00:10:41,305
that's also modified in
storyboard. I want to drag our
145
00:10:41,374 --> 00:10:47,645
button. And
146
00:10:47,714 --> 00:10:51,816
put it in the middle. Okay,
147
00:10:51,885 --> 00:10:57,087
let's save the change. So
now I have made some change.
148
00:10:57,156 --> 00:11:00,658
So we wanna commit
those changes.
149
00:11:00,727 --> 00:11:03,828
It's like clicking
the Source Control here, and
150
00:11:03,897 --> 00:11:09,901
then hit Commit. Then
a Version Editor will appear.
151
00:11:09,970 --> 00:11:14,505
And it's really nice because
you can see your change
152
00:11:14,573 --> 00:11:19,110
side by side by clicking the
filename. And it shows that
153
00:11:19,179 --> 00:11:22,780
you've added one line and
you've deleted some code.
154
00:11:22,849 --> 00:11:26,851
And if you click on
the button in the middle,
155
00:11:26,919 --> 00:11:30,554
you can actually don't
commit this specific line or
156
00:11:30,623 --> 00:11:37,962
discards the change. So, let's
also look at the storyboard.
157
00:11:38,030 --> 00:11:41,299
Maybe it's your first time
looking the storyboard.
158
00:11:41,368 --> 00:11:45,603
At this level, we generally
don't like added the file in,
159
00:11:45,671 --> 00:11:49,173
in this level.
It's a XML file, so
160
00:11:49,242 --> 00:11:54,511
it's super complicated. After
you've confirmed that this is
161
00:11:54,580 --> 00:11:58,817
the change you want, you would
enter the commit message here.
162
00:12:00,420 --> 00:12:07,558
Added a button and
a print. Notice that
163
00:12:07,626 --> 00:12:12,096
there's also a Push to remote
button here. But we don't
164
00:12:12,165 --> 00:12:16,767
have a remote now because the
repository only lives locally.
165
00:12:16,836 --> 00:12:19,937
We don't have a remote
repository in the GitHub.
166
00:12:20,006 --> 00:12:25,542
If we have the remote origin,
we can push also to remote.
167
00:12:25,611 --> 00:12:28,879
And I will show you how to add
the remote origin in a second.
168
00:12:28,948 --> 00:12:34,418
So now let's just
commit the files. So
169
00:12:34,487 --> 00:12:37,354
once I do that,
the M disappeared. And
170
00:12:37,423 --> 00:12:41,659
let's go back to
the Source Control Panel.
171
00:12:41,727 --> 00:12:46,764
Look, here's the commit I just
made. And you can inspect
172
00:12:46,833 --> 00:12:51,503
it by clicking it and then
select the button here. And
173
00:12:51,571 --> 00:12:57,007
it shows the author,
the date, the branches,
174
00:12:57,076 --> 00:13:02,079
and also the files. If you
want to inspect the changes,
175
00:13:02,148 --> 00:13:07,051
you can actually click
on the Assistant Editor.
176
00:13:07,119 --> 00:13:10,922
I want my Assistant Editor
on the bottom. And
177
00:13:10,991 --> 00:13:16,427
then when I click the file,
it actually
178
00:13:16,496 --> 00:13:21,232
displays the changes side by
side. You can easily see what
179
00:13:21,301 --> 00:13:26,237
files you've changed in
this specific commit.
180
00:13:32,112 --> 00:13:37,816
And let's make more
changes here, like print,
181
00:13:40,086 --> 00:13:43,621
hello CS193p.
182
00:13:47,393 --> 00:13:51,829
And over to Source Control
button here, I can actually
183
00:13:51,897 --> 00:13:56,734
discard all the changes
in my current repository.
184
00:13:56,802 --> 00:14:01,705
That basically roll back
to the latest commit. But
185
00:14:01,774 --> 00:14:07,544
I will show you how to revert
to several commits ago.
186
00:14:07,613 --> 00:14:11,315
So let's add this
commit first.
187
00:14:11,383 --> 00:14:17,087
Add an, another print.
188
00:14:17,156 --> 00:14:20,625
But we should have
successfully committed because
189
00:14:20,693 --> 00:14:25,095
there's no M over the
ViewController. And say, for
190
00:14:25,164 --> 00:14:30,534
this specific file, I want to
roll back to two commits ago.
191
00:14:30,603 --> 00:14:36,073
I can open this
Source Control button.
192
00:14:36,142 --> 00:14:40,812
And then it's a side by side
comparison between your
193
00:14:40,880 --> 00:14:45,250
local revision and
your commits. So
194
00:14:47,153 --> 00:14:52,990
if I select the commit,
two commits ago,
195
00:14:53,058 --> 00:14:58,495
I can see all my changes.
And I can click on the button
196
00:14:58,564 --> 00:15:03,368
here, if I want to
discard all the changes.
197
00:15:05,204 --> 00:15:10,641
Revert, so
now my local repository
198
00:15:10,710 --> 00:15:15,647
have reverted those changes.
199
00:15:18,584 --> 00:15:25,023
That's commit. Revert.
200
00:15:29,528 --> 00:15:31,495
Like I just mentioned,
201
00:15:31,564 --> 00:15:35,833
all the code now lives
in my local repository,
202
00:15:35,901 --> 00:15:40,971
so there's no way I can share
it to my teammate or work
203
00:15:41,040 --> 00:15:46,076
on the other workstation. And
also if I just lost my laptop,
204