Is there a way to force git to add a file despite the .gitignore file?
8 Answers
See man git-add:
-f, --force
Allow adding otherwise ignored files.
So run this
git add --force my/ignore/file.foo
11 Comments
echo "/foo" >> .gitignore; echo "bar" > foo; git add foo # should throw an error; git add -p foo # works (cannot paste newlines in comment, execute the commands 1 by 1Despite 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
6 Comments
.gitignore use !specific-file-name.txt without the folder hierarchy, this way tracking will follow the file around the repo.How to unignore some select contents (files or folders) within an ignored folder
If you have ignored the contents of a directory like this:
/ignored_dir/* # ignore the **contents of** this dir!
then you can unignore a file or directory inside there like this:
!/ignored_dir/special_file_to_include # Do NOT ignore this file--DO include it!
!/ignored_dir/special_dir_to_include/ # And do NOT ignore this dir--DO include it!
BUT, if you have ignored the "ignored_dir" directly like this instead:
/ignored_dir/ # ignore this directory **directly**
then the above ! rules which attempt to unignore some files or folders will NOT work! So, in that case, switch from using this style: /ignored_dir/ to this style: /ignored_dir/*, and then add your ! rules as shown above to unignore something within that dir!
References:
- Unignore subdirectories of ignored directories in Git
- [my eRCaGuy_dotfiles repo with an example .gitignore file containing these notes and demos]: eRCaGuy_dotfiles/.gitignore
2 Comments
In cases where the ignored file is enforced by a git hook using git update-index --assume-unchanged <file>, a forced git add (git add -f <file>) won't work. In such cases you will need to manually update the index, as shown below.
git update-index --no-assume-unchanged path-to/ignored-file.ext
git add ...
git commit ...
git push ...
git update-index --assume-unchanged path-to/ignored-file.ext
Comments
Another way of achieving it would be to temporary edit the .gitignore file, add the file and then revert back the .gitignore. A bit hacky I feel
1 Comment
A better question may be - why would you want to do that? If you want a file tracked, unignore it (prefix its pattern in the .gitignore file with !, e.g. !dont/ignore/this/file). – Ohad Schneider
Sometimes we might change the API_KEY in a config file and need to ensure we don't accidentally upload it via git add *.
config.json:
{
"DEBUG": true,
"TESTCASE_DIR": "testcases/",
"OUTPUT_DIR": "output", <--- a new attribute, we want to modify it
and update to the git repository
"API_KEY": "********-****-****-****-************",
"API_URL": "https://api-ffm-stg.twcc.ai/api/models/conversation",
...
}
Solution (for example: config.json)
If git add --force config.json is not having the expected effect, please try the following steps to resolve the issue:
Ensure
.gitignorecorrectly ignoresconfig.json:Make sure your
.gitignorefile containsconfig.json:__pycache__/ *.log config.jsonRemove
config.jsonfrom version control (this step is crucial):If
config.jsonwas previously tracked, you need to remove it from Git's index and then re-add it:git rm --cached config.jsonCommit the changes (this step will remove the current version of
config.json):git commit -m "Remove config.json from version control"Force-add
config.json:Now you can modify/update the
config.jsonand force-add it:git add --force config.jsonCommit again:
Commit the force-added
config.json:git commit -m "Force add config.json despite .gitignore"Push to the remote repository:
Push the changes to GitHub:
git push origin master
These steps should solve your issue. If you still cannot force-add config.json, ensure your Git client has no configuration issues and that there are no Git hooks or settings interfering with the process.
Critical Next:
- DO NOT track the file
config.jsonagain:$ git status # try to modify `config.json` again, e.g. edit the API_KEY and save it # check the status again to see if it is be tracked $ git status # the critical action $ git update-index --assume-unchanged config.json # check the status again to see if it is be tracked $ git status
Comments
The first option is git add -f ...:
git add -f my/ignored/file.txt
# or
git add --force my/ignored/file.txt
See
git add --help(https://git-scm.com/docs/git-add)
Is git add -f doing nothing?
If git add -f is still not working, the following solution works for me:
# Move the file to a place were you can add it
mv my/ignored/file.txt git-add-file.txt
# Add the file with `git add`
git add git-add-file.txt
# Restore the original path of the file using GIT
git mv git-add-file.txt my/ignored/file.txt

.gitignorefile with!, e.g.!dont/ignore/this/file)..gitignore(say, add a.gitignorein the same folder as the dll, or one above, or something)?