Git - Commit file in .gitignore

后端 未结 2 1628
一向
一向 2021-02-01 09:45

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 re

相关标签:
2条回答
  • 2021-02-01 10:30

    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.
    0 讨论(0)
  • 2021-02-01 10:36
    git add -f /path/to/file
    

    will force add file even if it is in ignored directory

    0 讨论(0)
提交回复
热议问题