Now there are certainly going to be times that we need to have
Git ignore certain files within our repository.
They are created by the system, and as such,
they don't really make up our repository.
We can have moved into our project directory, whatever we've called it.
Once we're in our project directory, we can create a subdirectory,
and then we can create a very simple Python file in there.
Now if you want to save time on typing,
you don't have to create the directory or the Python file or even set
the execute permission that we're going to do later.
I've supplied the Python directory inside of our CompTIA automation repository,
so you can easily copy it across from there.
We'll see that in a demonstration.
If you've created the file yourself, you can add in the execute permission.
If we check git status, what we should see,
even if we're working in our top‑level directory of the repo,
that we're able to see that the Python directory has changed.
Using git add dot, we've got that convenience,
and we're going to include all changed files or stage
those files from subdirectories as well.
So that's the idea of adding this in at the moment.
We can go through and run our commit and with its message.
So we're good to go, but we haven't executed our file at this stage.
And now comes the fun part.
If we had executed our script,
we might have found that a cache directory had been created.
The cache directory is there to store compiled versions of your script.
And this is how we control it.
At the top level of our repository, we create the .gitignore file.
We can add to it the __pycache__ entry,
which is then saying we're excluding the pycache directory where
the compiled files are going to be located.
We can add other files in here, but at this stage,
this is all we really need for our project.
Anything in the .gitignore file is not going to be
included to your Git repository.
We can force compilation of our Python scripts using python and the
compileall module pointing through to our python subdirectory.
Now with that done,
we can simply use the ls command to list the content of
our python subdirectory, and we're able to see the pycache
directory has been created.
But then taking a look at the output of git status,
we're going to see that we haven't included it, it's excluded, so
we have successfully ignored this directory.
Now the best way to learn then is diving out to our command line. We'll
implement the .gitignore file, add our Python code,
commit files from the subdirectory, and then compile our Python code.
And here we are at our glorious command line. Now we might as well
start off within our project and check our status, so that confirms the
branch that we are working with.
We want to be working on our master branch.
We can go through, and as I said, we can copy the python directory into our
project directory. And we can see there that we've got the directory python,
and inside of that python directory we've got a very simple Python file that
will just print the date and time.
You can, of course, create it, and the details of that was in the slides.
If I go through now and take a look at git status, even though the files
are underneath the python subdirectory, we can see that the python
subdirectory is listed as changed. Using the git add and dot becomes a
very friendly and convenient way to add everything in. And then checking
the git status now, we can see then to the staging area we've added in
python and my.py. So say from a point of view of just demonstration,
I wanted to be able to show you that we can use then the git add dot
that does include everything, including subdirectories.
If we go through then and run our commit message so we
can save these back to our repository,
and we can just put a message like adding a python file. So
we're good to go as far as this is concerned.
But if we executed this file, and certainly if we forced a compilation, we're
going to have that pycache directory, which shouldn't be part of our project.
So, again, we can see that everything is up to date, we've nothing to commit
back, so our tree is said to be clean. And we can go ahead and start setting up
the git ignore process. So we can have Git put its fingers in its proverbial is
so it doesn't notice what's going on. But obviously, we can control what it's
listening to, and we do that through the mechanism of the .gitignore file. We
place this in the top level of our project directory, so .gitignore. Obviously,
you can use any text editor to create it. And we can specify then line by line
in the file the files or directories we want to ignore.
So I'm going to add in __pycache__. I just put the forward slash. It's a
good indication to me that it's a directory. It's not required, but then
indicates to me that that's a directory.
We can also include patterns like this.
So if we didn't want SWP files to be included,
we could do something like *. swp.
Well, we don't need this for our project, but it just shows you
what we can do, what we can add in to these .gitignore files.
With that saved, it too is part of our project and needs to be
included within our project.
So if I look at git status,
we can see the .gitignore file there. So we can go ahead and make sure that
this is all saved and committed. But before we do that,
what we can do is go through and actually start proving
that if we execute our Python code, or at least if we go
through and force compilation, we are going to then create these pycache files.
So I've executed it now.
You see just displays a simple date and time. When I go
through and look at my python directory,
we can see that, well, true enough,
this was a simple enough file, so there was no compilation done or it didn't
need to store for later execution the compilation plan. But I can force a
compilation, so I can use python3 ‑m compileall on the python directory, and we
can see there that it's created then our compilation file, and that compilation
file will be in the pycache directory.
If we list the contents of that pycache directory,
we can see then that we've got our .pyc file.
So this is what we don't want to be in our project. We've added the
pycache directory into our .gitignore file so we can see even though
this has changed, it's not being listed as an untracked file. So I can
easily then go through and do a git add dot. That's only going to add
in my .gitignore file.
If I check the status, it's just one new file .gitignore.
We are ignoring the __pycache__ directory. And then, of course,
I can save all of this back into my repository. So we can just go
through and say in our message that we've added the .gitignore file.
And hit our big Enter key, and we're done.
So we've demonstrated that by creating the .gitignore file,
we can specify file patterns that we don't want to be included within our project.