599

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

4
  • 10
    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). Sep 7, 2015 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, 2015 at 2:15
  • 8
    @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, 2016 at 9: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)? Feb 17, 2016 at 13:41

6 Answers 6

802

See man git-add:

   -f, --force
       Allow adding otherwise ignored files.

So run this

git add --force my/ignore/file.foo
9
  • 31
    Uhm that doesn't work, on git status the file is still not shown
    – Mark
    Nov 5, 2011 at 5:36
  • 7
    Well, 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 Nov 5, 2011 at 10:26
  • 5
    Same 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, 2015 at 20:47
  • 4
    This only works for me using wildcards, using git 1.9.5 on Windows, even after trying --no--assume-unchanged. "git add - f <filename>" does nothing, but "git add -f *" works. Using TortoiseGit also works.
    – mhenry1384
    Oct 12, 2015 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, 2015 at 22:47
32

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

6
  • 5
    This 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, 2017 at 7: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, 2017 at 22:03
  • 51
    That's not a "better" solution. It's a solution to a different problem.
    – aij
    Mar 23, 2018 at 13: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, 2020 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 Aug 31, 2022 at 16:53
19

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:

  1. Unignore subdirectories of ignored directories in Git
  2. [my eRCaGuy_dotfiles repo with an example .gitignore file containing these notes and demos]: eRCaGuy_dotfiles/.gitignore
2
  • 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.) Jun 2, 2021 at 8: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. Jun 2, 2021 at 9:02
6

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
  • 15
    I agree, this is not what you want to do.
    – Merlin
    Oct 27, 2017 at 7:13
2

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
0

If you are using eclipse/STS, then you can do this:

  1. Right click on the file you want to unignore.
  2. Click on 'Team' post that.
  3. Select 'Advanced'.
  4. And finally click on 'No Assume Unchanged'
  5. Now you can see that your file is unignored.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.