Introduction
In this lab, you will learn how to use the git check-ignore command to determine if a file is being ignored by Git. You will create a .gitignore file, add patterns to it, and then use git check-ignore to verify which files are being ignored and why.
You will also explore how different patterns in the .gitignore file affect which files are excluded from tracking, gaining a deeper understanding of how Git handles ignored files.
Run git check-ignore File
In this step, we will learn how to use the git check-ignore command. This command is very useful for understanding why Git is ignoring certain files or directories. It helps you debug your .gitignore file.
First, let's make sure we are in our project directory. Open your terminal and type:
cd ~/project/my-time-machine
Now, let's create a file that we expect Git to ignore. We'll create a file named temp.log. Often, log files are ignored in Git repositories.
echo "This is a temporary log file." > temp.log
Next, let's create a .gitignore file and add a pattern to ignore .log files.
echo "*.log" > .gitignore
Now, we can use git check-ignore to see if Git is ignoring temp.log.
git check-ignore temp.log
If Git is ignoring the file, the command will output the pattern from the .gitignore file that matches the file. You should see output similar to this:
.gitignore:1:*.log temp.log
This output tells us that the file temp.log is being ignored because of the pattern *.log on line 1 of the .gitignore file.
If the file were not ignored, the command would produce no output.
Understanding git check-ignore is crucial for managing your project's files effectively. It prevents you from accidentally committing files you don't want in your repository, like temporary files, build artifacts, or sensitive configuration files.
Check .gitignore Patterns
In this step, we will explore how different patterns in .gitignore affect which files are ignored. The .gitignore file uses specific patterns to tell Git which files or directories to exclude from tracking.
Make sure you are still in the ~/project/my-time-machine directory.
Let's add a few more files and modify our .gitignore file to see how different patterns work.
First, create some new files:
mkdir build
echo "This is a build artifact." > build/output.txt
echo "Another log file." > error.log
echo "A temporary file." > temp.dat
echo "A configuration file." > config.ini
Now, let's edit the .gitignore file to include more patterns. We can use the nano editor for this.
nano .gitignore
Inside the nano editor, you should see *.log. Add the following lines below it:
build/
*.dat
config.ini
Your .gitignore file should now look like this:
*.log
build/
*.dat
config.ini
Press Ctrl + X, then Y, and Enter to save and exit nano.
Now, let's use git check-ignore with the -v flag to see which pattern is ignoring each file. The -v flag provides verbose output, showing the matching pattern and the source file (.gitignore in this case).
git check-ignore -v temp.log error.log build/output.txt temp.dat config.ini
You should see output similar to this:
.gitignore:1:*.log temp.log
.gitignore:1:*.log error.log
.gitignore:2:build/ build/output.txt
.gitignore:3:*.dat temp.dat
.gitignore:4:config.ini config.ini
This output confirms that:
temp.loganderror.logare ignored by the*.logpattern.build/output.txtis ignored because the entirebuild/directory is ignored.temp.datis ignored by the*.datpattern.config.iniis ignored by theconfig.inipattern.
Understanding these patterns is essential for keeping your repository clean and focused only on the files you want to track.
Test Non-Ignored Files
In this step, we will use git check-ignore to confirm that files that are not ignored by .gitignore produce no output. This helps solidify your understanding of how the command works and how to verify that Git is tracking the files you intend it to.
Ensure you are in the ~/project/my-time-machine directory.
We already have the message.txt file from a previous lab. This file is not listed in our .gitignore file, so Git should not ignore it.
Let's use git check-ignore on message.txt:
git check-ignore message.txt
As expected, this command should produce no output. This indicates that message.txt is not being ignored by any pattern in your .gitignore file or any other ignore rules.
Now, let's create another file that we don't intend to ignore:
echo "This file should be tracked." > important_file.txt
And check its ignore status:
git check-ignore important_file.txt
Again, you should see no output, confirming that important_file.txt is not ignored.
Finally, let's use git status to see how Git sees these files.
git status
You should see output similar to this:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: .gitignore
Untracked files:
(use "git add <file>..." to include in what will be committed)
important_file.txt
message.txt
nothing added to commit but untracked files present (use "git add" to track)
Notice that important_file.txt and message.txt are listed under "Untracked files". This means Git sees them but isn't currently tracking their changes. The ignored files (temp.log, error.log, build/output.txt, temp.dat, config.ini) are not listed here because Git is explicitly told to ignore them.
This step reinforces the concept that git check-ignore is a tool to verify ignore rules, and files not matched by ignore rules will appear as "Untracked" in git status until you explicitly add them for tracking with git add.
Summary
In this lab, we learned how to use the git check-ignore command to determine if a specific file is being ignored by Git. We practiced creating a .gitignore file, adding patterns to it, and then using git check-ignore to verify that the intended files were indeed being ignored. The output of git check-ignore helped us understand which pattern in the .gitignore file was causing the file to be ignored.
We also explored how different patterns in the .gitignore file affect which files are ignored. By creating various files and modifying the .gitignore file with different patterns, we gained a better understanding of how Git interprets these patterns to exclude files and directories from tracking. This knowledge is essential for effectively managing which files are included in a Git repository.



