gitgitignore

Match end of file name in .gitignore


In a git repo, I want to exclude a file named main but not a file named main.c. How would I do this?

If I add this to the .gitignore file:

main

both files are excluded. How do I match a pattern at the end of a file name in a .gitignore?

I found out I could just add !main.* to the gitignore, but is there a cleaner way to achieve the same result, perhaps with something like main^?


Solution

  • I get

    $ cd `mktemp -d`; git init
    […]
    $ echo main >.gitignore
    $ >main; >main.c
    $ git add -n .
    add '.gitignore'
    add 'main.c'
    $
    

    so I don't know what else you're doing to get

    both files are excluded

    but whatever's happening I think I did exactly as you describe and didn't get those results.

    Maybe main is a directory? Directory exclusions summarily exclude the entire contents regardless of name, that's the point: to tell git not to even look there for new files to track. If that's the case, you can say !main/ to stop directories named main being dropped whole like that.

    Anyway, try doing git ls-files -oc | git check-ignore --stdin -v to see exactly why all excluded files are excluded, and add the -n flag to the checkignore to see all the files that aren't ignored too, showing any explicit no-exclude rules that stopped earlier ones matching.