0 1 00:00:00,530 --> 00: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 2 00:00:06,060 --> 00:00:07,950 to our desktop. 2 3 00:00:07,950 --> 00: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 4 00:00:13,920 --> 00: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 5 00:00:18,330 --> 00:00:20,760 to create directories and navigate around. 5 6 00:00:20,760 --> 00:00:25,930 So if you're not yet familiar then it's a good time to take a look at that. 6 7 00:00:25,950 --> 00: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 8 00:00:33,540 --> 00: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 9 00:00:43,950 --> 00:00:45,800 can see that it's completely empty. 9 10 00:00:45,930 --> 00:00:50,360 And similarly inside Finder I can show you the same thing. 10 11 00:00:50,370 --> 00:00:54,560 So let's go ahead and create some text files. 11 12 00:00:54,600 --> 00:00:59,580 So I'm going to use touch to create a file called chapter1.txt. 12 13 00:00:59,760 --> 00: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 14 00:01:07,710 --> 00:01:11,820 it inside the default application which is TextEdit on a Mac, 14 15 00:01:12,060 --> 00:01:18,010 alternatively you can use a text editor within the terminal, for example Vim. 15 16 00:01:18,150 --> 00:01:21,710 Now this is only for people who are really familiar with this. 16 17 00:01:21,720 --> 00:01:23,990 So it's totally up to you what you decide to use. 17 18 00:01:24,030 --> 00:01:30,150 But if you are not familiar with using terminal based editors, then it's probably a good idea to just 18 19 00:01:30,150 --> 00:01:33,550 stick with the open command and use TextEdit. 19 20 00:01:33,870 --> 00:01:42,720 So let's go ahead and open chapter1 and let's write something inside, shall we? 20 21 00:01:43,010 --> 00:01:45,230 Okay, so that's my masterpiece done. 21 22 00:01:45,420 --> 00:01:46,910 Going to go ahead and hit save 22 23 00:01:46,950 --> 00:01:49,450 and I'm going to quit TextEdit. 23 24 00:01:49,520 --> 00:01:53,140 So that was our first chapter done. 24 25 00:01:53,330 --> 00:02:02,420 So now let's create a Git local repository and start tracking some of these file changes. So to initialize 25 26 00:02:02,420 --> 00:02:06,290 Git we simply write git init. 26 27 00:02:06,290 --> 00:02:12,440 And as you can see it's initialized and empty Git repository inside the story directory. 27 28 00:02:12,440 --> 00:02:17,260 Now if you have a look inside Finder you actually can't see this .git at all. 28 29 00:02:17,270 --> 00:02:18,870 But as we learned before 29 30 00:02:18,920 --> 00:02:20,640 if you use ls -a 30 31 00:02:20,870 --> 00: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 32 00:02:26,660 --> 00:02:32,270 to track all your changes, to commit your changes and to perform version control. 32 33 00:02:32,270 --> 00:02:38,060 So we're currently inside the story directory and you can also call this the working directory. 33 34 00:02:38,060 --> 00:02:45,200 So as I mentioned before, using Git and learning about version control it comes with some of its own terminology 34 35 00:02:45,290 --> 00:02:46,430 and language. 35 36 00:02:46,520 --> 00: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 37 00:02:51,320 --> 00:02:55,660 we can all be on the same page and we all understand what's going on. 37 38 00:02:55,670 --> 00:03:01,640 So currently we are inside the working directory which is the story directory. 38 39 00:03:02,000 --> 00:03:07,700 And here in order to start tracking the changes of my files 39 40 00:03:07,700 --> 00:03:09,740 for example chapter1.txt, 40 41 00:03:09,920 --> 00:03:15,320 then I need to add this file to what's called a staging area. 41 42 00:03:15,740 --> 00:03:23,180 And that is basically a intermediate place where you can pick and choose which files inside your working 42 43 00:03:23,180 --> 00:03:26,220 directory that you want to commit. 43 44 00:03:26,270 --> 00:03:33,170 So to see what's currently inside your staging area, you can use the git status command. And it shows 44 45 00:03:33,170 --> 00:03:39,560 you that there are untracked files which will be shown in red and this is something that simply inside 45 46 00:03:39,560 --> 00:03:41,020 your working directory, 46 47 00:03:41,030 --> 00:03:42,270 so inside here, 47 48 00:03:42,470 --> 00:03:45,310 but it's not yet in the staging area. 48 49 00:03:45,560 --> 00: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 50 00:03:52,520 --> 00:03:54,030 command git 50 51 00:03:54,060 --> 00:03:56,840 add. So we're going to type git add 51 52 00:03:57,170 --> 00:03:58,880 and we're going to type the filename. 52 53 00:03:58,880 --> 00:04:01,730 So in this case it's chapter1.txt. 53 54 00:04:01,910 --> 00: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 55 00:04:08,750 --> 00:04:11,770 added as a new file and it is now green. 55 56 00:04:11,780 --> 00:04:16,330 So this is now in the staging area and it's ready to be committed. 56 57 00:04:16,340 --> 00:04:20,210 So let's go ahead and commit this under version control. 57 58 00:04:20,210 --> 00:04:28,360 So the command is git commit and I'm going to use the -m flag to add a commit message. 58 59 00:04:28,430 --> 00:04:31,540 So the commit message is really really important. 59 60 00:04:31,580 --> 00:04:38,010 It's something that helps you keep track of what changes you have made in each commit. 60 61 00:04:38,090 --> 00: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 62 00:04:47,180 --> 00:04:50,300 between the last save point and this current save point. 62 63 00:04:50,300 --> 00:04:58,340 So for our initial commit we can use something very simple like Initial Commit. And this shows that this 63 64 00:04:58,340 --> 00:04:59,830 is our starting point. 64 65 00:05:00,050 --> 00:05:05,870 Alternatively if you want to be slightly more specific because in our case we've actually completed 65 66 00:05:05,870 --> 00:05:06,890 chapter1, 66 67 00:05:07,040 --> 00:05:11,100 so you can write Complete Chapter 1. 67 68 00:05:11,120 --> 00:05:16,490 Now the thing that you'll realize is that usually with commit messages they are written in the present 68 69 00:05:16,490 --> 00:05:18,550 tense and this is the best practice. 69 70 00:05:18,560 --> 00:05:25,160 So whereas it would probably make more sense I guess at least in my head anyways to write completed 70 71 00:05:25,220 --> 00:05:28,090 chapter 1 as this save point, 71 72 00:05:28,190 --> 00:05:32,630 it's actually by convention that you should always use the present tense. 72 73 00:05:32,630 --> 00:05:36,860 So it's like you are doing it now. You're submitting your changes now. 73 74 00:05:36,950 --> 00: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 75 00:05:44,150 --> 00:05:47,230 by using the git log command. 75 76 00:05:47,510 --> 00:05:53,200 So you can see that this commit was made at this time by this person. 76 77 00:05:53,490 --> 00:06:01,730 And it also has a hash and this hash uniquely identifies this particular commit. And then right at the 77 78 00:06:01,730 --> 00:06:06,640 end you see this commit message of what this save point was all about. 78 79 00:06:07,820 --> 00:06:12,260 So now I'm going to go ahead and create two more chapters. 79 80 00:06:12,290 --> 00:06:25,400 So let's just create chapter2.txt and chapter3.txt and now we have three chapters 80 81 00:06:25,670 --> 00:06:29,580 and I'm going to go in and change some of these text files. 81 82 00:06:29,630 --> 00:06:30,980 So let's say... 82 83 00:06:41,260 --> 00:06:43,900 Okay, that's chapter 2 done. 83 84 00:06:43,900 --> 00:06:49,330 And finally let's go ahead and just open chapter 3 and edit that as well. 84 85 00:07:01,090 --> 00:07:01,390 All right. 85 86 00:07:01,400 --> 00:07:03,670 So all three files have been changed. 86 87 00:07:03,710 --> 00: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 88 00:07:09,020 --> 00:07:14,720 be really useful for me to be able to demonstrate to you what Git is doing behind the background. 88 89 00:07:14,750 --> 00:07:20,810 So now let's go ahead and add these two new files to our staging area. 89 90 00:07:20,810 --> 00: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 91 00:07:28,130 --> 00:07:31,940 the working directory and not yet inside the staging area. 91 92 00:07:32,240 --> 00:07:38,580 So we can put it into the staging area by simply adding each of them as we did before, 92 93 00:07:38,600 --> 00:07:45,370 git add and writing something like chapter2.txt and then doing git add chapter3.txt. 93 94 00:07:45,590 --> 00: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 95 00:07:51,890 --> 00:07:53,530 this one by one. 95 96 00:07:53,540 --> 00:07:56,030 So of course there is a better way. 96 97 00:07:56,030 --> 00: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 98 00:08:01,820 --> 00:08:07,010 to specify everything inside this current directory, 98 99 00:08:07,010 --> 00:08:09,780 so everything inside the story directory. 99 100 00:08:10,010 --> 00:08:14,860 So now if I go ahead and hit enter and then let's go to git status again, 100 101 00:08:14,870 --> 00: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 102 00:08:21,290 --> 00:08:26,570 to commit those two files to a new commit to a new save point. 102 103 00:08:26,870 --> 00:08:30,060 And you know what to do. If you're following along with me 103 104 00:08:30,080 --> 00:08:31,420 go ahead and give it a go. 104 105 00:08:35,500 --> 00:08:35,830 All right. 105 106 00:08:35,830 --> 00:08:38,480 So how is that? If you remember, 106 107 00:08:38,500 --> 00:08:39,890 the command is 107 108 00:08:39,940 --> 00: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 109 00:08:49,150 --> 00:08:58,690 that is in the present tense. So let's say complete chapter 2 and 3. 109 110 00:08:58,900 --> 00:09:05,550 So that's everything I've done between the initial commit and this commit 110 111 00:09:05,560 --> 00:09:09,950 the only difference is the fact that I've completed now chapter 2 and chapter 3. 111 112 00:09:10,210 --> 00:09:13,120 So let's go ahead and and hit enter. 112 113 00:09:13,510 --> 00:09:15,720 So again let's check it out 113 114 00:09:15,730 --> 00:09:23,830 using git log. We can see that we now have two commits both with different hashes because they are unique 114 115 00:09:23,890 --> 00:09:24,960 and they are different. 115 116 00:09:25,240 --> 00: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 117 00:09:32,950 --> 00:09:36,060 minutes later I completed chapter 2 and 3 117 118 00:09:36,130 --> 00:09:38,140 and that was the second commit. 118 119 00:09:38,440 --> 00:09:41,170 And this is where we are at right now. 119 120 00:09:41,200 --> 00:09:44,800 So you can see by the head by this word HEAD 120 121 00:09:45,100 --> 00:09:49,420 this is the position or the current state that we are in. 121 122 00:09:49,420 --> 00:09:52,800 So I just want to quickly recap what we've just done. 122 123 00:09:52,900 --> 00:09:58,380 So we created a file in our working directory inside our story directory. 123 124 00:09:58,570 --> 00:10:05,960 So the working directory is the folder or the directory where you initialize your Git repository. 124 125 00:10:06,040 --> 00:10:10,340 When we said git init, we did that inside the story directory. 125 126 00:10:10,390 --> 00:10:12,470 So that becomes a working directory. 126 127 00:10:12,700 --> 00:10:18,850 And from now on Git is going to try and track the changes that it sees between the working directory 127 128 00:10:19,240 --> 00:10:22,020 and the local repository. 128 129 00:10:22,420 --> 00:10:31,020 So in the beginning we created a file inside our working directory inside story and then we used git 129 130 00:10:31,010 --> 00:10:34,270 add to push it to the staging area. 130 131 00:10:34,270 --> 00:10:39,700 Now the reason why there is this intermediate staging area because you might wonder, why not just go 131 132 00:10:39,700 --> 00:10:42,270 from the working directory straight to the repository? 132 133 00:10:42,460 --> 00:10:44,640 Why do we need this extra step? 133 134 00:10:44,800 --> 00: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 135 00:10:52,390 --> 00:10:53,510 committed. 135 136 00:10:53,560 --> 00: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 137 00:10:59,350 --> 00:11:02,070 and what are the things that you want to be tracked. 137 138 00:11:02,140 --> 00: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 139 00:11:09,130 --> 00: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 140 00:11:17,140 --> 00:11:25,660 So now our file is inside our local repository. so that .git and that version is given a name through 140 141 00:11:25,660 --> 00:11:27,310 the commit message. 141 142 00:11:27,310 --> 00: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 143 00:11:35,470 --> 00:11:43,720 control and we can use a special command called git checkout to revert back or rollback to the last 143 144 00:11:43,780 --> 00:11:46,210 position in our local repository. 144 145 00:11:46,330 --> 00: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 146 00:11:52,030 --> 00: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 147 00:11:57,370 --> 00:11:58,370 a big seller. 147 148 00:11:58,390 --> 00: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 149 00:12:06,460 --> 00:12:13,840 everything up and just you know fell asleep on my keyboard and I happened to have saved my file. 149 150 00:12:13,840 --> 00: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 151 00:12:21,190 --> 00:12:28,220 because we have version control and we have Git enabled so we have nothing to worry about. 151 152 00:12:28,540 --> 00:12:33,600 I can actually revert the changes that I've made locally in my working directory. 152 153 00:12:33,730 --> 00:12:39,230 So at this point you can use git status to see that we have modifications in our chapter3. 153 154 00:12:39,350 --> 00:12:44,740 txt file that have not yet been committed or added to the staging area. 154 155 00:12:44,800 --> 00:12:50,790 So if we wanted to we can actually revert this back to it's previous glory. 155 156 00:12:51,190 --> 00: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 157 00:12:57,850 --> 00:13:02,880 version of chapter 3 and the last save point in our Git repository. 157 158 00:13:03,190 --> 00:13:06,760 So to do that you can use the command git diff 158 159 00:13:06,910 --> 00:13:12,390 so the difference, and we'll give it the chapter 3 file name. 159 160 00:13:12,610 --> 00:13:17,140 And if you hit enter, you can see that this is the part that was deleted, 160 161 00:13:17,140 --> 00:13:23,810 so the part in red, and then this was the part that was added which is our gobbledegoop. 161 162 00:13:23,980 --> 00: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 163 00:13:29,890 --> 00: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 164 00:13:35,970 --> 00:13:41,340 I did certain things previously and change my current file. 164 165 00:13:41,670 --> 00:13:46,860 But other times, it might be that you know it's just you want to touch the new file. 165 166 00:13:46,860 --> 00:13:49,070 You just don't want anything to do with it 166 167 00:13:49,080 --> 00:13:52,860 and you would much rather roll back to the previous version. 167 168 00:13:52,920 --> 00: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 169 00:13:59,310 --> 00:14:00,650 useful for you. 169 170 00:14:00,750 --> 00:14:03,000 So git checkout 170 171 00:14:03,150 --> 00:14:07,890 and then we're going to specify the name of the file that we want to checkout which is chapter3. 171 172 00:14:07,890 --> 00:14:14,520 txt. And if you just watch over here which is the preview of the current version of chapter3.txt, 172 173 00:14:15,120 --> 00:14:22,980 once I hit enter on this command that basically asks to roll back this chapter 3 to the last version 173 174 00:14:23,010 --> 00:14:26,520 that was committed in our local repository. 174 175 00:14:26,520 --> 00:14:33,840 So if I hit enter, you can see that almost immediately my chapter 3 has been restored to its previous 175 176 00:14:34,380 --> 00:14:35,550 glorious state. 176 177 00:14:35,760 --> 00:14:41,340 And this is the version of chapter 3 at the last check point at which I committed it. 177 178 00:14:41,580 --> 00:14:45,940 So that was this one which is completed chapter 2 and 3. 178 179 00:14:45,960 --> 00:14:53,820 So whereas in this lesson we've looked mostly at local implementations of Git so saving these versions 179 180 00:14:53,910 --> 00:14:56,810 on our computer locally, in the next lesson 180 181 00:14:56,820 --> 00:15:02,520 I'm going to talk about GitHub and creating remote repositories. So I'll see you there.