WEBVTT 0 00:00.530 --> 00:06.060 Now the first thing we're going to do is open up terminal and inside terminal we're going to navigate 1 00:06.060 --> 00:07.950 to our desktop. 2 00:07.950 --> 00:13.590 So if you're not familiar with the command line and you haven't watched the module on the command line, 3 00:13.920 --> 00:18.330 then I recommend you to take a look at it now because we're going to be using a lot of the commands 4 00:18.330 --> 00:20.760 to create directories and navigate around. 5 00:20.760 --> 00:25.930 So if you're not yet familiar then it's a good time to take a look at that. 6 00:25.950 --> 00:33.540 So the first thing I'm going to do is I'm going to cd into my desktop. And here I'm going to create a 7 00:33.540 --> 00:43.950 new directory called Story. Then I'm going to cd into this Story directory and if I show you with ls you 8 00:43.950 --> 00:45.800 can see that it's completely empty. 9 00:45.930 --> 00:50.360 And similarly inside Finder I can show you the same thing. 10 00:50.370 --> 00:54.560 So let's go ahead and create some text files. 11 00:54.600 --> 00:59.580 So I'm going to use touch to create a file called chapter1.txt. 12 00:59.760 --> 01:07.710 And then I'm going to open chapter1.txt and you can either do this by simply writing open and it'll open 13 01:07.710 --> 01:11.820 it inside the default application which is TextEdit on a Mac, 14 01:12.060 --> 01:18.010 alternatively you can use a text editor within the terminal, for example Vim. 15 01:18.150 --> 01:21.710 Now this is only for people who are really familiar with this. 16 01:21.720 --> 01:23.990 So it's totally up to you what you decide to use. 17 01:24.030 --> 01:30.150 But if you are not familiar with using terminal based editors, then it's probably a good idea to just 18 01:30.150 --> 01:33.550 stick with the open command and use TextEdit. 19 01:33.870 --> 01:42.720 So let's go ahead and open chapter1 and let's write something inside, shall we? 20 01:43.010 --> 01:45.230 Okay, so that's my masterpiece done. 21 01:45.420 --> 01:46.910 Going to go ahead and hit save 22 01:46.950 --> 01:49.450 and I'm going to quit TextEdit. 23 01:49.520 --> 01:53.140 So that was our first chapter done. 24 01:53.330 --> 02:02.420 So now let's create a Git local repository and start tracking some of these file changes. So to initialize 25 02:02.420 --> 02:06.290 Git we simply write git init. 26 02:06.290 --> 02:12.440 And as you can see it's initialized and empty Git repository inside the story directory. 27 02:12.440 --> 02:17.260 Now if you have a look inside Finder you actually can't see this .git at all. 28 02:17.270 --> 02:18.870 But as we learned before 29 02:18.920 --> 02:20.640 if you use ls -a 30 02:20.870 --> 02:26.540 you can see all the hidden files and you can see that .git is right there and it's going to be used 31 02:26.660 --> 02:32.270 to track all your changes, to commit your changes and to perform version control. 32 02:32.270 --> 02:38.060 So we're currently inside the story directory and you can also call this the working directory. 33 02:38.060 --> 02:45.200 So as I mentioned before, using Git and learning about version control it comes with some of its own terminology 34 02:45.290 --> 02:46.430 and language. 35 02:46.520 --> 02:51.320 So I'm going to try and debunk and simplify a lot of these terms that you'll come across just so that 36 02:51.320 --> 02:55.660 we can all be on the same page and we all understand what's going on. 37 02:55.670 --> 03:01.640 So currently we are inside the working directory which is the story directory. 38 03:02.000 --> 03:07.700 And here in order to start tracking the changes of my files 39 03:07.700 --> 03:09.740 for example chapter1.txt, 40 03:09.920 --> 03:15.320 then I need to add this file to what's called a staging area. 41 03:15.740 --> 03:23.180 And that is basically a intermediate place where you can pick and choose which files inside your working 42 03:23.180 --> 03:26.220 directory that you want to commit. 43 03:26.270 --> 03:33.170 So to see what's currently inside your staging area, you can use the git status command. And it shows 44 03:33.170 --> 03:39.560 you that there are untracked files which will be shown in red and this is something that simply inside 45 03:39.560 --> 03:41.020 your working directory, 46 03:41.030 --> 03:42.270 so inside here, 47 03:42.470 --> 03:45.310 but it's not yet in the staging area. 48 03:45.560 --> 03:52.520 So in order to add it to the staging area and to start tracking changes in it, then we have to use the 49 03:52.520 --> 03:54.030 command git 50 03:54.060 --> 03:56.840 add. So we're going to type git add 51 03:57.170 --> 03:58.880 and we're going to type the filename. 52 03:58.880 --> 04:01.730 So in this case it's chapter1.txt. 53 04:01.910 --> 04:08.660 So go ahead and hit enter and then if we try using git status again you can see that that file has been 54 04:08.750 --> 04:11.770 added as a new file and it is now green. 55 04:11.780 --> 04:16.330 So this is now in the staging area and it's ready to be committed. 56 04:16.340 --> 04:20.210 So let's go ahead and commit this under version control. 57 04:20.210 --> 04:28.360 So the command is git commit and I'm going to use the -m flag to add a commit message. 58 04:28.430 --> 04:31.540 So the commit message is really really important. 59 04:31.580 --> 04:38.010 It's something that helps you keep track of what changes you have made in each commit. 60 04:38.090 --> 04:47.150 So when you create a new save point, you want to be as explicit as possible about what changes were made 61 04:47.180 --> 04:50.300 between the last save point and this current save point. 62 04:50.300 --> 04:58.340 So for our initial commit we can use something very simple like Initial Commit. And this shows that this 63 04:58.340 --> 04:59.830 is our starting point. 64 05:00.050 --> 05:05.870 Alternatively if you want to be slightly more specific because in our case we've actually completed 65 05:05.870 --> 05:06.890 chapter1, 66 05:07.040 --> 05:11.100 so you can write Complete Chapter 1. 67 05:11.120 --> 05:16.490 Now the thing that you'll realize is that usually with commit messages they are written in the present 68 05:16.490 --> 05:18.550 tense and this is the best practice. 69 05:18.560 --> 05:25.160 So whereas it would probably make more sense I guess at least in my head anyways to write completed 70 05:25.220 --> 05:28.090 chapter 1 as this save point, 71 05:28.190 --> 05:32.630 it's actually by convention that you should always use the present tense. 72 05:32.630 --> 05:36.860 So it's like you are doing it now. You're submitting your changes now. 73 05:36.950 --> 05:44.150 So let's go ahead and hit enter to make our first commit and you can see what commits you have made 74 05:44.150 --> 05:47.230 by using the git log command. 75 05:47.510 --> 05:53.200 So you can see that this commit was made at this time by this person. 76 05:53.490 --> 06:01.730 And it also has a hash and this hash uniquely identifies this particular commit. And then right at the 77 06:01.730 --> 06:06.640 end you see this commit message of what this save point was all about. 78 06:07.820 --> 06:12.260 So now I'm going to go ahead and create two more chapters. 79 06:12.290 --> 06:25.400 So let's just create chapter2.txt and chapter3.txt and now we have three chapters 80 06:25.670 --> 06:29.580 and I'm going to go in and change some of these text files. 81 06:29.630 --> 06:30.980 So let's say... 82 06:41.260 --> 06:43.900 Okay, that's chapter 2 done. 83 06:43.900 --> 06:49.330 And finally let's go ahead and just open chapter 3 and edit that as well. 84 07:01.090 --> 07:01.390 All right. 85 07:01.400 --> 07:03.670 So all three files have been changed. 86 07:03.710 --> 07:09.020 And over here in Finder you can actually get a quick peek at what the contents are which is going to 87 07:09.020 --> 07:14.720 be really useful for me to be able to demonstrate to you what Git is doing behind the background. 88 07:14.750 --> 07:20.810 So now let's go ahead and add these two new files to our staging area. 89 07:20.810 --> 07:28.130 So again if we use git status, you can see that there's two files that are untracked which are only in 90 07:28.130 --> 07:31.940 the working directory and not yet inside the staging area. 91 07:32.240 --> 07:38.580 So we can put it into the staging area by simply adding each of them as we did before, 92 07:38.600 --> 07:45.370 git add and writing something like chapter2.txt and then doing git add chapter3.txt. 93 07:45.590 --> 07:51.890 But as you can imagine if you have quite a few files then it can get incredibly tedious having to do 94 07:51.890 --> 07:53.530 this one by one. 95 07:53.540 --> 07:56.030 So of course there is a better way. 96 07:56.030 --> 08:01.820 So instead of adding these files one by one, we can actually simply just say git add and then use the 97 08:01.820 --> 08:07.010 to specify everything inside this current directory, 98 08:07.010 --> 08:09.780 so everything inside the story directory. 99 08:10.010 --> 08:14.860 So now if I go ahead and hit enter and then let's go to git status again, 100 08:14.870 --> 08:21.290 you can see that there's two new files that have been added to the staging area. And now we're going 101 08:21.290 --> 08:26.570 to commit those two files to a new commit to a new save point. 102 08:26.870 --> 08:30.060 And you know what to do. If you're following along with me 103 08:30.080 --> 08:31.420 go ahead and give it a go. 104 08:35.500 --> 08:35.830 All right. 105 08:35.830 --> 08:38.480 So how is that? If you remember, 106 08:38.500 --> 08:39.890 the command is 107 08:39.940 --> 08:48.730 git commit and we're going to use the -m to specify a commit message. And we're going to write a message 108 08:49.150 --> 08:58.690 that is in the present tense. So let's say complete chapter 2 and 3. 109 08:58.900 --> 09:05.550 So that's everything I've done between the initial commit and this commit 110 09:05.560 --> 09:09.950 the only difference is the fact that I've completed now chapter 2 and chapter 3. 111 09:10.210 --> 09:13.120 So let's go ahead and and hit enter. 112 09:13.510 --> 09:15.720 So again let's check it out 113 09:15.730 --> 09:23.830 using git log. We can see that we now have two commits both with different hashes because they are unique 114 09:23.890 --> 09:24.960 and they are different. 115 09:25.240 --> 09:32.950 So the initial one was complete chapter 1 and it was done at this time. And then later on about five 116 09:32.950 --> 09:36.060 minutes later I completed chapter 2 and 3 117 09:36.130 --> 09:38.140 and that was the second commit. 118 09:38.440 --> 09:41.170 And this is where we are at right now. 119 09:41.200 --> 09:44.800 So you can see by the head by this word HEAD 120 09:45.100 --> 09:49.420 this is the position or the current state that we are in. 121 09:49.420 --> 09:52.800 So I just want to quickly recap what we've just done. 122 09:52.900 --> 09:58.380 So we created a file in our working directory inside our story directory. 123 09:58.570 --> 10:05.960 So the working directory is the folder or the directory where you initialize your Git repository. 124 10:06.040 --> 10:10.340 When we said git init, we did that inside the story directory. 125 10:10.390 --> 10:12.470 So that becomes a working directory. 126 10:12.700 --> 10:18.850 And from now on Git is going to try and track the changes that it sees between the working directory 127 10:19.240 --> 10:22.020 and the local repository. 128 10:22.420 --> 10:31.020 So in the beginning we created a file inside our working directory inside story and then we used git 129 10:31.010 --> 10:34.270 add to push it to the staging area. 130 10:34.270 --> 10:39.700 Now the reason why there is this intermediate staging area because you might wonder, why not just go 131 10:39.700 --> 10:42.270 from the working directory straight to the repository? 132 10:42.460 --> 10:44.640 Why do we need this extra step? 133 10:44.800 --> 10:52.390 Well sometimes you might not want to add all of your files to be tracked or all of your files to be 134 10:52.390 --> 10:53.510 committed. 135 10:53.560 --> 10:59.190 So the staging area is a good place to try and figure out what are the things that you want Git to ignore 136 10:59.350 --> 11:02.070 and what are the things that you want to be tracked. 137 11:02.140 --> 11:09.130 So once we've used git add we've put all file into the staging area and we're happy with the changes 138 11:09.130 --> 11:17.120 that we are going to commit, then the next step is to go ahead and commit it using the git commit command. 139 11:17.140 --> 11:25.660 So now our file is inside our local repository. so that .git and that version is given a name through 140 11:25.660 --> 11:27.310 the commit message. 141 11:27.310 --> 11:35.470 So that means that even if we've messed up our file, we can still use the last version that's under version 142 11:35.470 --> 11:43.720 control and we can use a special command called git checkout to revert back or rollback to the last 143 11:43.780 --> 11:46.210 position in our local repository. 144 11:46.330 --> 11:52.010 So let me show you what that looks like in the command line and how we would do that in practice. 145 11:52.030 --> 11:57.370 So at the moment, I've got three nicely written chapters and I have a feeling my book is going to be 146 11:57.370 --> 11:58.370 a big seller. 147 11:58.390 --> 12:06.460 So now let's say that I have you know I've been working on chapter 3 and I have completely messed 148 12:06.460 --> 12:13.840 everything up and just you know fell asleep on my keyboard and I happened to have saved my file. 149 12:13.840 --> 12:21.140 And now if you have a look at it, it's now just mumbo jumbo and I've ruined my masterpiece. But fear not 150 12:21.190 --> 12:28.220 because we have version control and we have Git enabled so we have nothing to worry about. 151 12:28.540 --> 12:33.600 I can actually revert the changes that I've made locally in my working directory. 152 12:33.730 --> 12:39.230 So at this point you can use git status to see that we have modifications in our chapter3. 153 12:39.350 --> 12:44.740 txt file that have not yet been committed or added to the staging area. 154 12:44.800 --> 12:50.790 So if we wanted to we can actually revert this back to it's previous glory. 155 12:51.190 --> 12:57.850 But before we do that, we can use a git command to check out one of the differences between the current 156 12:57.850 --> 13:02.880 version of chapter 3 and the last save point in our Git repository. 157 13:03.190 --> 13:06.760 So to do that you can use the command git diff 158 13:06.910 --> 13:12.390 so the difference, and we'll give it the chapter 3 file name. 159 13:12.610 --> 13:17.140 And if you hit enter, you can see that this is the part that was deleted, 160 13:17.140 --> 13:23.810 so the part in red, and then this was the part that was added which is our gobbledegoop. 161 13:23.980 --> 13:29.560 So now if I've looked at these differences, it might just be that you know there's only a few mistakes 162 13:29.890 --> 13:35.970 in my new version of chapter 3 and I just want to maybe copy some things over or have a look at how 163 13:35.970 --> 13:41.340 I did certain things previously and change my current file. 164 13:41.670 --> 13:46.860 But other times, it might be that you know it's just you want to touch the new file. 165 13:46.860 --> 13:49.070 You just don't want anything to do with it 166 13:49.080 --> 13:52.860 and you would much rather roll back to the previous version. 167 13:52.920 --> 13:59.310 So if you want to do that then there is a command called git checkout that is going to be really really 168 13:59.310 --> 14:00.650 useful for you. 169 14:00.750 --> 14:03.000 So git checkout 170 14:03.150 --> 14:07.890 and then we're going to specify the name of the file that we want to checkout which is chapter3. 171 14:07.890 --> 14:14.520 txt. And if you just watch over here which is the preview of the current version of chapter3.txt, 172 14:15.120 --> 14:22.980 once I hit enter on this command that basically asks to roll back this chapter 3 to the last version 173 14:23.010 --> 14:26.520 that was committed in our local repository. 174 14:26.520 --> 14:33.840 So if I hit enter, you can see that almost immediately my chapter 3 has been restored to its previous 175 14:34.380 --> 14:35.550 glorious state. 176 14:35.760 --> 14:41.340 And this is the version of chapter 3 at the last check point at which I committed it. 177 14:41.580 --> 14:45.940 So that was this one which is completed chapter 2 and 3. 178 14:45.960 --> 14:53.820 So whereas in this lesson we've looked mostly at local implementations of Git so saving these versions 179 14:53.910 --> 14:56.810 on our computer locally, in the next lesson 180 14:56.820 --> 15:02.520 I'm going to talk about GitHub and creating remote repositories. So I'll see you there.