Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to validate .gitignore file

Tags:

git

gitignore

Is there a way to validate a .gitignore file so you quickly find doubles paths or paths that don't exist anymore? Normally when a project is small this isn't really necessary but with lots of branches and mergings of .gitignore files this can come in hand.

like image 983
Olivier de Jonge Avatar asked Nov 13 '14 11:11

Olivier de Jonge


People also ask

How do you check if git ignore is working?

Check if a single File is ignored by git To do this, execute the check-ignore command with the file path. It will output the file name if it is ignored by git.

How do I show a .gitignore file?

Git Ignoring Files and Folders Checking if a file is ignored From Git 1.7. 6 onwards you can also use git status --ignored in order to see ignored files. You can find more info on this in the official documentation or in Finding files ignored by . gitignore.

How do I know my Gitignore?

gitignore file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo. / will ignore directories with the name.

Should Gitignore file be checked in?

Yes, you can track the . gitignore file, but you do not have to. The main reason of having this file into repository is to have everyone working on the project, ignoring same files and folders. Also see this: Should you commit .

How do I test gitignore?

Each pattern in a particular.gitignore file is tested relative to the directory containing that file. However the convention, and simplest approach, is to define a single.gitignore file in the root. As your.gitignore file is checked in, it is versioned like any other file in your repository and shared with your teammates when you push.

What is gitignore and how to add it to your project?

Gitignore Explained: What is Gitignore and How to Add it to Your Repo The.gitignore file is a text file that tells Git which files or folders to ignore in a project. A local.gitignore file is usually placed in the root directory of a project.

How do I change the default gitignore settings?

To add or change your global .gitignore file, run the following command: This will create the file ~/.gitignore_global. Now you can edit that file the same way as a local .gitignore file. All of your Git repositories will ignore the files and folders listed in the global .gitignore file.

How to check if a file is ignored or not in Git?

Git has an inbuilt command named check-ignore to check if a file is ignored or not. To validate the .gitignore file, first, we will look at an example .gitignore file. Also, you can take a look at the gitignore templates for reference. Now let’s look at different scenarios using the above .gitignore file as an example.


3 Answers

Not exactly.
One command which could come close would be git check-ignore

Choose a file you know is supposed to be ignored, and check the output of:

git check-ignore -v -- /path/to/ignored/file

You will see the rules of your .gitignore which apply.


Update March 2016: Git 2.8 will add a new way to debug .gitignore files and their rules.

In the context of allowing a sub-folder to not be ignored (even if its parent folder is ignored: see example here), I have found this gem by Thái Ngọc Duy (pclouds):

dir.c: support tracing exclude

man git includes:

GIT_TRACE_EXCLUDE:

Enables trace messages that can help debugging .gitignore processing.
See 'GIT_TRACE' for available trace output options.

With GIT_TRACE_EXCLUDE set to 1, you will see (after a git status) stderr debug messages like:

exclude: from ...
exclude: xxx => n/a
exclude: xxx vs. yyy at line z: => www
like image 159
VonC Avatar answered Dec 28 '22 00:12

VonC


In order to also validate git's ** pattern in paths I had to write a one-liner inspired by the script above:

find . -type f | git check-ignore -v --stdin | perl -pe 's,.*\.gitignore:,,; s,\t," "x100,e' | sort | uniq -w 100 -c | perl -pe 's, {100}.*,,'

Not exactly pretty but it works.

like image 33
kPshi Avatar answered Dec 28 '22 00:12

kPshi


You can do a script to check it. I have made one for you there:

#!/bin/bash
set -o noglob
for file in `cat .gitignore | grep -v \#`
do
    printf "$file"
    find . -name "$file" | wc -l
done

it will display the rules followed by the number of match in the current directory and recursively. Example:

*.log     31
*.gz      0
*~     42
*.swp      0
*.aux     33
*.pdf     51
*.out      7
*.toc      6
*.nav      1
*.snm      1
.DS_Store      0

You could restrict the output to the line containing 0 by piping into egrep "\b0\b" if you want.

like image 42
M'vy Avatar answered Dec 27 '22 23:12

M'vy