How git ignore works#
Check ignored files#
To list files that match any pattern in your .gitignore
, use the --ignored
option to the git status
command. All files to be ignored will be listed in the Ignored files:
section.
In the next cell I define a .gitignore' file, which tells git to ignore any file that matches
file_to_be_ignored*’.
%%writefile ignore_files/check_ignored_files/.gitignore
file_to_be_ignored*
Overwriting ignore_files/check_ignored_files/.gitignore
So lets see how it looks. In the following cell I create some files, some matching file_to_be_ignored
, some not, and then run git status
with and without --ignored
.
With the --ignored
option, you can see the file_to_be_ignored*
files in the Ignored files:
section.
%%bash
cd ignore_files/check_ignored_files
git init &> /dev/null
touch file_to_be_ignored{1..3}
touch other_file{1..3}
echo "=====git status====="
git status
echo
echo "=====git status --ignored====="
git status --ignored
rm -r .git
=====git status=====
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
other_file1
other_file2
other_file3
nothing added to commit but untracked files present (use "git add" to track)
=====git status --ignored=====
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
other_file1
other_file2
other_file3
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
file_to_be_ignored1
file_to_be_ignored2
file_to_be_ignored3
nothing added to commit but untracked files present (use "git add" to track)
Any entry#
Using the syntax *<symbol_combination>*
you can specify a symbol combination and any file with that symbol combination in the relative filepath will be ignored by git.
The following example shows:
Files tree and
.gitignore
, which tells git to ignore any file withsymb_comb
in it’s filepath;The results of the
git status
of a freshly created repository, which helps us understand exactly what files git is ignoring.
There are few important fatures:
Of course,
some_folder2
doesn’t havesymb_comb
in it’s filepath, but it only has one file and that file should be ignored by git, so to git this folder appears empty;Git ignores files in directories started or ended with
symb_comb
.
%%bash
cd ignore_files/any_entry
git init &> /dev/null
echo "=====FILES TREE====="
tree
echo
echo
echo "=====.GITIGNORE FILE====="
cat .gitignore
echo
echo
echo "=====GIT STATUS====="
git status
rm -r .git
=====FILES TREE=====
.
├── some_folder
│ └── some_file
└── some_random_file
1 directory, 2 files
=====.GITIGNORE FILE=====
*symb_comb*
=====GIT STATUS=====
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
some_folder/
some_random_file
nothing added to commit but untracked files present (use "git add" to track)
.gitignore in subfolders#
You can add a .gitignore
file in any subdirectory of your repository - git will interpret this and ignore all files, and you should describe the files relative to the subdirectory path.
For example, let’s look at creating a repository based on the ignore_files/subfolder
folder. Suppose there is also a test_subfolder
folder, and there is some special work being done there that requires the some_file_in_subfolder
file to be ignored. So you can just add a .gitignore
file to test_subfolder
and git will automatically ignore any files mentioned in test_subfolder/.gitinore
from test_subfolder
.
In the following cells, this is what happens to the file test_subfolder/some_file_in_subfolder
, and git status --ignored
shows this file as ignored.
%%writefile ignore_files/subfolders/test_subfolder/.gitignore
some_file_in_subfolder
Overwriting ignore_files/subfolders/test_subfolder/.gitignore
%%writefile ignore_files/subfolders/test_subfolder/some_file_in_subfolder
test file content
Writing ignore_files/subfolders/test_subfolder/some_file_in_subfolder
%%bash
cd ignore_files/subfolders
git init &> /dev/null
git add --all
git status --ignored
rm -r .git
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test_subfolder/.gitignore
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
test_subfolder/some_file_in_subfolder