bashgitcommand-line-interfacegitignore

CLI command to add files to gitignore


I'm working in a highly nested project and want to add some files to gitignore from the shell, but it's a bit of a pain working relative paths.

I can do a one liner like this (wrapped for legibility):

echo file |
xargs -n1 greadlink -f |
xargs realpath --relative-to=$(git rev-parse --show-toplevel) \
  > $(git rev-parse --show-toplevel)/.gitignore

but if feels like there should be something a better like git addtoignore file. Is there a simple, reliable command I can use to add a file to the gitignore without dealing with relative path names.


Solution

  • Beside using multiple .gitignore, allowing you to add file directly, the other approach is to use find, assuming your file name is unique enough.

    cd /path/to/repo
    find . -name "file" >> .gitignore