Chapter 3. Git & GitHub Project Setup

Project Initiator – .gitignore File

Project Initiator – .gitignore File
Tag:

Files that you don't want Git to track

One of the key values of Git is sharing files. There are files or directories which you may not want to share or you don't need to share. For example, the .env file may contain passwords or other secret information. Suppose you keep the file in your project directory. In that case, Git detects the file and it will be stored in the Remote Repository when you push the project directory into the Remote Repository. If you are a Mac user, you may see a .DS_Store file as a hidden file in each directory. This file is used to store custom attributes of the folder on your computer, such as the position of icons or the choice of a background image. This file is also not necessary for a coding project and doesn’t require sharing with other developers.

The .gitignore file

To exclude those files from the Git version control system, Git uses the .gitignore file. The files and directories written in the .gitignore file are excluded from the Git version control system.

Besides the .env file and OS-specific files, log files and package files are also typically written in the .gitignore file.

Executing this is very simple. You just need to do the following actions.

  1. Create a .gitignore file right under the project directory (the same level as .git directory)
  2. In the .gitignore file, list up files or directories you want to exclude from the Git version control system and save the .gitignore file

To understand the concept more clearly, please go through the practice section below.

bloovee-round-icon.pngDeveloper A (Project Owner Role)

Practice

Objective:
Create a .gitignore file and test it

1. Prepare a test file

Create .env file and run the git status command. The git status command gives you the status of the files that are recognized by the Git system.

Command Line - INPUT
touch .env
git status

You can see that the .env file is recognized by the Git system as shown below. The response says "untracked" but these files are already recognized by the Git version control system. The status tells that those files are not added to the Staging Area, which will be explained in more detail later.

Command Line - RESPONSE
On branch master
No commits yet
Untracked files:
  (use "git add ..." to include in what will be committed)
	.env
nothing added to commit but untracked files present (use "git add" to track)

2. Create and edit the .gitignore file

Create the .gitignore file.

Command Line - INPUT
touch .gitignore

You'll see the .gitignore file in the VS Code.

Project-Initiator--gitignore-File

Open the file and edit it by adding .env. For Mac OS, also add .DS_Store. This file is an auto-generated file by Mac OS to store custom attributes of its containing folder, such as folder view options, icon positions, etc. You don't need to add .DS_Store for Windows.

.gitignore
.env
.DS_Store

Make sure that you save the file.

3. Check that the selected files are ignored

Run the git status command to check the files recognized by the Git system

Command Line - INPUT
git status

You can see that the .env file and .DS_Store file are no longer recognized by the Git system as shown below.

Command Line - RESPONSE
On branch master
No commits yet
Untracked files:
  (use "git add ..." to include in what will be committed)
	.gitignore
nothing added to commit but untracked files present (use "git add" to track)

Delete the .env file as it will not be used in the following sections.

(As the .DS_Store file is an auto-generated file by Mac OS, you don’t need to delete it. Even if you delete it, it will be automatically generated again.)

4. Commit the .gitignore file

As the .gitignore file itself needs to be tracked by Git, add the file to the Staging Area and make a commit by running the command below.

Command Line - INPUT
git add .
git commit -m "added .gitignore file"

You can confirm that the file has been committed.

Command Line - RESPONSE
git commit -m "added .gitignore file"
[master acc4aa4] added .gitignore file
 1 file changed, 2 insertions(+)
 create mode 100644 .gitignore

IdeaTips: .gitignore file useful writing rules

Excluding files with a certain extension: if you want to exclude all the files with a specific extension, you can put "*" right before the extension like *exe.

Excluding files under a certain directory: If you want to exclude all the files under a certain directory, you can put "/" right after the directory name like bin/.

IdeaTips: gitignore.io

Listing all necessary items for the .gitignore file may be time-consuming. You can use gitignore.io to check typical files or directories that should be listed in the .gitignore file for a certain type of project.

For example, if you are creating a Django-based app, type 'Django' and click on the Create button.

Project-Initiator--gitignore-File

You'll see a list of files or directories like in the example below. Copy the list and paste it into your .gitignore file.

