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