All right. 1
So we've been doing a lot of stuff in the command line and it's great practice, 2
and also a lot of developers prefer it. 3
But in the new Xcode 9, there's actually a couple of changes that are made in terms of how Git version 4
control is implemented, 5
and I have to say it's a lot better than previous versions, 6
almost to the point that I'm tempted to use this instead of using the command line. But, of course, you can 7
choose whichever one you prefer, 8
and that's why I'm going to show you how you can do everything that we've just done using Xcode itself 9
without having to ever touch the command line. 10
So let's go ahead and create a Xcode project and it's going to be a single view iOS application. 11
And I'm going to call it Udder which is like an alternative to Uber. But here's my pitch: what if you 12
could get fresh milk on demand through the other app? 13
Okay. 14
Let me know in the Q and A what you think about this. 15
But anyways, let's go ahead with our example. I"m going to go ahead and click next. 16
And it's really important that previously, we've not really cared much about this. But given that we're 17
talking about source control and version control, we need to create a git repository on my Mac using 18
Xcode. And this is the equivalent of using "git init" in our Terminal. 19
So I'm going to save it on my desktop and just make sure that you've got that checked. 20
Go ahead and click Create. 21
And here's our brand-new project. 22
Now, I'm going to do a couple of things first, 23
going to maybe add a UILabel to the top of my file, and I'm going to call my app Udder, as I already 24
pitched here. And I'm going to change the size of it to size 40, make it maybe a dark gray color. 25
And let's just make that resized to the right size. 26
And let's give it a tagline, why not "Fresh Milk, On Demand. 27
That sounds pretty good, right? 28
Sounds like a start 29
already. 30
All right. 31
So now that I've done that, the other thing I want to do is add some image assets. 32
So I've got two images, one of milk and one of cow, which is all going to be very relevant in this particular 33
app. 34
So, now I am ready to go ahead and make my second commit. 35
So the first commit is on initializing the project and you can take a look at where the commits are by going 36
to this brand-new source control navigator. 37
So if you head over there and you expand these folders, then you can see that we've got a other master, 38
and we got an initial commit that was made when our project was created. 39
So let's go ahead and commit our latest changes. 40
So to do that, we simply go to source control and hit commit, or use the option command, see shortcut if 41
you wish. 42
Now, in the left-hand pane here, you can see some of those changes that were made. 43
So we added these two new images. 44
We changed our Main.storyboard, of course, this is all in XML. And you can see that this is showing what 45
the differences are. 46
So, for example, I added a brand-new label, which previously didn't exist, 47
and also I added a UI Image View. 48
So I'm ready to commit those changes, so I have to enter a commit message here, and label "Add label and 49
images." And we're going to go ahead and commit those seven files to our local repository. 50
So, now if you have a look at our master branch, you can see that we have two commits that are made. 51
All right. So, now that that commits were made, let's say we go into our Main.storyboard and we kind of 52
just mess everything up. 53
Let's just try and mess up our interface and make it something pretty terrible, and let's delete all 54
the useful parts. 55
And now it's pretty much a mess. 56
And our view controller, let's add some mumbo-jumbo which is not code. 57
And let's say, we accidentally even deleted our Main.story board. 58
Let's just move it to trash, shall we? 59
Now we've made some pretty big issues. 60
Let's delete 61
ViewControllers as well. This is a really common bug, 62
by the way, that a lot of students in our in-person programming boot camp get themselves into which is 63
when you think that you've got something selected over here, 64
but actually you've still got the file selected. 65
And if you hit backspace, it'll actually try to move your file to the trash. 66
So we've made a mess of things, basically. 67
So, of course, you can drag them out of trash, because we just place them in the trash, 68
but let's say that we just mess everything up irreversibly. 69
But what we can do is because we have that last good commit, we can simply go to source control and discard 70
all changes. 71
So this will discard all the changes that we made since the last commit inside our working copy. 72
So if I hit this button, you can see that little bit of work is done behind the scenes. And, ta-da! 73
All of our beautiful work is restored to that last checkpoint and we will no longer get fired from our jobs, 74
and there are countless stories of Git and source control saving people's lives at work every day. 75
So that was pretty cool. 76
Now, what about another feature of Git? 77
What about branching? 78
Well, it's equally easy to do. 79
So if we head into the master branch and we decide to make a branch at this point, and we can simply 80
just right click and say , "Branch from..." this particular hash or this particular point, and we can give our 81
branch a name, and I'm going to call this the milk-design and it will all become clear very quickly. 82
So, now I have the milk design. But it's important to note that current or the branch that we're currently 83
on is still the master branch. 84
So I have to also checkout to switch to the milk-design branch. 85
So let's go ahead and do that. 86
So we're now on the milk design-branch and I'm going to go into the Main.storyboard and I'm going to 87
put a image view onto the screen. And I'm going to place the milk image I've got in my Assets folder 88
on here. 89
Okay. 90
Looks pretty good. 91
Okay. 92
So that's my milk branch done. 93
And I'm going to save my Main.storyboard and I'm going to commit my changes. 94
So let's say we change interface, change view controller interface. 95
And these are obviously the changes in XML that I have just made. 96
So I added that image view and that includes that milk resource. 97
So let's go ahead and commit that file. 98
So, now let's head back to the source control navigator and let's have a look at our milk-design branch. 99
So you can see that the latest commit is in action, "Change view control interface." 100
Now, I'm going to create another branch. So I'm going to branch from the latest checkpoint inside master, 101
so we're going to branch from here again. 102
I'm going to call this a cow-design. 103
So just bear with me, bear with me here. And I'm going to check out to switch over to the cow design branch. 104
And now I'm going to go to my Main.storyboard. And remember, because we're currently at the last checkpoint 105
on the main branch, we haven't actually got an image view yet. 106
That's only inside the commit for the milk-design branch. 107
So here, I'm going to put another image view, and you guessed it, I'm going to put the cow-image that I've 108
got in here. 109
So, now I have got a cow design and also a milk design. 110
So, now I'm going to save my Main.storyboard. I'm going to go to source control and hit commit. A nd I'm 111
going to say, "Change interface to incorporate cow image." 112
Now, you always want to be precise when you are writing your commit messages because if somebody else 113
is coming across this and each of your commits are just changes, changes, changes, then that really doesn't 114
help anybody. 115
And I've actually seen this before, but this is bad practice, and this would probably get you fired if 116
you're working in a very Git intensive team. 117
So let's go ahead and commit this file and go back to our source control navigator and check out our 118
cow-design. 119
Okay, so we've got the cow images and we've got the milk design. And you know what, I think that I'd prefer 120
the milk-design. 121
So I am going to merge my milk-design into my master and that is going to become my master branch. 122
So all I have to do is right-click on milk design and I'm going to click 123
Checkout. 124
And now I can right-click on master and say, "Merge 'milk-design' into 'master.'" So milk-design is my current branch 125
that I'm currently on and I'm going to merge into my master branch. 126
So go ahead and click Merge. 127
Now once it's done that you're going to be reverted back to the master branch because it's now merged. 128
And if you have a look inside your Main.storyboard, you can see the milk-design is now the accepted design 129
on our master branch. 130
All right. 131
So that was all very easy. 132
Now, another thing that you can do is what if you wanted to have a look back at what milk-design looked like 133
or at the code that you made in another branch or, you know, way, way back in a random or different 134
commit. 135
well, say, if at the moment, we're currently at this point on the master branch, if I want to have a look 136
at what, you know, this checkpoint was like, then I can simply right click and say, "Checkout this particular 137
commit." 138
So if we check that out and we go back to our Main.storyboard, you can see that everything is reverted 139
to that checkpoint or that commit. 140
Now, this can be really invaluable for your code files because sometimes, you know, way, way back at some, 141
you know, three months ago, you might have created some sort of feature that you thought was a little 142
bit crappy, but now you've decided that it's actually a great idea. So you can have a look at what each 143
and every commit looks like by simply checking out the commit. 144
So let's go back to master and check out the main master branch and head back to where we are meant 145
to be. 146
So when you check out one of the earlier commits, you can use it to compare the differences. 147
So currently, we're on the master branch and, let's say, that, you know, type some code in here, let's 148
just add, I don't know, maybe viewWillAppear and a comment. 149
So let's go ahead and save that. 150
So this is our master branch and Commit, "And view will appear," and we're going to commit that file. 151
And you can see that this is the difference between this commit and the previous commit in terms of the code. 152
This is the addition. 153
So let's go ahead and commit that file. And then if we head over to one of our milk-design, let's say 154
checkout, 155
and in the view controller, you can see that it doesn't have the viewDidLoad. 156
But here, we're going to delete that didReceiveMemoryWarning block of code. 157
I'm going to save and I'm going to commit, and I'm going to say, "Delete 158
memory warning code." 159
Okay. So, now you can see that this is the latest commit. 160
This is the previous version and we are removing this block of code. 161
So let's commit that one file. 162
So, now what I'm gonna do is if I go into this particular button, which is the version editor, I can compare 163
and contrast my code files. 164
So on the left, I've got my master branch latest version which has the viewWillAppear and the 165
didReceiveMemoryWarning. 166
Now, on the right, let's say that I've got my milk-design latest version, so you can see that the version 167
editor actually highlights the differences between the two code files that you're comparing. 168
So you can compare different commits against each other, either from different branches or on the same 169
branch, 170
but, you know, way, way back historically, and you can check out what are the code changes that you've made 171
and see what the previous code look like. 172
And this is really invaluable when you screw up or when you mess up your code because you can always 173
compare and put things over if you realize that you actually do need this. 174
So the last thing that I want to show you in terms of using Xcode for source control is using remotes, 175
and namely, and namely GitHub. 176
So Xcode has integrated really neat feature where you can actually push your project using Xcode to 177
your GitHub remote. 178
So the first thing you have to do is go into Xcode and go to preferences. 179
And if you go to accounts, you can click add an account, and instead of picking Apple ID, you're going 180
to pick GitHub. 181
So continue, and then you're going to log in using your GitHub account. And once you've done that, 182
and, of course, you need to be internet-enabled for this check to pass because they're going to log you 183
in remotely to GitHub and they're going to grab your SSH keys and everything that you need to be 184
able to create a remote and push to your remotes. 185
So once you are done with that, you can close that down. 186
And now if we go into the source control navigator, we can head back to master. 187
So let's checkout master. And from here, I'm going to create a new remote and I'm going to push my project 188
to GitHub. 189
So if you click on your main repository and you right-click, you can "Create "Udder" Remote on GitHub," 190
or you can also add an existing remote. 191
So if you created a GitHub remote already and you just want to add that existing remote, then you 192
can use this. 193
But in most cases, you'll probably be creating a brand-new remote on GitHub. 194
So go ahead and give it a repository name, a description. 195
You can see this is very, very similar to what we did when we set up a new repository inside GitHub. 196
And as you can see the default name for your remote is still origin and I would recommend to keep it 197
so. 198
So once you're done, 199
go ahead and click Create, and it's going to push your existing repository, your local repository that's 200
managed by Xcode to your GitHub account. 201
And now, if you look inside this Remote folder, whereas before it was empty, you now have a origin, 202
and the origin has the master branch pushed to it. 203
So, now if I head over to my GitHub repository, you can see this brand-new other repository that's been created 204
for me. 205
And so you can see how incredibly simple it has been to push all of these commits onto my other repository, 206
and I've done all of that without touching the command line, without touching GitHub. 207
So if you are using a remote repository, 208
so from now on if you make some changes to your code, say, let's add 209
a viewDidDisappear, and we save and we go to Source Control, and we say Commit, you have the option 210
to push to your remote, and you can have more than one remote and you can pick which one you wanted to 211
go to. 212
Now, alternatively, if you ready committed without pushing, you could also use the Push functionality over 213
here as well. 214
So, now let me show you something. If we go on to Udder and we add a README, 215
now this is going to make our remote repository be different to our local repository, 216
so we're just going to say, "This is an app that gives you on demand fresh cow's milk," and let's just say, 217
"Create README" and commit to the master branch. 218
All right. 219
So that's done, the README has added, and now you can see that my remote repository now has a new file 220
that my local directory does not have. 221
So, now if I tried to go to Source Control and I said "Push," then it's actually going to fail. 222
And the reason is because the local repository is out of date. 223
It's basically saying that on the remote repository, we have stuff that we don't have locally. 224
So we're actually behind the remote repository because they have more things or more up-to-date stuff. 225
So in order to make our local repository reflect the changes or be up to date with the remote repository, 226
we're going to use pull. 227
So let's first commit our local changes, 228
so "Add viewDidDisappear." 229
And we're going to commit that file without pushing, and then we're going to say pull, and we're going to 230
pull from our origin/master branch, hit Pull, and it's going to pull successful. 231
Great. 232
So, now if we have a look inside this file Udder, you'll see that it actually pulled down that README 233
file that we created inside GitHub. 234
So, now our Udder local repository has everything that the remote repository has, and it has some more 235
changes. 236
So at this point, we're going to use push to push that latest change or that latest commit which is, of 237
course, the one where I added the viewDidDisappear. 238
So let's go ahead and go to Source Control, push to origin/master, hit Push, and now it will successfully push 239
to the GitHub remote. 240
So, now if we check under Udder, check inside ViewControl.swift, you can see if viewDidDisappear 241
now shows up. 242
So that's a little bit of primer on how you can work with GitHub, git source control all using Xcode. 243
So I hope that has been interesting for you. 244
And, of course, it's totally up to you which one you prefer, 245
which one is quicker, easier, more reliable. 246
And the whole reason why I showed you both ways of doing it, either using the command line or using 247
Xcode's graphical user interface is for you to pick and choose. 248
So in the next lesson, we're going to be diving into some slightly more advanced topics in terms of 249
GitHub and Git, and we're going to be talking about forking, pull requests, and merging. 250
So I will see you there.