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?
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.git add -f /path/to/file
will force add file even if it is in ignored directory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With