Search code examples
gitgitignore

Is there a difference between folder/ and folder/*


Now I did try to search for this, but searching for punctuation is often difficult. But equally I feel sure this would have been answered before so please point out any duplicate answers to this already if there are!

My quetsion is in a .gitignore file is there any difference between:

.gitignore file 1:

ignore_this_folder/

.gitignore file 2:

ignore_this_folder/*

I think both will ignore anything within the folder ignore_this_folder. Is there a "best practise"? is the * useless in this case? or is there some subtle difference?


Solution

  • Ignore_this_folder/ will ignore the directory without looking inside of it.

    Ignore_this_folder/* will ignore all of the files inside the directory and since git doesn't track empty folders, which means the the two patterns are the same. But there is a certain use case where you want to use one over the other.

    Suppose you want to to ignore all the files inside of the directory except one file(example.txt) the following pattern won't work

    Ignore_this_folder/
    !Ignore_this_folder/example.txt
    

    Instead you would use

    Ignore_this_folder/*
    !Ignore_this_folder/example.txt
    

    Answer inspired from the possible duplicate

    What's the difference between Git ignoring directory and directory/*?