Is there a way to force git
to add a file despite the .gitignore
file?
6 Answers
See man git-add
:
-f, --force
Allow adding otherwise ignored files.
So run this
git add --force my/ignore/file.foo
-
31
-
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 Nov 5, 2011 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– JakoAug 28, 2015 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 <filename>" does nothing, but "git add -f *" works. Using TortoiseGit also works. 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.– ohwOct 18, 2015 at 22:47
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
-
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.– MerlinOct 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-SOct 27, 2017 at 22:03
-
51
-
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-SOct 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
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
-
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
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
-
15
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
If you are using eclipse/STS, then you can do this:
- Right click on the file you want to unignore.
- Click on 'Team' post that.
- Select 'Advanced'.
- And finally click on 'No Assume Unchanged'
- Now you can see that your file is unignored.
.gitignore
file with!
, e.g.!dont/ignore/this/file
)..gitignore
(say, add a.gitignore
in the same folder as the dll, or one above, or something)?