Is there a way to force git
to add a file despite the .gitignore
file?
The git add command can be used to add ignored files with the -f (force) option.
The . gitignore file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo. / will ignore directories with the name.
The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area. We also say that they will be staged.
See man git-add
:
-f, --force Allow adding otherwise ignored files.
So run this
git add --force my/ignore/file.foo
Despite Daniel Böhmer's working solution, Ohad Schneider offered a better solution in a comment:
If the file is usually ignored, and you force adding it - it can be accidentally ignored again in the future (like when the file is deleted, then a commit is made and the file is re-created.
You should just un-ignore it in the .gitignore file like this: Unignore subdirectories of ignored directories in Git
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With