0 1 00:00:00,730 --> 00: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 2 00:00:06,720 --> 00:00:12,510 to prevent committing certain files to your local and remote Git repositories. 2 3 00:00:12,510 --> 00: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 4 00:00:17,850 --> 00:00:22,800 desktop and we're going to create a new directory here called project. 4 5 00:00:22,800 --> 00:00:25,920 Just say that we have this brand new project. 5 6 00:00:26,050 --> 00: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 7 00:00:36,780 --> 00: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 8 00:00:44,280 --> 00:00:46,510 2.txt, file3.txt 8 9 00:00:46,560 --> 00:00:51,890 and we've also got something called secrets.txt file. 9 10 00:00:52,470 --> 00:00:54,320 So we've now got four files. 10 11 00:00:54,510 --> 00:01:01,110 And just to illustrate the use cases of when we might want to have a gitignore file. 11 12 00:01:01,110 --> 00:01:09,660 So say if you have this file that has some you know secret passwords or API keys basically stuff that 12 13 00:01:09,660 --> 00: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 14 00:01:18,360 --> 00:01:26,490 There have been horror stories of people having their Amazon AWS secret keys in their project and that 14 15 00:01:26,490 --> 00:01:29,600 getting pushed to their remote and GitHub. 15 16 00:01:30,240 --> 00:01:36,860 And within seconds their entire AWS account has been basically used up and wiped clean. 16 17 00:01:37,050 --> 00: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 18 00:01:44,700 --> 00:01:47,820 onto this public platform. 18 19 00:01:47,820 --> 00:01:51,960 So let's say that we've got this secrets file and we've saved it. 19 20 00:01:52,140 --> 00: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 21 00:01:58,230 --> 00:02:04,420 GitHub, files that are to do with your local settings or your user preferences. 21 22 00:02:04,530 --> 00: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 23 00:02:11,160 --> 00:02:14,940 to download and copy into their project folder 23 24 00:02:14,940 --> 00:02:18,470 if they're cloning or if they're forking your projects. 24 25 00:02:18,780 --> 00:02:25,260 So a really common example that people tend to add to their gitignore files are what are called DS_ 25 26 00:02:25,290 --> 00:02:32,430 Store files. S DS_Store files are basically a settings file that saves certain things like you know how 26 27 00:02:32,430 --> 00:02:36,370 you like your icons to be arranged in a particular project folder. 27 28 00:02:36,630 --> 00: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 29 00:02:41,960 --> 00:02:45,010 And then I'm going to move my files around. 29 30 00:02:45,090 --> 00:02:49,290 Let's say that this is just my preference for how I want my files to be shown 30 31 00:02:49,290 --> 00: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 32 00:02:57,080 --> 00:02:58,000 your finder. 32 33 00:02:58,170 --> 00: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 34 00:03:06,300 --> 00:03:11,720 files that are inside our project folder, and you can see there it is DS_Store. 34 35 00:03:11,910 --> 00: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 36 00:03:18,180 --> 00:03:22,890 GitHub remote repository or anybody else's computer with that file. 36 37 00:03:22,920 --> 00:03:25,730 So we're going to add that to our gitignore as well. 37 38 00:03:25,800 --> 00:03:32,160 So getting on to the point of using and creating-- of creating and using a gitignore file, 38 39 00:03:32,160 --> 00: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 40 00:03:40,020 --> 00:03:40,700 file, 40 41 00:03:40,710 --> 00:03:45,700 so using the '.' in front of the file name and it's going to be code gitignore. 41 42 00:03:45,900 --> 00: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 43 00:03:52,470 --> 00:03:53,160 file. 43 44 00:03:53,160 --> 00:03:57,160 So make sure that when you're using it, it looks exactly like this. 44 45 00:03:57,180 --> 00:03:58,610 So let's create that file 45 46 00:03:58,710 --> 00: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 47 00:04:05,010 --> 00:04:10,930 and we can also open it in the terminal by simply saying open .gitignore. And 47 48 00:04:11,310 --> 00:04:14,640 that opens it and your default editor 48 49 00:04:14,640 --> 00:04:16,370 so for example TextEdit. 49 50 00:04:16,650 --> 00:04:22,380 And here is where we add the files that should be ignored when we commit our project to Git. 50 51 00:04:22,380 --> 00:04:27,490 So let me first show you what happens when we don't have anything inside our gitignore. 51 52 00:04:27,600 --> 00: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 53 00:04:32,910 --> 00:04:34,950 setting up Git and committing it. 53 54 00:04:34,950 --> 00: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 55 00:04:44,100 --> 00:04:50,640 and then I'm going to use git add . to add all the files that are currently inside this directory 55 56 00:04:51,030 --> 00:04:52,800 to Git the staging area. 56 57 00:04:52,920 --> 00: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 58 00:05:00,710 --> 00:05:04,280 of these files are added into the staging area and 58 59 00:05:04,290 --> 00:05:05,940 they're going to be committed. 59 60 00:05:06,200 --> 00:05:09,480 If I go ahead and do git commit 60 61 00:05:09,590 --> 00:05:11,230 so that's not what we want. 61 62 00:05:11,410 --> 00:05:14,520 And instead I don't want to commit DS_Store 62 63 00:05:14,550 --> 00:05:18,130 and I also don't want to commit my secrets.txt file. 63 64 00:05:18,410 --> 00:05:22,450 So first things first I'm going to basically undo what I've just done. 64 65 00:05:22,460 --> 00: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 66 00:05:30,110 --> 00:05:33,490 on once I've gitignore set up. 66 67 00:05:33,500 --> 00:05:36,400 So basically we're just undoing the last step. 67 68 00:05:36,590 --> 00: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 69 00:05:45,110 --> 00:05:50,210 and I'm going to use a . to say that everything inside the current directory should be removed from 69 70 00:05:50,210 --> 00:05:51,240 the staging area. 70 71 00:05:51,440 --> 00: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 72 00:05:57,950 --> 00: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 73 00:06:04,340 --> 00: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 74 00:06:10,250 --> 00:06:14,240 that we want to be ignored when we're adding and committing to Git. 74 75 00:06:14,510 --> 00:06:21,570 So in order to add individual files you can simply specify the file name on each and every new line. 75 76 00:06:21,800 --> 00:06:26,570 So for example if we wanted to ignore the DS_Store we can simply .DS_Store 76 77 00:06:26,780 --> 00:06:32,010 so exactly the same upper and lower casing as the name of the file. 77 78 00:06:32,420 --> 00: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 79 00:06:41,450 --> 00:06:43,220 on a new line. 79 80 00:06:43,250 --> 00:06:46,070 Now with gitignore it has certain rules. 80 81 00:06:46,070 --> 00:06:51,000 So for example you can use the # to comment. 81 82 00:06:51,350 --> 00:06:58,480 So for example you can say you know, #pod files right? to be ignored. 82 83 00:06:58,490 --> 00:07:01,240 Another thing you can do is use wildcards. 83 84 00:07:01,310 --> 00:07:08,510 So if you use the asterisks and you say *.text that means that when you commit your project all 84 85 00:07:08,510 --> 00:07:12,540 of the files that have a text extension will be ignored. 85 86 00:07:13,010 --> 00: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 87 00:07:20,150 --> 00:07:26,990 to my-- to Git but I want to know the DS_Store and the secrets.txt files. 87 88 00:07:27,350 --> 00:07:31,730 So now we're ready to add our files to our staging area. 88 89 00:07:31,730 --> 00:07:38,430 So let's again use git add . and use git status to see what's been added. 89 90 00:07:38,600 --> 00: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 91 00:07:44,660 --> 00:07:50,230 as the secrets.txt are ignored as per the specifications in our git 91 92 00:07:50,240 --> 00:07:51,920 ignore file. 92 93 00:07:51,950 --> 00: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 94 00:08:02,930 --> 00:08:05,940 my local commit are going to be these four 94 95 00:08:06,050 --> 00:08:12,980 and similarly if I push this to GitHub it will also be only these four files. 95 96 00:08:12,980 --> 00: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 97 00:08:21,620 --> 00:08:22,790 Xcode projects. 97 98 00:08:22,970 --> 00: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 99 00:08:32,430 --> 00:08:33,260 changes. 99 100 00:08:33,270 --> 00: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 101 00:08:42,840 --> 00:08:48,130 to clear my terminal and cd into my root. 101 102 00:08:48,420 --> 00:08:54,340 And also I'm going to open this new test folder that we've created just now. 102 103 00:08:54,330 --> 00: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 104 00:09:05,730 --> 00: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 105 00:09:14,790 --> 00:09:18,090 will be ignored for our Xcode project. 105 106 00:09:18,090 --> 00:09:21,420 Now if you go to github.com/github, 106 107 00:09:21,430 --> 00:09:25,150 so this is a repository owned by the GitHub team, 107 108 00:09:25,200 --> 00:09:28,050 they have a repository called gitignore. 108 109 00:09:28,170 --> 00:09:35,250 And it's basically a pre-made collection of useful gitignore templates and if you have a search in 109 110 00:09:35,250 --> 00:09:42,120 here you can find that you've got Swift and you've also got Objective-C. 110 111 00:09:42,660 --> 00: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 112 00:09:50,910 --> 00:09:57,760 for some of the files that you probably won't want to upload to GitHub or any other remote repository. 112 113 00:09:58,050 --> 00:10:05,610 So these have the file extensions for things that are user defaults or user settings for example pbx 113 114 00:10:05,610 --> 00: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 115 00:10:15,540 --> 00:10:19,180 your gitignore file that you created earlier on. 115 116 00:10:19,230 --> 00:10:21,250 So just straight up paste. 116 117 00:10:21,300 --> 00: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 118 00:10:26,250 --> 00:10:32,360 comments and these ones are extensions that you would ignore and you might want to add things like DS_ 118 119 00:10:32,370 --> 00:10:34,050 Store into here as well 119 120 00:10:34,050 --> 00:10:37,070 so you can have something like project wide. 120 121 00:10:37,170 --> 00:10:41,500 So if for example that DS_Store that we mentioned earlier on. And 121 122 00:10:41,670 --> 00:10:43,230 if you hit save, 122 123 00:10:43,230 --> 00: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 124 00:10:54,110 --> 00: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 125 00:11:05,670 --> 00:11:06,860 There we go. 125 126 00:11:07,450 --> 00: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 127 00:11:14,330 --> 00:11:21,200 common user related files that are not going to be useful to your collaborators or anybody who wants 127 128 00:11:21,200 --> 00:11:23,180 to contribute on GitHub. 128 129 00:11:23,210 --> 00:11:26,780 So that was just a quick tidbit about using gitignore. 129 130 00:11:27,110 --> 00: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 131 00:11:32,870 --> 00:11:38,420 should you put your sensitive data? And you have to make sure that when you're uploading your projects 131 132 00:11:38,750 --> 00:11:44,960 with these secret keys and passwords to make sure that you set up a gitignore file and to include 132 133 00:11:44,960 --> 00:11:47,440 those things in there. 133 134 00:11:47,450 --> 00:11:50,300 All right so that's all for this lesson. 134 135 00:11:50,300 --> 00:11:56,690 Now in the next lesson I want to talk about git clone and how to clone various repositories to your 135 136 00:11:56,690 --> 00:11:58,210 local system. 136 137 00:11:58,310 --> 00:12:00,970 So all of that and more in the next lesson, 137 138 00:12:01,190 --> 00:12:01,580 so I'll see you there.