14

I want to commit and push two new files that are inside a folder that is listed in the project's .gitignore. I figure I can just change the .gitignore file, but then I would require two commits: one commit to push the two new files, and a second commit to reset the .gitignore. I'd like to do this in one commit if possible.

Is there any way to commit & push a specific file that is ignored?

Isaac
  • 2,246
  • 5
  • 21
  • 34

2 Answers2

42

You don't have to modify the .gitignore: you can force the addition of those files:

git add --force -- file1 file2
git commit -m "add previously ignored files"
git push

From git add man page:

-f
--force

Allow adding otherwise ignored files.

As Jakub Narębski comments, those files are no longer ignored, even if they would still be selected by the .gitignore directives.


Tom Hart asks in the comments:

Just wondering if there's anyway to re-ignore the files after using --force?

You need to record their deletion from the index, in order for those files to be ignored again in the working tree:

git rm --cached file1 file2
git commit -m "Remove files which should be ignored"
git push

The .gitignore rules will work as soon as the files are removed from the index (before the commit and push steps).
Pushing means other contributors will be impacted by the operation.

If you just want to ignore those files locally and temporarily, use:

git update-index --assume-unchanged -- file1 file2

(as I detailed earlier today in "GIT Ignore already committed files using exclude for local changes")

As Zeeker adds in the comments:

Atm git doesn't provide a mechanism to ignore all changes to a committed file across each clone.

So:

  • git rm --cache would remove the file (while keeping its previous history): file1 or file2 would no longer be visible, and would be ignore.
  • git update-index --assume-unchanged -- file1 file2 would not remove the files, but no longer detect local modifications.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Note that tracked files are no longer ignored. – Jakub Narębski Feb 16 '15 at 14:31
  • @JakubNarębski True. I have included your comment in the answer for more visibility. – VonC Feb 16 '15 at 14:34
  • 2
    Just wondering if there's anyway to re-ignore the files after using `--force`? – TMH Feb 16 '15 at 14:43
  • Thanks, whilst I don't personally need to do this at the moment I can think of some use cases where I'd need to just commit a file once and ignore it again afterwards. Thanks for the quick update :) – TMH Feb 16 '15 at 15:04
  • @TomHart note however that `update-index --assume-unchanged` is completly local. Atm git doesn't provide a mechanism to ignore all changes to a commited file accros each clone. – Sascha Wolf Feb 16 '15 at 15:10
  • @Zeeker True, hence the "locally and temporarily" expression I used. I have included your comment in the answer for more visibility. – VonC Feb 16 '15 at 15:18
  • Note to self: this is my 1000th "Good Answer". – VonC Jul 03 '20 at 21:36
  • How can we do this for directories? I'm get "fatal: unable to mark file ". And I tried doing the ./*/directory/** for all files – dko Jan 26 '22 at 02:04
  • I would rename the `.gitignore` temporarily, add the folder I want, restore the `.gitignore`. – VonC Jan 26 '22 at 07:18
3
git add -f /path/to/file

will force add file even if it is in ignored directory

pankijs
  • 6,531
  • 3
  • 16
  • 13