(E) Creating .gitignore file¶
Goal:
I will learn how to put .gitignore
file in my repository.
Instructions:
In your personal Git central repository, create a
.gitignore
file to eliminate build directories generated by
Qt Creator and files with .pro.user
suffix from version control.
Important
This exercise is highly recommended, since it makes working on this course easier.
It is a good idea to do the exercise, even if its deadline has expired.
Backgrounds¶
There exist files that are not wanted to be stored in version control. For example, all the files in the build folder of Qt Creator can be regenerated by performing build in Qt Creator, if the source code files and project file exist. Therefore it is not reasonable to store the content of the build folder in Git.
We do not want to add the above kind of files by accident, for example,
with the command git add --all
, or see the files with the command
git status
.
The situation can be solved with the file called .gitignore
.
This file is a normal text file, containing a list of all paths and files
that are meant to be left outside version control.
You can create such a file with any text editor.
(For example, from Linux remote desktop, you can find an editor:
Applications -> Accessories -> Text Editor.)
At this point, recall using wild cards and what is meant, for example,
with *.txt
or *~
.
(If you do not remember these things, you can check them from
the second-last chapter Self study: Using Git in command line, section
Command line operations, at Special characters in paths.)
For example, the files ending with .pro.user
include local
settings of a Qt project, and they are not typically intended to
store in Git.
If you add the line:
*.pro.user
in the file .gitignore
, Git will ignore all the files ending
with .pro.user
in sequel.
This is because in the place of the the character *
, any number
(including zero) of any characters are allowed.
If you have earlier stored files ending with .pro.user
in your central
repository, you can remove them with the command git rm
.
Initial step of the assignment¶
In the root of your repository, create two new text files,
.gitignore
and test.pro.user
, in a way you have used to.
In the command line (Terminal), this can be done with the command:
touch .gitignore test.pro.user
What says the command git status
?
Open the file .gitignore
with a text editor and add there the line
*.pro.user
.
See again how the status looks like.
Completing the assignment¶
In your .gitignore
file, add also such a line that prevents
the build directories to be tracked in the version control.
Here we mean all the subdirectories in your home directory that begin
with the text build-
, and after
this text any number of any characters are allowed.
How could you use the character *
now?
You can test the behavior of your gitignore by using command the
git status
.
If you add new files and git status
does not show them as
”Untracked files:”, it means that your gitignore works at least in the
case of that file.
Tips for completing the assignment:
- Make first sure that your repository has no unsaved modifications.
- Create
.gitignore
file in the root of your repository (in the same directory, where you have subdirectoriesExamples
,Student
, andTemplates
). - During the assignment, use the command
git status
to check the status, as guided previously. - Never ever add such files in your
.gitignore
file that you should submit, e.g. files with suffixes.cpp
,.hh
, or.pro
. - If desired, you can write also other lines in your
.gitignore
file to eliminate files with certain type from version control. - The name of the
.gitignore
file begins with a dot. Therefore you cannot see it in the list produced by the command line commandls
, but you can see it with the commandls -a
. Similarly, the file cannot be seen in Files view in the remote desktop, unless you have checked Show Hidden Files.
A+ presents the exercise submission form here.