Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 555
Att Righ Avatar asked Aug 09 '26 10:08

Att Righ


1 Answers

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
like image 133
VonC Avatar answered Aug 12 '26 09:08

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!