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









up vote
107
down vote

favorite
18












I'm confused about what's the correct way to ignore the contents of a directory in git.



Assume I have the following directory structure:



my_project 
|--www
|--1.txt
|--2.txt
|--.gitignore


What's the difference between putting this:



www


And this?



www/*


The reason I'm asking this question is: In git, if a directory is empty, git won't include such empty directory in repository. So I was trying the solution that is add an extra .gitkeep file under the directory so that it won't be empty. When I was trying that solution, if in the .gitignore file, I write like below:



www
!*.gitkeep


It doesn't work(My intention is to ignore all contents under www but keep the directory). But if I try the following:



www/* 
!*.gitkeep


Then it works! So I think it must has some differences between the two approaches.










share|improve this question



















  • 3




    Why not integrate your comment into your question? It would make the latter even more interesting :)
    – jubobs
    Sep 8 '14 at 7:47






  • 1




    Thanks Jubobs for your suggestion. I've integrated my comment into my question.
    – Aaron Shen
    Sep 8 '14 at 12:14










  • A simple difference between bin and bin/ is that the former will ignore files or folders, the latter only folders. I don't know the difference with bin/*
    – Colonel Panic
    Sep 13 '14 at 13:28














up vote
107
down vote

favorite
18












I'm confused about what's the correct way to ignore the contents of a directory in git.



Assume I have the following directory structure:



my_project 
|--www
|--1.txt
|--2.txt
|--.gitignore


What's the difference between putting this:



www


And this?



www/*


The reason I'm asking this question is: In git, if a directory is empty, git won't include such empty directory in repository. So I was trying the solution that is add an extra .gitkeep file under the directory so that it won't be empty. When I was trying that solution, if in the .gitignore file, I write like below:



www
!*.gitkeep


It doesn't work(My intention is to ignore all contents under www but keep the directory). But if I try the following:



www/* 
!*.gitkeep


Then it works! So I think it must has some differences between the two approaches.










share|improve this question



















  • 3




    Why not integrate your comment into your question? It would make the latter even more interesting :)
    – jubobs
    Sep 8 '14 at 7:47






  • 1




    Thanks Jubobs for your suggestion. I've integrated my comment into my question.
    – Aaron Shen
    Sep 8 '14 at 12:14










  • A simple difference between bin and bin/ is that the former will ignore files or folders, the latter only folders. I don't know the difference with bin/*
    – Colonel Panic
    Sep 13 '14 at 13:28












up vote
107
down vote

favorite
18









up vote
107
down vote

favorite
18






18





I'm confused about what's the correct way to ignore the contents of a directory in git.



Assume I have the following directory structure:



my_project 
|--www
|--1.txt
|--2.txt
|--.gitignore


What's the difference between putting this:



www


And this?



www/*


The reason I'm asking this question is: In git, if a directory is empty, git won't include such empty directory in repository. So I was trying the solution that is add an extra .gitkeep file under the directory so that it won't be empty. When I was trying that solution, if in the .gitignore file, I write like below:



www
!*.gitkeep


It doesn't work(My intention is to ignore all contents under www but keep the directory). But if I try the following:



www/* 
!*.gitkeep


Then it works! So I think it must has some differences between the two approaches.










share|improve this question















I'm confused about what's the correct way to ignore the contents of a directory in git.



Assume I have the following directory structure:



my_project 
|--www
|--1.txt
|--2.txt
|--.gitignore


What's the difference between putting this:



www


And this?



www/*


The reason I'm asking this question is: In git, if a directory is empty, git won't include such empty directory in repository. So I was trying the solution that is add an extra .gitkeep file under the directory so that it won't be empty. When I was trying that solution, if in the .gitignore file, I write like below:



www
!*.gitkeep


It doesn't work(My intention is to ignore all contents under www but keep the directory). But if I try the following:



www/* 
!*.gitkeep


Then it works! So I think it must has some differences between the two approaches.







git gitignore notation dotfiles






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 25 '14 at 18:45









igaurav

2,02912035




2,02912035










asked Sep 8 '14 at 0:03









Aaron Shen

3,18562962




3,18562962







  • 3




    Why not integrate your comment into your question? It would make the latter even more interesting :)
    – jubobs
    Sep 8 '14 at 7:47






  • 1




    Thanks Jubobs for your suggestion. I've integrated my comment into my question.
    – Aaron Shen
    Sep 8 '14 at 12:14










  • A simple difference between bin and bin/ is that the former will ignore files or folders, the latter only folders. I don't know the difference with bin/*
    – Colonel Panic
    Sep 13 '14 at 13:28












  • 3




    Why not integrate your comment into your question? It would make the latter even more interesting :)
    – jubobs
    Sep 8 '14 at 7:47






  • 1




    Thanks Jubobs for your suggestion. I've integrated my comment into my question.
    – Aaron Shen
    Sep 8 '14 at 12:14










  • A simple difference between bin and bin/ is that the former will ignore files or folders, the latter only folders. I don't know the difference with bin/*
    – Colonel Panic
    Sep 13 '14 at 13:28







3




3




Why not integrate your comment into your question? It would make the latter even more interesting :)
– jubobs
Sep 8 '14 at 7:47




Why not integrate your comment into your question? It would make the latter even more interesting :)
– jubobs
Sep 8 '14 at 7:47




1




1




Thanks Jubobs for your suggestion. I've integrated my comment into my question.
– Aaron Shen
Sep 8 '14 at 12:14




Thanks Jubobs for your suggestion. I've integrated my comment into my question.
– Aaron Shen
Sep 8 '14 at 12:14












A simple difference between bin and bin/ is that the former will ignore files or folders, the latter only folders. I don't know the difference with bin/*
– Colonel Panic
Sep 13 '14 at 13:28




A simple difference between bin and bin/ is that the former will ignore files or folders, the latter only folders. I don't know the difference with bin/*
– Colonel Panic
Sep 13 '14 at 13:28












4 Answers
4






active

oldest

votes

















up vote
196
down vote



accepted










There're differences among www, www/ and www/*.



Basically from the documentation and my own tests, www find a match with a file or a directory, www/ only matches a directory, while www/* matches directories and files inside www.



I'll only discuss on the differences between www/ and www/* here, since the differences between www and www/ are obvious.



For www/, git ignores the directory www itself, which means git won't even look inside. But for www/*, git checks all files/folders inside www, and ignores all of them with the pattern *. It seems to lead to the same results since git won't track an empty folder www if all its child files/folders are ignored. And indeed the results will be no difference for OP's case with www/ or www/* standalone. But it does make differences if it's combined with other rules.



For example, what if we want to only include www/1.txt but ignore all others inside www?



The following .gitignore won't work.



www/
!www/1.txt


While the following .gitignore works, why?



www/*
!www/1.txt


For the former, git just ignores the directory www, and won't even look inside to include www/1.txt again. The first rule excludes the parent directory www but not www/1.txt, and as a result www/1.txt cannot be "included again".



But for the latter, git first ignores all files/folers under www, and then includes one of them again which is www/1.txt.



For this example, the follwing lines in the documentation may help:




An optional prefix "!" which negates the pattern; any matching file
excluded by a previous pattern will become included again. It is not
possible to re-include a file if a parent directory of that file is
excluded.







share|improve this answer


















  • 2




    +1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
    – Benjamin Gruenbaum
    Sep 9 '14 at 12:12










  • Don't you think that www/1.txt and then www/ would do the same as the second approach...
    – Naveed Butt
    Sep 10 '14 at 4:58


















up vote
8
down vote













I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.



$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar


I did test the above, and if you replace !/foo with !/foo/*, you do indeed get a different result.



Note



foo


Will exclude any file foo, but



foo/


will only exclude directories named foo.






share|improve this answer





























    up vote
    3
    down vote













    Apart from the perfectly good answers you have already obtained, you should note that you can have .gitignore anywhere in your project, including subfolders.



    So if you want to ignore all files inside www, but whant the www folder to be versioned, instead of using an empty .gitkeep, .dummy or whatever name you choose, why not use a .gitignore there, telling to ignore all files?



    /
    |- .gitignore (a)
    - www
    |- .gitignore (b)
    |- 1.jpg
    - 2.jpg


    In the root .gitignore (a), you don't say anything about the www folder or its contents.



    In the www/.gitignore (b) you put the following:



    # ignore all files in this folder except this .gitignore
    *
    !.gitignore


    This way everything looks more organized (to me at least).






    share|improve this answer



























      up vote
      1
      down vote













      To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore:



      www/[^.]*


      So no need for an extra .gitignore, just simply add a .keep file to your www directory.






      share|improve this answer





















        protected by Ionică Bizău Sep 14 '14 at 14:34



        Thank you for your interest in this question.
        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



        Would you like to answer one of these unanswered questions instead?














        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        196
        down vote



        accepted










        There're differences among www, www/ and www/*.



        Basically from the documentation and my own tests, www find a match with a file or a directory, www/ only matches a directory, while www/* matches directories and files inside www.



        I'll only discuss on the differences between www/ and www/* here, since the differences between www and www/ are obvious.



        For www/, git ignores the directory www itself, which means git won't even look inside. But for www/*, git checks all files/folders inside www, and ignores all of them with the pattern *. It seems to lead to the same results since git won't track an empty folder www if all its child files/folders are ignored. And indeed the results will be no difference for OP's case with www/ or www/* standalone. But it does make differences if it's combined with other rules.



        For example, what if we want to only include www/1.txt but ignore all others inside www?



        The following .gitignore won't work.



        www/
        !www/1.txt


        While the following .gitignore works, why?



        www/*
        !www/1.txt


        For the former, git just ignores the directory www, and won't even look inside to include www/1.txt again. The first rule excludes the parent directory www but not www/1.txt, and as a result www/1.txt cannot be "included again".



        But for the latter, git first ignores all files/folers under www, and then includes one of them again which is www/1.txt.



        For this example, the follwing lines in the documentation may help:




        An optional prefix "!" which negates the pattern; any matching file
        excluded by a previous pattern will become included again. It is not
        possible to re-include a file if a parent directory of that file is
        excluded.







        share|improve this answer


















        • 2




          +1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
          – Benjamin Gruenbaum
          Sep 9 '14 at 12:12










        • Don't you think that www/1.txt and then www/ would do the same as the second approach...
          – Naveed Butt
          Sep 10 '14 at 4:58















        up vote
        196
        down vote



        accepted










        There're differences among www, www/ and www/*.



        Basically from the documentation and my own tests, www find a match with a file or a directory, www/ only matches a directory, while www/* matches directories and files inside www.



        I'll only discuss on the differences between www/ and www/* here, since the differences between www and www/ are obvious.



        For www/, git ignores the directory www itself, which means git won't even look inside. But for www/*, git checks all files/folders inside www, and ignores all of them with the pattern *. It seems to lead to the same results since git won't track an empty folder www if all its child files/folders are ignored. And indeed the results will be no difference for OP's case with www/ or www/* standalone. But it does make differences if it's combined with other rules.



        For example, what if we want to only include www/1.txt but ignore all others inside www?



        The following .gitignore won't work.



        www/
        !www/1.txt


        While the following .gitignore works, why?



        www/*
        !www/1.txt


        For the former, git just ignores the directory www, and won't even look inside to include www/1.txt again. The first rule excludes the parent directory www but not www/1.txt, and as a result www/1.txt cannot be "included again".



        But for the latter, git first ignores all files/folers under www, and then includes one of them again which is www/1.txt.



        For this example, the follwing lines in the documentation may help:




        An optional prefix "!" which negates the pattern; any matching file
        excluded by a previous pattern will become included again. It is not
        possible to re-include a file if a parent directory of that file is
        excluded.







        share|improve this answer


















        • 2




          +1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
          – Benjamin Gruenbaum
          Sep 9 '14 at 12:12










        • Don't you think that www/1.txt and then www/ would do the same as the second approach...
          – Naveed Butt
          Sep 10 '14 at 4:58













        up vote
        196
        down vote



        accepted







        up vote
        196
        down vote



        accepted






        There're differences among www, www/ and www/*.



        Basically from the documentation and my own tests, www find a match with a file or a directory, www/ only matches a directory, while www/* matches directories and files inside www.



        I'll only discuss on the differences between www/ and www/* here, since the differences between www and www/ are obvious.



        For www/, git ignores the directory www itself, which means git won't even look inside. But for www/*, git checks all files/folders inside www, and ignores all of them with the pattern *. It seems to lead to the same results since git won't track an empty folder www if all its child files/folders are ignored. And indeed the results will be no difference for OP's case with www/ or www/* standalone. But it does make differences if it's combined with other rules.



        For example, what if we want to only include www/1.txt but ignore all others inside www?



        The following .gitignore won't work.



        www/
        !www/1.txt


        While the following .gitignore works, why?



        www/*
        !www/1.txt


        For the former, git just ignores the directory www, and won't even look inside to include www/1.txt again. The first rule excludes the parent directory www but not www/1.txt, and as a result www/1.txt cannot be "included again".



        But for the latter, git first ignores all files/folers under www, and then includes one of them again which is www/1.txt.



        For this example, the follwing lines in the documentation may help:




        An optional prefix "!" which negates the pattern; any matching file
        excluded by a previous pattern will become included again. It is not
        possible to re-include a file if a parent directory of that file is
        excluded.







        share|improve this answer














        There're differences among www, www/ and www/*.



        Basically from the documentation and my own tests, www find a match with a file or a directory, www/ only matches a directory, while www/* matches directories and files inside www.



        I'll only discuss on the differences between www/ and www/* here, since the differences between www and www/ are obvious.



        For www/, git ignores the directory www itself, which means git won't even look inside. But for www/*, git checks all files/folders inside www, and ignores all of them with the pattern *. It seems to lead to the same results since git won't track an empty folder www if all its child files/folders are ignored. And indeed the results will be no difference for OP's case with www/ or www/* standalone. But it does make differences if it's combined with other rules.



        For example, what if we want to only include www/1.txt but ignore all others inside www?



        The following .gitignore won't work.



        www/
        !www/1.txt


        While the following .gitignore works, why?



        www/*
        !www/1.txt


        For the former, git just ignores the directory www, and won't even look inside to include www/1.txt again. The first rule excludes the parent directory www but not www/1.txt, and as a result www/1.txt cannot be "included again".



        But for the latter, git first ignores all files/folers under www, and then includes one of them again which is www/1.txt.



        For this example, the follwing lines in the documentation may help:




        An optional prefix "!" which negates the pattern; any matching file
        excluded by a previous pattern will become included again. It is not
        possible to re-include a file if a parent directory of that file is
        excluded.








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 8 '14 at 2:58

























        answered Sep 8 '14 at 2:45









        Landys

        4,36921529




        4,36921529







        • 2




          +1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
          – Benjamin Gruenbaum
          Sep 9 '14 at 12:12










        • Don't you think that www/1.txt and then www/ would do the same as the second approach...
          – Naveed Butt
          Sep 10 '14 at 4:58













        • 2




          +1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
          – Benjamin Gruenbaum
          Sep 9 '14 at 12:12










        • Don't you think that www/1.txt and then www/ would do the same as the second approach...
          – Naveed Butt
          Sep 10 '14 at 4:58








        2




        2




        +1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
        – Benjamin Gruenbaum
        Sep 9 '14 at 12:12




        +1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
        – Benjamin Gruenbaum
        Sep 9 '14 at 12:12












        Don't you think that www/1.txt and then www/ would do the same as the second approach...
        – Naveed Butt
        Sep 10 '14 at 4:58





        Don't you think that www/1.txt and then www/ would do the same as the second approach...
        – Naveed Butt
        Sep 10 '14 at 4:58













        up vote
        8
        down vote













        I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.



        $ cat .gitignore
        # exclude everything except directory foo/bar
        /*
        !/foo
        /foo/*
        !/foo/bar


        I did test the above, and if you replace !/foo with !/foo/*, you do indeed get a different result.



        Note



        foo


        Will exclude any file foo, but



        foo/


        will only exclude directories named foo.






        share|improve this answer


























          up vote
          8
          down vote













          I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.



          $ cat .gitignore
          # exclude everything except directory foo/bar
          /*
          !/foo
          /foo/*
          !/foo/bar


          I did test the above, and if you replace !/foo with !/foo/*, you do indeed get a different result.



          Note



          foo


          Will exclude any file foo, but



          foo/


          will only exclude directories named foo.






          share|improve this answer
























            up vote
            8
            down vote










            up vote
            8
            down vote









            I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.



            $ cat .gitignore
            # exclude everything except directory foo/bar
            /*
            !/foo
            /foo/*
            !/foo/bar


            I did test the above, and if you replace !/foo with !/foo/*, you do indeed get a different result.



            Note



            foo


            Will exclude any file foo, but



            foo/


            will only exclude directories named foo.






            share|improve this answer














            I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.



            $ cat .gitignore
            # exclude everything except directory foo/bar
            /*
            !/foo
            /foo/*
            !/foo/bar


            I did test the above, and if you replace !/foo with !/foo/*, you do indeed get a different result.



            Note



            foo


            Will exclude any file foo, but



            foo/


            will only exclude directories named foo.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 8 '14 at 1:19

























            answered Sep 8 '14 at 1:08









            djechlin

            41k20112223




            41k20112223




















                up vote
                3
                down vote













                Apart from the perfectly good answers you have already obtained, you should note that you can have .gitignore anywhere in your project, including subfolders.



                So if you want to ignore all files inside www, but whant the www folder to be versioned, instead of using an empty .gitkeep, .dummy or whatever name you choose, why not use a .gitignore there, telling to ignore all files?



                /
                |- .gitignore (a)
                - www
                |- .gitignore (b)
                |- 1.jpg
                - 2.jpg


                In the root .gitignore (a), you don't say anything about the www folder or its contents.



                In the www/.gitignore (b) you put the following:



                # ignore all files in this folder except this .gitignore
                *
                !.gitignore


                This way everything looks more organized (to me at least).






                share|improve this answer
























                  up vote
                  3
                  down vote













                  Apart from the perfectly good answers you have already obtained, you should note that you can have .gitignore anywhere in your project, including subfolders.



                  So if you want to ignore all files inside www, but whant the www folder to be versioned, instead of using an empty .gitkeep, .dummy or whatever name you choose, why not use a .gitignore there, telling to ignore all files?



                  /
                  |- .gitignore (a)
                  - www
                  |- .gitignore (b)
                  |- 1.jpg
                  - 2.jpg


                  In the root .gitignore (a), you don't say anything about the www folder or its contents.



                  In the www/.gitignore (b) you put the following:



                  # ignore all files in this folder except this .gitignore
                  *
                  !.gitignore


                  This way everything looks more organized (to me at least).






                  share|improve this answer






















                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    Apart from the perfectly good answers you have already obtained, you should note that you can have .gitignore anywhere in your project, including subfolders.



                    So if you want to ignore all files inside www, but whant the www folder to be versioned, instead of using an empty .gitkeep, .dummy or whatever name you choose, why not use a .gitignore there, telling to ignore all files?



                    /
                    |- .gitignore (a)
                    - www
                    |- .gitignore (b)
                    |- 1.jpg
                    - 2.jpg


                    In the root .gitignore (a), you don't say anything about the www folder or its contents.



                    In the www/.gitignore (b) you put the following:



                    # ignore all files in this folder except this .gitignore
                    *
                    !.gitignore


                    This way everything looks more organized (to me at least).






                    share|improve this answer












                    Apart from the perfectly good answers you have already obtained, you should note that you can have .gitignore anywhere in your project, including subfolders.



                    So if you want to ignore all files inside www, but whant the www folder to be versioned, instead of using an empty .gitkeep, .dummy or whatever name you choose, why not use a .gitignore there, telling to ignore all files?



                    /
                    |- .gitignore (a)
                    - www
                    |- .gitignore (b)
                    |- 1.jpg
                    - 2.jpg


                    In the root .gitignore (a), you don't say anything about the www folder or its contents.



                    In the www/.gitignore (b) you put the following:



                    # ignore all files in this folder except this .gitignore
                    *
                    !.gitignore


                    This way everything looks more organized (to me at least).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 9 '14 at 9:08









                    Carlos Campderrós

                    13.9k64253




                    13.9k64253




















                        up vote
                        1
                        down vote













                        To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore:



                        www/[^.]*


                        So no need for an extra .gitignore, just simply add a .keep file to your www directory.






                        share|improve this answer


























                          up vote
                          1
                          down vote













                          To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore:



                          www/[^.]*


                          So no need for an extra .gitignore, just simply add a .keep file to your www directory.






                          share|improve this answer
























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore:



                            www/[^.]*


                            So no need for an extra .gitignore, just simply add a .keep file to your www directory.






                            share|improve this answer














                            To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore:



                            www/[^.]*


                            So no need for an extra .gitignore, just simply add a .keep file to your www directory.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Sep 9 '14 at 22:38

























                            answered Sep 9 '14 at 22:25









                            Koen.

                            14.9k56069




                            14.9k56069















                                protected by Ionică Bizău Sep 14 '14 at 14:34



                                Thank you for your interest in this question.
                                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                Would you like to answer one of these unanswered questions instead?



                                Popular posts from this blog

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                政党

                                天津地下鉄3号線