All right, 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,
file2, file3, and we've also got something called, secrets 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 projects and that
getting pushed to their remote on 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.
Another type of file that you might want to add into a gitignore or you might want to not upload to
GitHub, are files that are to do with your local settings or your user preferences.
And 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 is called DS_Store
files.
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 and 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 an 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.
Getting on to the point of creating and using a gitignore file, the first thing you have to do is
to make it, right?
And to do that, we're just going to create a hidden file.
So using the dot (.) in front of the file name and it's going to be called ".gitignore".
Now the case and the wording matters a huge deal because we're using Git and it's looking for this specific
file.
So make sure that when you're using it, it looks exactly like this.
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 inside VS Code like this.
Here is where we add the files that should be ignored when we commit our project to Git.
Let me first show you what happens when we don't have anything inside our gitignore.
And I'm going to go through the normal process of setting up Git and committing it. First,
if you remember, I have to use "git init" to initialize an empty Git repository inside my Project's directory.
And then I'm going to use "git add ." to add all the files that are currently inside this directory
to the Git 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 the DS_Store and I also don't want to commit
my secrets.txt
file.
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 got my gitignore setup.
Basically, we're just undoing the last step.
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 "." (dot) 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 where we're going to use that gitignore file and we're going to add the file
names that we want to be ignored when we're adding and committing to Git.
In order to add individual files, you can simply specify the file name on each and every new line.
So, for example, if we want to ignore the DS_Store, we can simply write, ".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 pound sign (#) to comment.
So for example, you can say, you know, "#Log files," right? "To be ignored" .
Another thing you can do is use wildcards.
So if you use the asterisks (*) and you say *.txt, that means that when you commit your
project, all of the files that have a .txt 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 Git, but I want it to ignore the DS_Store and the secrets.txt files.
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 that are added are the
gitignore, the file 1,2,3, but the DS_Store, as well as, the secrets.txt are ignored as per the specifications
in our gitignore file.
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.
Now, let's open a new Node project, and let me show you how you would add a gitignore file for your
Node projects.
So let's just call this test and I'm going to put it inside my desktop.
And now let's install the Node modules using npm.
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 Node project.
If you go to github.com/github, so this is a repository owned by the GitHub team.
They have a repository called gitgnore, 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 Node, 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 Node modules or environment settings and it's
basically all pre-made 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 for example, the .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.
That was just a quick tidbit about using gitignore, and I know that some of 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.
In the next lesson, I want to talk about Git Clone and how to clone various repositories to your local
system.
So for all of that and more in the next lesson.
So I'll see you there.