Alright guys. In this lesson we're going to be talking all about the gitignore and how you can set rules
to prevent committing certain files to your local and remote Git repositories.
So first things first, let's bring up terminal and what we're going to do is we're going to cd into our
desktop and we're going to create a new directory here called project.
Just say that we have this brand new project.
And now if we go into our project and we create a whole bunch of files to simulate this example project.
So let's say we've got something called file1.txt and we've got a whole bunch of other ones, file
2.txt, file3.txt
and we've also got something called secrets.txt file.
So we've now got four files.
And just to illustrate the use cases of when we might want to have a gitignore file.
So say if you have this file that has some you know secret passwords or API keys basically stuff that
you don't want to be committed especially you don't want it to be hosted on an open platform like GitHub.
There have been horror stories of people having their Amazon AWS secret keys in their project and that
getting pushed to their remote and GitHub.
And within seconds their entire AWS account has been basically used up and wiped clean.
So it's really really important to think about some of these things and to be aware of what you're putting
onto this public platform.
So let's say that we've got this secrets file and we've saved it.
Another type of file that you might want to add in a gitignore or you might want to not upload to
GitHub, files that are to do with your local settings or your user preferences.
There's a whole bunch of these types of utility files that you don't really want another person to have
to download and copy into their project folder
if they're cloning or if they're forking your projects.
So a really common example that people tend to add to their gitignore files are what are called DS_
Store files. S DS_Store files are basically a settings file that saves certain things like you know how
you like your icons to be arranged in a particular project folder.
So let's say if we go in here and we just have a look at arrange by-- arrange by none.
And then I'm going to move my files around.
Let's say that this is just my preference for how I want my files to be shown
inside this project folder. Now the DS_Store file is a hidden file so you won't be able to see it inside
your finder.
But of course we as command line experts know that we can simply do a ls -a to see all of the hidden
files that are inside our project folder, and you can see there it is DS_Store.
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
GitHub remote repository or anybody else's computer with that file.
So we're going to add that to our gitignore as well.
So getting on to the point of using and creating-- of creating and using a gitignore file,
the first thing you have to do is to make it right? And to do that which is going to create a hidden
file,
so using the '.' in front of the file name and it's going to be code gitignore.
Now the case and the wording matters a huge deal because we're using Git and it's looking for the specific
file.
So make sure that when you're using it, it looks exactly like this.
So let's create that file
and of course it doesn't appear because it's a hidden file but we can see it using ls -a
and we can also open it in the terminal by simply saying open .gitignore. And
that opens it and your default editor
so for example TextEdit.
And here is where we add the files that should be ignored when we commit our project to Git.
So let me first show you what happens when we don't have anything inside our gitignore.
So I'm just going to save it, it's completely empty. And I'm going to go through the normal process of
setting up Git and committing it.
So first if you remember I have to use git init to initialize an empty Git repository inside my projects directory
and then I'm going to use git add . to add all the files that are currently inside this directory
to Git the staging area.
So hit enter and we can have a look at what's been added by using git status and you can see that all
of these files are added into the staging area and
they're going to be committed.
If I go ahead and do git commit
so that's not what we want.
And instead I don't want to commit DS_Store
and I also don't want to commit my secrets.txt file.
So first things first I'm going to basically undo what I've just done.
So I'm going to remove all of these files from my staging area and then I'm going to add them later
on once I've gitignore set up.
So basically we're just undoing the last step.
So to do that I'm going to write git rm -cached and I'm going to use the r flag for recursive
and I'm going to use a . to say that everything inside the current directory should be removed from
the staging area.
So now if I hit enter, you can see that all of these things have been removed from the Git staging area.
And if I do a git status again you can see that all of these files are no longer tracked to be committed.
So now this is the point we're going to use that git ignore file and we're going to add the file names
that we want to be ignored when we're adding and committing to Git.
So in order to add individual files you can simply specify the file name on each and every new line.
So for example if we wanted to ignore the DS_Store we can simply .DS_Store
so exactly the same upper and lower casing as the name of the file.
And if we wanted to ignore the secrets.txt file then all we have to do is just to specify it in here
on a new line.
Now with gitignore it has certain rules.
So for example you can use the # to comment.
So for example you can say you know, #pod files right? to be ignored.
Another thing you can do is use wildcards.
So if you use the asterisks and you say *.text that means that when you commit your project all
of the files that have a text extension will be ignored.
All right. So I'm going to get rid of that because I do want to commit the file1 and file2 and file3
to my-- to Git but I want to know the DS_Store and the secrets.txt files.
So now we're ready to add our files to our staging area.
So let's again use git add . and use git status to see what's been added.
So you can see that the only files I added are the gitignore, the file1, file2, file3. But the DS_Store as well
as the secrets.txt are ignored as per the specifications in our git
ignore file.
So now if I go ahead and commit this as my initial commit, then the only files that are going to be put onto
my local commit are going to be these four
and similarly if I push this to GitHub it will also be only these four files.
So now let's create a new Xcode project and let me show you how you would add a gitignore file for your
Xcode projects.
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
changes.
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
to clear my terminal and cd into my root.
And also I'm going to open this new test folder that we've created just now.
So let's cd into desktop/test so that we are right here and I'm going to create a gitignore
file. Then we're going to open our gitignore and in here I'm going to add some of the files that
will be ignored for our Xcode project.
Now if you go to github.com/github,
so this is a repository owned by the GitHub team,
they have a repository called gitignore.
And it's basically a pre-made collection of useful gitignore templates and if you have a search in
here you can find that you've got Swift and you've also got Objective-C.
So we're going to go inside the Swift gitignore file and you can see that this is a pre-built template
for some of the files that you probably won't want to upload to GitHub or any other remote repository.
So these have the file extensions for things that are user defaults or user settings for example pbx
user and it's basically all premade for you so you can simply just copy all of this and put it into
your gitignore file that you created earlier on.
So just straight up paste.
And if you remember from earlier on we said that the lines that start off with a pound sign are the
comments and these ones are extensions that you would ignore and you might want to add things like DS_
Store into here as well
so you can have something like project wide.
So if for example that DS_Store that we mentioned earlier on. And
if you hit save,
now if we go into terminal and we're inside our test folder so we're going to do a git init, git add,
git status to see what's going to be committed. And then we're going to say git commit -m initial commit.
There we go.
And now once you're ready to commit and add all of this to your remote then it will ignore all of these
common user related files that are not going to be useful to your collaborators or anybody who wants
to contribute on GitHub.
So that was just a quick tidbit about using gitignore.
And I know that some you guys have been asking about you know, where should you put your API keys? Where
should you put your sensitive data? And you have to make sure that when you're uploading your projects
with these secret keys and passwords to make sure that you set up a gitignore file and to include
those things in there.
All right so that's all for this lesson.
Now in the next lesson I want to talk about git clone and how to clone various repositories to your
local system.
So all of that and more in the next lesson,
so I'll see you there.