(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.

Attention

This exercise is highly recommended, since it makes working on this course easier. The exercise is not quite necessary, and you need not do it right now. You can come back to the exercise later, when you realize how important a .gitignore file is. (The deadline of the exercise is very late in the spring.)

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” at ”Command line operations”.)

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 does not concern those files that have earlier been stored in Git. (If you want to get rid of this type of files stored earlier, you must 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, 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.

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.
  • 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.

A+ presents the exercise submission form here.