Search code examples
gitgitignore

Git .gitignore Commands


I am reading the official GIT manual. However, I am finding that some things are not being ignored in my .gitignore file. For instance:

The GIT manual says, "You can end patterns with a forward slash (/) to specify a directory." You can start patterns with a forward slash (/) to avoid recursively

As it stands, I have a directory in the same directory where the .gitignore file is located. I also have a directory entitled 'test'. Test has several sub-dirs and the last directory has a text file. The full path is test/matt/end/matt.txt

I only have one command in the .gitignore file. For testing I am substituting to versions of it:

When I use the following:

/test - It ignores the entire path including the matt.txt file. When I try: test/ - It does the same exact thing.

The GIT manual also says the following, "only ignore the TODO file in the current directory, not subdir /TODO: /TODO"

Can someone please explain this to me please? Could the manual be wrong?


I created a new git repository on Linux. You will see everything in the screen shot.

In the .gitignore file I have tried it both ways /matt and matt/ Take a look at the screen shot here .gitignore. Based on the GIT manual, it shouldn't be working this way.


Solution

  • You're confused about what recursive means here. In this context, if you write

    /test
    

    It will not ignore any subdirectories named test. If you instead write

    test/
    

    all subfolders named test will be ignored as well.

    Example

    I created a repository with the following files:

    new file:   .gitattributes
    new file:   .gitignore
    new file:   aFolder/test/New Text Document.txt
    new file:   test/New Text Document.txt
    

    If add /test to my .gitignore file:

    new file:   .gitattributes
    new file:   .gitignore
    new file:   aFolder/test/New Text Document.txt
    

    Notice git doesn't recurse for the test directory. It only ignored top-level directories named test. If instead I add test/:

    new file:   .gitattributes
    new file:   .gitignore
    

    Now, it ignores any file that is contained within a test directory, regardless of the depth of that directory.