In almost every project, we should tell Git to ignore
certain files and directories. For example, we don't want to
include lock files or binary files that get generated as a
result of compiling our code. And adding those files is just
going to increase the size of our repository without providing
any values. every developer can have their own lock files,
right? So lock files are not something we want to share and
synchronize with other team members. So for this demo, this
creates a directory called logs, and then add a log file here.
Once again, we can use the echo command to write hello to logs
slash dev deadlock. Now let's run git status. So getting
saying that we have an untracked directory called logs. But we
don't want to add this to the staging area, because we don't
want Git to track this. So to prevent this, we have to create
a special file called dot Git ignore. So this file has no
name. It only has an extension and it should be in the root of
your project. So let's echo logs forward slash to dot, git
ignore. Now I'm going to open this file using VS code. So
code, git ignore. So in this file, we have a single entry
logs, forward slash, which indicates a directory, we can
list as many files and directories we want here. For
example, we can include main dot log, we can also use patterns,
like all log files, and so on. Once we are done, we save the
changes back in the terminal. Now, if you run git status one
more time, it no longer says that we have a new directory
called locks, because it's ignoring it. Instead, it says,
we have a new file called dot Git ignore. So let's add this
file to the staging area and then commit our code. So add,
git ignore. So this is how we can ignore files and directories
and get. Just remember, this only works if you haven't
already included a file or a directory in your repository. In
other words, if you accidentally include a file in your
repository, and then later added to Git ignore, Git is not gonna
ignore that. Let me show you. So let's create a new directory
called ban. And let's imagine that this directory contains our
compiled source code. So using the echo command, I'm going to
write hello to ban slash app that bear. Now let's run git
status. So we have a new directory. Now we want to
accidentally commit this to our repository. So we add all the
changes, and then come in our code
admin.
Here's the problem. Every time it compiled our code, Git is
going to say that this file, bin slash after bin is changed. So
we have to stage it and then commit it. It doesn't make
sense. Why do we have to commit this file every time we compile
our application? So back to Git ignore? Let's add the bin
directory here as well. Back in the terminal, let's run git
status. So we have modified Git ignore beautiful, let's date and
commit this change. So git add, period. And then git commit,
include in slash, and Git ignore. Now in this case, Git is
not going to ignore the changes in this directory, because it's
already tracking this directory. So let's modify our bin file by
saying echo hello world to bin slash app, that bin get status.
Look good is saying that this file is modified. This is not
what we want. To solve this problem, we have to remove this
file from the staging area, which is what we're proposing
for the next commit. So earlier we talked about get LS dash
files. This command shows the files in our staging area. So as
you can see, this bin file or the bin directory is already in
the staging area. We should remove it here How? Well earlier
we talked about the Git remote command. I told you that with
this Command, we can remove a file or a directory from both
the working directory as well as the staging area. But in this
case, we don't want to remove this file from our working
directory, because that's how we launch our application. So we
want to remove this file only from the staging area, how well
let's add dash H for a quick help. So we type Git RM and then
we can add zero or more options. Here we have this option called
dash dash cash. And with this we can remove stuff only from the
index index is the alternate for the staging area. So when you
look at Git documentation, most of the time you see index. So
using this option, we can remove the bin directory from the
index. So Git RM dash dash cached bin for slash. Now we get
an error saying not removing bin recursively without dash R. So
one more time. Let's look at the help for this command. We have
another option called dash r for recursive removal. So we've been
removed The entire bin directory from the staging area. To do
that we type Git RM, dash dash cash dash r bin, forward slash.
Beautiful. Now this entire directory is removed from the
staging area. Let's verify. So get LS files, our bin directory
is no longer here. Now let's run git status. Look, we have one
change that is ready to be committed. This directory is
deleted from our staging area. So let's commit the change and
remove the bin directory that was accidentally committed.
Okay, from this point forward, Git is no longer going to track
the changes in this directory. So if we echo test to bin slash
after bin, you can see our working directory is still
clean, we don't have any changes. We can verify it using
git status as well. So this is how we can ignore files and
directories and Git Now if you head over to github.com, slash
GitHub slash category, you can see various Git ignore templates
for different programming languages. For example, let's
look at the template for Java. So for Java projects, it's a
great idea to exclude all the class files, because these files
get automatically generated when you compile your code, so
there's no need to include them in your repository. So here we
have various patterns, like all the class files, or all the log
files, the lines that start with a high sign. These are comments
so they get ignored by Git