Ignore#
It sometimes useful to declare a group of files that should be ingored by Git.
Get ignored#
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 is defined a .gitignore file, which tells git to ignore any file that matches `file_to_be_ignored*’.
%init
cat << EOF > .gitignore
file_to_be_ignored*
EOF
The following cell defines two types of files. One group corresponds to the pattern defined in the .gitignore; the other does not.
touch file_to_be_ignored{1..3}
touch other_file{1..3}
git status -s
?? .gitignore
?? other_file1
?? other_file2
?? other_file3
The result is that the file_to_be_ignored group is not represented in the git status -s output. The following cell runs the git status --ignored:
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)
There is now a special section Ignored files.
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 cell defines the the git repo that ignores any path that contains the symb_comb entry.
%init
cat << EOF > .gitignore
*symb_comb*
EOF
echo "content" > regular_file
echo "content" > regular_symb_comb_file
mkdir this_symb_comb_contains
echo "content" > this_symb_comb_contains/file
tree
.
├── regular_file
├── regular_symb_comb_file
└── this_symb_comb_contains
└── file
2 directories, 3 files
The project contains a file with symb_comb in its name, as well as regular file stored in folder with symb_comb in its name. The status of the repo:
git status --ignored -s
?? .gitignore
?? regular_file
!! regular_symb_comb_file
!! this_symb_comb_contains/
Any path containing the restricted entry in its name is marked as ignored.
.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.
Consider the example where craeted a subfolder and the .gitignore that bans any name file is placed just in it.
%init
touch file
mkdir subfolder
touch subfolder/file
cat << EOF > subfolder/.gitignore
*file*
EOF
As a result, it marked a file as ignored only from the subfolder.
git status -s -uall --ignored
?? file
?? subfolder/.gitignore
!! subfolder/file