0 1 00:00:00,720 --> 00:00:06,000 All right, guys. In this lesson, we're going to be talking all about the gitignore and how you can set 1 2 00:00:06,000 --> 00:00:12,570 rules to prevent committing certain files to your local and remote git repositories. 2 3 00:00:12,570 --> 00:00:17,910 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,910 --> 00:00:22,800 Desktop and we're going to create a new directory here called Project. 4 5 00:00:22,860 --> 00:00:26,000 Just say that we have this brand-new project. 5 6 00:00:26,120 --> 00:00:36,810 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,810 --> 00:00:44,330 So let's say, we've got something called file1.txt and we've got a whole bunch of other ones, file2, 7 8 00:00:44,360 --> 00:00:46,610 file3, 8 9 00:00:46,620 --> 00:00:52,500 and we've also got something called secrets file. 9 10 00:00:52,500 --> 00:00:54,290 So we've now got four files. 10 11 00:00:54,540 --> 00:01:01,170 And just to illustrate the use cases of when we might want to have a gitignore file, 11 12 00:01:01,170 --> 00:01:09,720 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,720 --> 00:01:17,580 you don't want to be committed, especially you don't want it to be hosted on an open platform like 13 14 00:01:17,580 --> 00:01:18,150 GitHub. 14 15 00:01:18,430 --> 00:01:26,550 There have been horror stories of people having their Amazon AWS secret keys in their project and that 15 16 00:01:26,550 --> 00:01:29,580 getting pushed to that remote on GitHub, 16 17 00:01:30,300 --> 00:01:37,060 and within seconds, their entire AWS account has been, basically, used up and wiped clean. 17 18 00:01:37,110 --> 00:01:44,370 So it's really really important to think about some of these things and to be aware of what you're putting 18 19 00:01:44,830 --> 00:01:47,850 onto this public platform. 19 20 00:01:47,880 --> 00:01:52,020 So let's say that we've got this secret file and we've saved it. 20 21 00:01:52,200 --> 00:01:58,260 Another type of file that you might want to add to a getignore or you might want to not upload to 21 22 00:01:58,270 --> 00:02:04,510 GitHub are files that are to do with your local settings or your user preferences. 22 23 00:02:04,590 --> 00:02:11,220 There's a whole bunch of these types of utility files that you don't really want another person to have 23 24 00:02:11,220 --> 00:02:14,990 to download and copy into their Project folder 24 25 00:02:15,000 --> 00:02:18,550 if they're cloning or if they're forking your projects. 25 26 00:02:18,840 --> 00:02:25,340 So a really common example that people tend to add to their gitignore files are what are called 26 27 00:02:25,350 --> 00:02:32,670 DS_Store files. So DS_Store files are basically a settings file that saves certain things like, you know, how you 27 28 00:02:32,670 --> 00:02:36,420 like your icons be arranged in a particular Project folder. 28 29 00:02:36,690 --> 00:02:41,960 So let's say, if we go in here, we just have a look at Arranged By, arranged by None, 29 30 00:02:42,030 --> 00:02:44,790 and then I'm going to move my files around, 30 31 00:02:45,150 --> 00:02:49,220 and let's say that this is just my preference for how I want my files to be shown 31 32 00:02:49,350 --> 00:02:51,500 inside this Project folder. 32 33 00:02:51,840 --> 00:02:58,100 Now, the DS_Store file is a hidden file, so you won't be able to see it inside your Finder. 33 34 00:02:58,230 --> 00:03:06,360 But, of course, we, as command line experts, know that we can simply do a "ls -a" to see all of the hidden 34 35 00:03:06,360 --> 00:03:11,760 files that are inside our project folder. And you can see, there it is, DS_Store. 35 36 00:03:11,940 --> 00:03:17,820 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 36 37 00:03:18,280 --> 00:03:22,930 our GitHub remote repository or anybody else's computer with that file, 37 38 00:03:22,950 --> 00:03:25,600 So we're going to add that to our gitignore as well. 38 39 00:03:25,860 --> 00:03:32,050 So getting on to the point of using and creating, of creating and using, a gitignore file, 39 40 00:03:32,220 --> 00:03:40,080 The first thing you have to do is to make it, right? And to do that, which is going to create a hidden 40 41 00:03:40,080 --> 00:03:40,760 file, 41 42 00:03:40,770 --> 00:03:45,740 so using the dot in front of the file name, and it's going to be called .gitignore. 42 43 00:03:45,930 --> 00:03:52,530 Now, the case and the wording matters a huge deal because we're using git and it's looking for the specific 43 44 00:03:52,530 --> 00:03:53,220 file, 44 45 00:03:53,220 --> 00:03:57,190 so make sure that when you're using it, it looks exactly like this. 45 46 00:03:57,210 --> 00:03:58,710 So let's create that file. 46 47 00:03:58,770 --> 00:04:05,370 And, of course, it doesn't appear because it's a hidden file, but we can see it using "ls -a" and we 47 48 00:04:05,370 --> 00:04:13,410 can also open it in the Terminal by simply saying, "open .gitignore," and that opens it and your default 48 49 00:04:14,010 --> 00:04:14,640 editor. 49 50 00:04:14,700 --> 00:04:16,390 So, for example, TextEdit. 50 51 00:04:16,680 --> 00:04:22,170 And here is where we add the files that should be ignored when we commit our project to Git. 51 52 00:04:22,440 --> 00:04:26,760 So let me first show you what happens when we don't have anything inside 52 53 00:04:26,790 --> 00:04:27,540 gitignore. 53 54 00:04:27,660 --> 00:04:32,970 So I'm just going to save it. It's completely empty. And I'm going to go through the normal process of 54 55 00:04:32,970 --> 00:04:35,010 setting up git and committing it. 55 56 00:04:35,010 --> 00:04:42,870 So, first, if you remember, I have to use "git init" to initialize an empty git repository inside my Project's 56 57 00:04:42,870 --> 00:04:45,650 directory, and then I'm going to use 57 58 00:04:45,700 --> 00:04:52,900 "git add," dot to add all the files that are currently inside this directory to the git staging area. 58 59 00:04:52,950 --> 00:04:59,080 So hit enter and we can have a look at what's been added by using git status. 59 60 00:04:59,340 --> 00:05:04,340 And you can see that all of these files are added into the staging area 60 61 00:05:04,340 --> 00:05:09,650 and they're going to be committed if I go ahead and do "git commit." 61 62 00:05:09,650 --> 00:05:11,270 So that's not what we want. 62 63 00:05:11,470 --> 00:05:14,550 And instead, I don't want to commit the DS_Store 63 64 00:05:14,600 --> 00:05:18,450 and I also don't want to commit my secrets.txt file. 64 65 00:05:18,470 --> 00:05:24,740 So first things first, I'm going to basically undo what I've just done, so I'm going to remove all of 65 66 00:05:24,740 --> 00:05:32,830 these files from my staging area, and then I'm going to add them later on once I've got my gitignore 66 67 00:05:32,830 --> 00:05:33,540 setup. 67 68 00:05:33,560 --> 00:05:36,380 So basically, we're just doing the last step. 68 69 00:05:36,650 --> 00:05:44,870 So to do that, I'm going to write "git rm --cached" and I'm going to use the -r flag for recursive, 69 70 00:05:45,150 --> 00:05:50,240 and I'm going to use a dot to say that everything inside the current directory should be removed from 70 71 00:05:50,240 --> 00:05:51,280 the staging area. 71 72 00:05:51,470 --> 00:05:57,980 So, now if I hit enter, you can see that all of these things have been removed from git staging area. 72 73 00:05:58,010 --> 00:06:04,400 And if I do a git status again, you can see that all of these files are no longer tracked to be committed. 73 74 00:06:04,400 --> 00:06:10,040 So, now this is the point where we're going to use that gitignore file and we're going to add the file names 74 75 00:06:10,310 --> 00:06:14,560 that we want to be ignored when we're adding and committing to Git. 75 76 00:06:14,570 --> 00:06:21,860 So in order to add individual files, you can simply specify the file name on each and every new line. 76 77 00:06:21,860 --> 00:06:26,580 So, for example, if we want to ignore the DS_Store, we can simply write DS_Store, 77 78 00:06:26,820 --> 00:06:32,070 so exactly the same upper and lower casing as the name of the file. 78 79 00:06:32,450 --> 00:06:41,480 And if we wanted to ignore the secrets.txt file, then all we have to do is just to specify it in here 79 80 00:06:41,510 --> 00:06:43,280 on a new line. 80 81 00:06:43,310 --> 00:06:46,130 Now, with gitignore, it has certain rules. 81 82 00:06:46,130 --> 00:06:51,040 So, for example, you can use the pound sign to comment. 82 83 00:06:51,410 --> 00:06:58,510 So, for example, you can say you know Pod files, right, to be ignored. 83 84 00:06:58,520 --> 00:07:01,310 Another thing you can do is use wildcards. 84 85 00:07:01,370 --> 00:07:08,570 So if you use the asterisks and you say "*.text," that means that you commit your project, all 85 86 00:07:08,570 --> 00:07:13,070 of the files that have a text extension will be ignored. 86 87 00:07:13,070 --> 00:07:19,880 All right, so I'm going to get rid of that because I do want to commit the file1 and file2 and file3 87 88 00:07:20,210 --> 00:07:27,410 to my two git, but I wanted to ignore the DS_Store and the secrets.text files. 88 89 00:07:27,410 --> 00:07:31,790 So, now that we're ready to add our files to our staging area, 89 90 00:07:31,790 --> 00:07:38,490 so let's, again, use "git add ." and use git status to see what's been added. 90 91 00:07:38,660 --> 00:07:43,410 So you can see that the only files that are added are the gitignore, the file 1, 2, 3, 91 92 00:07:43,460 --> 00:07:50,290 but the DS_Store as well as the secrets.txt are ignored as per the specifications 92 93 00:07:50,300 --> 00:07:51,970 in our gitignore file. 93 94 00:07:52,010 --> 00:08:02,890 So, now if I go ahead and commit this as my "Initial Commit," then the only files are going to be put onto 94 95 00:08:02,990 --> 00:08:05,990 my local, commit are going to be these four. 95 96 00:08:06,110 --> 00:08:12,970 And similarly, if I push this to GitHub, it will also be only these four files. 96 97 00:08:13,040 --> 00:08:22,070 So, now let's create a new Xcode project and let me show you how you would add a gitignore file for your Xcode 97 98 00:08:22,080 --> 00:08:22,990 projects. 98 99 00:08:23,030 --> 00:08:32,480 So let's just call this Test and I'm going to create it inside my desktop. Now, let's just make a few 99 100 00:08:32,480 --> 00:08:33,310 changes. 100 101 00:08:33,330 --> 00:08:37,640 Just drag on an Image View onto our View Controller. 101 102 00:08:37,880 --> 00:08:48,110 And now I'm going to save and exit Xcode and I'm going to clear my Terminal and cd into my root, 102 103 00:08:48,470 --> 00:08:54,380 and also I'm going to open this new Test folder that we've created just now. 103 104 00:08:54,380 --> 00:09:05,360 So let's cd into desktop/Test so that we are right here, and I'm going to create a gitignore 104 105 00:09:05,780 --> 00:09:14,840 file, and we're going to open out gitignore. And in here, I'm going to add some of the files that 105 106 00:09:14,840 --> 00:09:18,150 will be ignored for our Xcode project. 106 107 00:09:18,170 --> 00:09:21,480 Now, if you go to github.com/github, 107 108 00:09:21,480 --> 00:09:25,190 so this is a repository owned by the GitHub team, 108 109 00:09:25,250 --> 00:09:28,080 they have a repository called gitignore. 109 110 00:09:28,220 --> 00:09:35,300 And it's basically a premade collection of useful gitignore templates. And if you have a search in 110 111 00:09:35,300 --> 00:09:42,210 here, you can find that you've got Swift and you've also got Objective-C. 111 112 00:09:42,710 --> 00:09:50,630 So we're going to go inside the Swift gitignore file, and you can see that this is a prebuilt template 112 113 00:09:50,960 --> 00:09:57,900 for some of the files that you probably won't want to upload to GitHub or any other remote repository. 113 114 00:09:58,130 --> 00:10:05,660 So these have the file extensions for things that are user default or user settings, for example, PBX user, 114 115 00:10:05,660 --> 00:10:15,500 and it's basically all premade for you, so you can simply just copy all of this and put it into 115 116 00:10:15,590 --> 00:10:19,280 your gitignore file that you created earlier on. 116 117 00:10:19,280 --> 00:10:21,310 So just straight up, paste. 117 118 00:10:21,350 --> 00:10:26,270 And if you remember from earlier on, we said that the lines that start off with a pound sign are the 118 119 00:10:26,300 --> 00:10:32,390 comments, and these ones are extensions that you would ignore, and you might want to add things like 119 120 00:10:32,430 --> 00:10:33,990 DS_Store into here as well, 120 121 00:10:34,100 --> 00:10:37,110 so you can have something like Project-wide. 121 122 00:10:37,220 --> 00:10:41,610 So, for example, the DS_Store that we mentioned earlier on, 122 123 00:10:41,720 --> 00:10:43,260 and if you hit save-- 123 124 00:10:43,280 --> 00:10:52,430 Now, if we go into Terminal and we're inside our Test folder, so we're going to do a "git init, git add, 124 125 00:10:54,160 --> 00:11:03,700 git status" to see what's going to be committed, and then we're going to say "git commit-m 125 126 00:11:04,030 --> 00:11:07,020 "Initial Commit." There we go. 126 127 00:11:07,520 --> 00:11:14,260 And now once you're ready to commit and add all of this to your remote, then it will ignore all of these 127 128 00:11:14,380 --> 00:11:21,250 common user related files that are not going to be useful to your collaborators or anybody who wants 128 129 00:11:21,250 --> 00:11:23,230 to contribute on GitHub. 129 130 00:11:23,260 --> 00:11:29,440 So that was just a quick tidbit about using gitignore. And I know that some of you guys have been asking 130 131 00:11:29,710 --> 00:11:35,710 about, you know, where should you put your API keys, where should you put your sensitive data, and you have 131 132 00:11:35,710 --> 00:11:42,340 to make sure that when you're uploading your projects with these secret keys and passwords to make sure 132 133 00:11:42,340 --> 00:11:47,500 that you set up a gitignore file, and to include those things in there. 133 134 00:11:47,500 --> 00:11:50,280 All right, so that's all for this lesson. 134 135 00:11:50,350 --> 00:11:56,740 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,740 --> 00:11:58,360 local system. 136 137 00:11:58,360 --> 00:12:01,010 So all of that and more in the next lesson, 137 138 00:12:01,100 --> 00:12:01,630 so I'll see you there.