Is there a way to force git
to add a file despite the .gitignore
file?

- 14,463
- 5
- 36
- 46

- 67,098
- 47
- 117
- 162
-
10A 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 Sep 07 '15 at 12:57
-
7@OhadSchneider This is useful if you force commit build file(s) to another branch. A few projects do this such as StrongLoop for safe deployments – Deminetix Dec 21 '15 at 02:15
-
7@OhadSchneider For us, it's more because most developers install git with Visual Studio 2015, which adds a global gitignore to the user folder. This excludes files like *.dll and *.exe. That is great for our new projects which only use nuget packages that are retrieved during build, but for a small number of older projects, we still use dll's that are manually referenced in the solution. For those projects, we would like to manually include them, instead of telling every developer to comment the *.dll and *.exe files in the global gitignore. – Nullius Feb 17 '16 at 09:11
-
7@Nullius Can't you just unignore them in a higher level `.gitignore` (say, add a `.gitignore` in the same folder as the dll, or one above, or something)? – Ohad Schneider Feb 17 '16 at 13:41
5 Answers
See man git-add
:
-f, --force
Allow adding otherwise ignored files.
So run this
git add --force my/ignore/file.foo

- 14,463
- 5
- 36
- 46
-
29
-
7Well, I tested it to make sure it really works and it does. Can you describe your environemnt (OS, git version ...)? This is what I've basically done: `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 1 – Daniel Böhmer Nov 05 '11 at 10:26
-
5Same happened to me, I had previously ran git update-index --asume-unchanged on my files. I had to undo it with --no-assume-unchanged and it worked – Jako Aug 28 '15 at 20:47
-
4This only works for me using wildcards, using git 1.9.5 on Windows, even after trying --no--assume-unchanged. "git add - f
" does nothing, but "git add -f *" works. Using TortoiseGit also works. – mhenry1384 Oct 12 '15 at 21:04 -
1@mhenry1384 The reason that it works for wildcard only in Windows may be due to how the path separator is used. For me slash "/" works but backslash "\" doesn't. The fact that cmd/PowerShell convert "/" to "\" for us makes it easy to run into this. – ohw Oct 18 '15 at 22:47
-
1Hi, I just wanted to share that although `git status` didn't show the `--force` added file, I was able to `git commit` my latest changes of the previously ignored file. I am on CentOS 7 git version 2.25.0. Thanks! – tash Nov 25 '20 at 03:38
-
This worked for me on WIndows 11 using git version 2.39.1.windows.1 and PowerShell 7.3.2 – somethingRandom Feb 14 '23 at 07:36
-
I couldn't get this to work on Windows until I used the proper casing for the file name. – Polshgiant Apr 14 '23 at 03:24
-
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

- 36,492
- 15
- 194
- 265

- 2,547
- 2
- 27
- 37
-
5This is correct. I was using the --force option for small data files, and then rearranged a bit of folder structure and clobbered all the force-tracked files. HOWEVER: in `.gitignore` use `!specific-file-name.txt` without the folder hierarchy, this way tracking will follow the file around the repo. – Merlin Oct 27 '17 at 07:13
-
It depends on the scenario, e.g. sometime you want to exclude "1.txt" in all folders, and sometime "folder1/*.txt" – A-S Oct 27 '17 at 22:03
-
46
-
1@aij - I disagree, my answer offers another "way to force git to add a file despite the (current state of) .gitignore file". Although the extra text in the parentheses was added by me - I think it is also implied from the text of the question. Anyway, I think that this is also important information to whoever that considers using the "--force" option – A-S Oct 29 '20 at 15:01
-
This is not a better solution becasue it would unignore the file that should be ignored. I use this gitignore method to push deploy files with the values marked as "YOUR_ID_HERE" and such so I do not want people to push their personal info but if there is a change to the deploy i still need to update the file upstream – bia.migueis Aug 31 '22 at 16:53
-
Finding a use-case that my solution doesn't fit - doesn't prevent it from being a better solution for other use-cases. BTW, IMO, having a file that its changes should sometimes be committed and sometimes ignored - is not a good practice. There are other ways to solve the use-case that you've listed. – A-S Sep 01 '22 at 09:03
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

- 36,492
- 15
- 194
- 265
-
This might've been better as an edit (albeit a rather extensive one) on the A-S answer above, as it is the same answer and that one has 24 votes so is always going to appear higher. (Your version of it is much more helpful though.) – Darren Cook Jun 02 '21 at 08:36
-
1@DarrenCook, I didn't want to do an extensive edit to someone else's answer. One of the key appeals of Stack Overflow is personal ownership of answers and content, and large edits disturb that. I think my answer is much more helpful too, and I'm hoping to keep ownership over it myself to make it just how I like, and to get some credit along the way. Eventually, if people like you upvote it, it will float towards the top. I've seen it dozens of times. I added it because it provides new and lacking content and insight which wasn't already present on this page. – Gabriel Staples Jun 02 '21 at 09:02
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

- 357
- 2
- 6
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

- 13,750
- 36
- 127
- 202