Project-Initiator--gitignore-File

Files that you don't want Git to track

One of the key values of Git is sharing files. There are files or directories which you may not want to share or you don't need to share. For example, the .env file may contain passwords or other secret information. Suppose you keep the file in your project directory. In that case, Git detects the file and it will be stored in the Remote Repository when you push the project directory into the Remote Repository. If you are a Mac user, you may see a .DS_Store file as a hidden file in each directory. This file is used to store custom attributes of the folder on your computer, such as the position of icons or the choice of a background image. This file is also not necessary for a coding project and doesn’t require sharing with other developers.

The .gitignore file

To exclude those files from the Git version control system, Git uses the .gitignore file. The files and directories written in the .gitignore file are excluded from the Git version control system.

Besides the .env file and OS-specific files, log files and package files are also typically written in the .gitignore file.

Executing this is very simple. You just need to do the following actions.

  1. Create a .gitignore file right under the project directory (the same level as .git directory)
  2. In the .gitignore file, list up files or directories you want to exclude from the Git version control system and save the .gitignore file

To understand the concept more clearly, please go through the practice section below.

bloovee-round-icon.pngDeveloper A (Project Owner Role)

Practice

Objective:
Create a .gitignore file and test it

1. Prepare a test file

Create .env file and run the git status command. The git status command gives you the status of the files that are recognized by the Git system.

Command Line - INPUT
touch .env
git status

You can see that the .env file is recognized by the Git system as shown below. The response says "untracked" but these files are already recognized by the Git version control system. The status tells that those files are not added to the Staging Area, which will be explained in more detail later.

Command Line - RESPONSE
On branch master
No commits yet
Untracked files:
  (use "git add ..." to include in what will be committed)
	.env
nothing added to commit but untracked files present (use "git add" to track)

2. Create and edit the .gitignore file

Create the .gitignore file.

Command Line - INPUT
touch .gitignore

You'll see the .gitignore file in the VS Code.

Project-Initiator--gitignore-File

Open the file and edit it by adding .env. For Mac OS, also add .DS_Store. This file is an auto-generated file by Mac OS to store custom attributes of its containing folder, such as folder view options, icon positions, etc. You don't need to add .DS_Store for Windows.

.gitignore
.env
.DS_Store

Make sure that you save the file.

3. Check that the selected files are ignored

Run the git status command to check the files recognized by the Git system

Command Line - INPUT
git status

You can see that the .env file and .DS_Store file are no longer recognized by the Git system as shown below.

Command Line - RESPONSE
On branch master
No commits yet
Untracked files:
  (use "git add ..." to include in what will be committed)
	.gitignore
nothing added to commit but untracked files present (use "git add" to track)

Delete the .env file as it will not be used in the following sections.

(As the .DS_Store file is an auto-generated file by Mac OS, you don’t need to delete it. Even if you delete it, it will be automatically generated again.)

4. Commit the .gitignore file

As the .gitignore file itself needs to be tracked by Git, add the file to the Staging Area and make a commit by running the command below.

Command Line - INPUT
git add .
git commit -m "added .gitignore file"

You can confirm that the file has been committed.

Command Line - RESPONSE
git commit -m "added .gitignore file"
[master acc4aa4] added .gitignore file
 1 file changed, 2 insertions(+)
 create mode 100644 .gitignore

IdeaTips: .gitignore file useful writing rules

Excluding files with a certain extension: if you want to exclude all the files with a specific extension, you can put "*" right before the extension like *exe.

Excluding files under a certain directory: If you want to exclude all the files under a certain directory, you can put "/" right after the directory name like bin/.

IdeaTips: gitignore.io

Listing all necessary items for the .gitignore file may be time-consuming. You can use gitignore.io to check typical files or directories that should be listed in the .gitignore file for a certain type of project.

For example, if you are creating a Django-based app, type 'Django' and click on the Create button.

Project-Initiator--gitignore-File

You'll see a list of files or directories like in the example below. Copy the list and paste it into your .gitignore file.

Project-Initiator--gitignore-File

Tag: