WEBVTT 0 00:00.730 --> 00:06.420 Alright guys. In this lesson we're going to be talking all about the gitignore and how you can set rules 1 00:06.720 --> 00:12.510 to prevent committing certain files to your local and remote Git repositories. 2 00:12.510 --> 00:17.850 So first things first, let's bring up terminal and what we're going to do is we're going to cd into our 3 00:17.850 --> 00:22.800 desktop and we're going to create a new directory here called project. 4 00:22.800 --> 00:25.920 Just say that we have this brand new project. 5 00:26.050 --> 00:36.660 And now if we go into our project and we create a whole bunch of files to simulate this example project. 6 00:36.780 --> 00:44.280 So let's say we've got something called file1.txt and we've got a whole bunch of other ones, file 7 00:44.280 --> 00:46.510 2.txt, file3.txt 8 00:46.560 --> 00:51.890 and we've also got something called secrets.txt file. 9 00:52.470 --> 00:54.320 So we've now got four files. 10 00:54.510 --> 01:01.110 And just to illustrate the use cases of when we might want to have a gitignore file. 11 01:01.110 --> 01:09.660 So say if you have this file that has some you know secret passwords or API keys basically stuff that 12 01:09.660 --> 01:18.100 you don't want to be committed especially you don't want it to be hosted on an open platform like GitHub. 13 01:18.360 --> 01:26.490 There have been horror stories of people having their Amazon AWS secret keys in their project and that 14 01:26.490 --> 01:29.600 getting pushed to their remote and GitHub. 15 01:30.240 --> 01:36.860 And within seconds their entire AWS account has been basically used up and wiped clean. 16 01:37.050 --> 01:44.310 So it's really really important to think about some of these things and to be aware of what you're putting 17 01:44.700 --> 01:47.820 onto this public platform. 18 01:47.820 --> 01:51.960 So let's say that we've got this secrets file and we've saved it. 19 01:52.140 --> 01:58.230 Another type of file that you might want to add in a gitignore or you might want to not upload to 20 01:58.230 --> 02:04.420 GitHub, files that are to do with your local settings or your user preferences. 21 02:04.530 --> 02:11.160 There's a whole bunch of these types of utility files that you don't really want another person to have 22 02:11.160 --> 02:14.940 to download and copy into their project folder 23 02:14.940 --> 02:18.470 if they're cloning or if they're forking your projects. 24 02:18.780 --> 02:25.260 So a really common example that people tend to add to their gitignore files are what are called DS_ 25 02:25.290 --> 02:32.430 Store files. S DS_Store files are basically a settings file that saves certain things like you know how 26 02:32.430 --> 02:36.370 you like your icons to be arranged in a particular project folder. 27 02:36.630 --> 02:41.840 So let's say if we go in here and we just have a look at arrange by-- arrange by none. 28 02:41.960 --> 02:45.010 And then I'm going to move my files around. 29 02:45.090 --> 02:49.290 Let's say that this is just my preference for how I want my files to be shown 30 02:49.290 --> 02:57.090 inside this project folder. Now the DS_Store file is a hidden file so you won't be able to see it inside 31 02:57.080 --> 02:58.000 your finder. 32 02:58.170 --> 03:06.300 But of course we as command line experts know that we can simply do a ls -a to see all of the hidden 33 03:06.300 --> 03:11.720 files that are inside our project folder, and you can see there it is DS_Store. 34 03:11.910 --> 03:17.790 So that's not something that's going to be of any use to anybody else and we don't want to have to clutter our 35 03:18.180 --> 03:22.890 GitHub remote repository or anybody else's computer with that file. 36 03:22.920 --> 03:25.730 So we're going to add that to our gitignore as well. 37 03:25.800 --> 03:32.160 So getting on to the point of using and creating-- of creating and using a gitignore file, 38 03:32.160 --> 03:40.010 the first thing you have to do is to make it right? And to do that which is going to create a hidden 39 03:40.020 --> 03:40.700 file, 40 03:40.710 --> 03:45.700 so using the '.' in front of the file name and it's going to be code gitignore. 41 03:45.900 --> 03:52.470 Now the case and the wording matters a huge deal because we're using Git and it's looking for the specific 42 03:52.470 --> 03:53.160 file. 43 03:53.160 --> 03:57.160 So make sure that when you're using it, it looks exactly like this. 44 03:57.180 --> 03:58.610 So let's create that file 45 03:58.710 --> 04:04.520 and of course it doesn't appear because it's a hidden file but we can see it using ls -a 46 04:05.010 --> 04:10.930 and we can also open it in the terminal by simply saying open .gitignore. And 47 04:11.310 --> 04:14.640 that opens it and your default editor 48 04:14.640 --> 04:16.370 so for example TextEdit. 49 04:16.650 --> 04:22.380 And here is where we add the files that should be ignored when we commit our project to Git. 50 04:22.380 --> 04:27.490 So let me first show you what happens when we don't have anything inside our gitignore. 51 04:27.600 --> 04:32.910 So I'm just going to save it, it's completely empty. And I'm going to go through the normal process of 52 04:32.910 --> 04:34.950 setting up Git and committing it. 53 04:34.950 --> 04:43.400 So first if you remember I have to use git init to initialize an empty Git repository inside my projects directory 54 04:44.100 --> 04:50.640 and then I'm going to use git add . to add all the files that are currently inside this directory 55 04:51.030 --> 04:52.800 to Git the staging area. 56 04:52.920 --> 05:00.710 So hit enter and we can have a look at what's been added by using git status and you can see that all 57 05:00.710 --> 05:04.280 of these files are added into the staging area and 58 05:04.290 --> 05:05.940 they're going to be committed. 59 05:06.200 --> 05:09.480 If I go ahead and do git commit 60 05:09.590 --> 05:11.230 so that's not what we want. 61 05:11.410 --> 05:14.520 And instead I don't want to commit DS_Store 62 05:14.550 --> 05:18.130 and I also don't want to commit my secrets.txt file. 63 05:18.410 --> 05:22.450 So first things first I'm going to basically undo what I've just done. 64 05:22.460 --> 05:30.110 So I'm going to remove all of these files from my staging area and then I'm going to add them later 65 05:30.110 --> 05:33.490 on once I've gitignore set up. 66 05:33.500 --> 05:36.400 So basically we're just undoing the last step. 67 05:36.590 --> 05:45.010 So to do that I'm going to write git rm -cached and I'm going to use the r flag for recursive 68 05:45.110 --> 05:50.210 and I'm going to use a . to say that everything inside the current directory should be removed from 69 05:50.210 --> 05:51.240 the staging area. 70 05:51.440 --> 05:57.910 So now if I hit enter, you can see that all of these things have been removed from the Git staging area. 71 05:57.950 --> 06:04.340 And if I do a git status again you can see that all of these files are no longer tracked to be committed. 72 06:04.340 --> 06:09.980 So now this is the point we're going to use that git ignore file and we're going to add the file names 73 06:10.250 --> 06:14.240 that we want to be ignored when we're adding and committing to Git. 74 06:14.510 --> 06:21.570 So in order to add individual files you can simply specify the file name on each and every new line. 75 06:21.800 --> 06:26.570 So for example if we wanted to ignore the DS_Store we can simply .DS_Store 76 06:26.780 --> 06:32.010 so exactly the same upper and lower casing as the name of the file. 77 06:32.420 --> 06:41.420 And if we wanted to ignore the secrets.txt file then all we have to do is just to specify it in here 78 06:41.450 --> 06:43.220 on a new line. 79 06:43.250 --> 06:46.070 Now with gitignore it has certain rules. 80 06:46.070 --> 06:51.000 So for example you can use the # to comment. 81 06:51.350 --> 06:58.480 So for example you can say you know, #pod files right? to be ignored. 82 06:58.490 --> 07:01.240 Another thing you can do is use wildcards. 83 07:01.310 --> 07:08.510 So if you use the asterisks and you say *.text that means that when you commit your project all 84 07:08.510 --> 07:12.540 of the files that have a text extension will be ignored. 85 07:13.010 --> 07:19.820 All right. So I'm going to get rid of that because I do want to commit the file1 and file2 and file3 86 07:20.150 --> 07:26.990 to my-- to Git but I want to know the DS_Store and the secrets.txt files. 87 07:27.350 --> 07:31.730 So now we're ready to add our files to our staging area. 88 07:31.730 --> 07:38.430 So let's again use git add . and use git status to see what's been added. 89 07:38.600 --> 07:44.660 So you can see that the only files I added are the gitignore, the file1, file2, file3. But the DS_Store as well 90 07:44.660 --> 07:50.230 as the secrets.txt are ignored as per the specifications in our git 91 07:50.240 --> 07:51.920 ignore file. 92 07:51.950 --> 08:02.890 So now if I go ahead and commit this as my initial commit, then the only files that are going to be put onto 93 08:02.930 --> 08:05.940 my local commit are going to be these four 94 08:06.050 --> 08:12.980 and similarly if I push this to GitHub it will also be only these four files. 95 08:12.980 --> 08:21.590 So now let's create a new Xcode project and let me show you how you would add a gitignore file for your 96 08:21.620 --> 08:22.790 Xcode projects. 97 08:22.970 --> 08:32.430 So let's just call this test and I'm going to create it inside my desktop. And now let's just make a few 98 08:32.430 --> 08:33.260 changes. 99 08:33.270 --> 08:42.840 Let's drag on an image view onto our view controller and now I'm going to save and exit Xcode and I'm going 100 08:42.840 --> 08:48.130 to clear my terminal and cd into my root. 101 08:48.420 --> 08:54.340 And also I'm going to open this new test folder that we've created just now. 102 08:54.330 --> 09:05.320 So let's cd into desktop/test so that we are right here and I'm going to create a gitignore 103 09:05.730 --> 09:14.790 file. Then we're going to open our gitignore and in here I'm going to add some of the files that 104 09:14.790 --> 09:18.090 will be ignored for our Xcode project. 105 09:18.090 --> 09:21.420 Now if you go to github.com/github, 106 09:21.430 --> 09:25.150 so this is a repository owned by the GitHub team, 107 09:25.200 --> 09:28.050 they have a repository called gitignore. 108 09:28.170 --> 09:35.250 And it's basically a pre-made collection of useful gitignore templates and if you have a search in 109 09:35.250 --> 09:42.120 here you can find that you've got Swift and you've also got Objective-C. 110 09:42.660 --> 09:50.580 So we're going to go inside the Swift gitignore file and you can see that this is a pre-built template 111 09:50.910 --> 09:57.760 for some of the files that you probably won't want to upload to GitHub or any other remote repository. 112 09:58.050 --> 10:05.610 So these have the file extensions for things that are user defaults or user settings for example pbx 113 10:05.610 --> 10:15.450 user and it's basically all premade for you so you can simply just copy all of this and put it into 114 10:15.540 --> 10:19.180 your gitignore file that you created earlier on. 115 10:19.230 --> 10:21.250 So just straight up paste. 116 10:21.300 --> 10:26.220 And if you remember from earlier on we said that the lines that start off with a pound sign are the 117 10:26.250 --> 10:32.360 comments and these ones are extensions that you would ignore and you might want to add things like DS_ 118 10:32.370 --> 10:34.050 Store into here as well 119 10:34.050 --> 10:37.070 so you can have something like project wide. 120 10:37.170 --> 10:41.500 So if for example that DS_Store that we mentioned earlier on. And 121 10:41.670 --> 10:43.230 if you hit save, 122 10:43.230 --> 10:52.480 now if we go into terminal and we're inside our test folder so we're going to do a git init, git add, 123 10:54.110 --> 11:04.430 git status to see what's going to be committed. And then we're going to say git commit -m initial commit. 124 11:05.670 --> 11:06.860 There we go. 125 11:07.450 --> 11:14.210 And now once you're ready to commit and add all of this to your remote then it will ignore all of these 126 11:14.330 --> 11:21.200 common user related files that are not going to be useful to your collaborators or anybody who wants 127 11:21.200 --> 11:23.180 to contribute on GitHub. 128 11:23.210 --> 11:26.780 So that was just a quick tidbit about using gitignore. 129 11:27.110 --> 11:32.870 And I know that some you guys have been asking about you know, where should you put your API keys? Where 130 11:32.870 --> 11:38.420 should you put your sensitive data? And you have to make sure that when you're uploading your projects 131 11:38.750 --> 11:44.960 with these secret keys and passwords to make sure that you set up a gitignore file and to include 132 11:44.960 --> 11:47.440 those things in there. 133 11:47.450 --> 11:50.300 All right so that's all for this lesson. 134 11:50.300 --> 11:56.690 Now in the next lesson I want to talk about git clone and how to clone various repositories to your 135 11:56.690 --> 11:58.210 local system. 136 11:58.310 --> 12:00.970 So all of that and more in the next lesson, 137 12:01.190 --> 12:01.580 so I'll see you there.