尽管有 .gitignore 文件,仍强制添加
有没有办法强制 git 添加文件,尽管有 .gitignore 文件?
Is there a way to force git
to add a file despite the .gitignore
file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有办法强制 git 添加文件,尽管有 .gitignore 文件?
Is there a way to force git
to add a file despite the .gitignore
file?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
请参阅
man git-add
:所以运行这个
See
man git-add
:So run this
尽管 Daniel Böhmer 提出了可行的解决方案,但 Ohad Schneider 在评论中提供了更好的解决方案:
如果文件通常被忽略,并且您强制添加它 - 将来它可能会再次被意外忽略(例如当文件被删除时,则提交是创建并重新创建文件,
您应该在 .gitignore 文件中取消忽略它,如下所示:
忽略 Git 中被忽略目录的子目录
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
如何不忽略忽略文件夹中的某些选定内容(文件或文件夹)
如果您忽略了如下目录的内容:
那么您可以un忽略其中的文件或目录,如下所示:
但是,如果您直接忽略了“ignored_dir”:
则上面的
!< /code> 尝试的规则不忽略某些文件或文件夹将不起作用!因此,在这种情况下,请从使用此样式:
/ignored_dir/
切换到此样式:/ignored_dir/*
,然后添加您的!
如上所示的规则un忽略该目录中的某些内容!参考文献:
How to unignore some select contents (files or folders) within an ignored folder
If you have ignored the contents of a directory like this:
then you can unignore a file or directory inside there like this:
BUT, if you have ignored the "ignored_dir" directly like this instead:
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:
实现它的另一种方法是临时编辑 gitignore 文件,添加该文件,然后恢复回 gitignore。我觉得有点老套
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
如果忽略的文件是由 git hook 使用 git update-index --assume-unchanged强制执行的,则强制 git add (
git add -f;
) 不起作用。在这种情况下,您将需要手动更新索引,如下所示。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.如果您使用的是 eclipse/STS,那么您可以执行以下操作:
If you are using eclipse/STS, then you can do this:
有时我们可能会更改配置文件中的 API_KEY,并且需要确保我们不会意外地通过 git add * 上传它。
config.json
:解决方案(例如:
config.json
)如果
git add --force config.json
未达到预期效果,请尝试以下步骤解决问题:确保
.gitignore
正确忽略config.json
:确保您的
.gitignore
文件包含config.json
:<前><代码>__pycache__/
*。日志
配置.json
从版本控制中删除
config.json
(此步骤至关重要):如果
config.json
之前被跟踪,您需要将其从 Git 索引中删除,然后重新添加:提交更改(此步骤将删除
config.json
的当前版本):强制添加
config.json
:现在您可以修改/更新
config.json
并强制添加它:再次提交:
提交强制添加的
config.json
:推送到远程仓库:
将更改推送到 GitHub:
这些步骤应该可以解决您的问题。如果您仍然无法强制添加 config.json,请确保您的 Git 客户端没有配置问题,并且没有 Git 挂钩或设置干扰该过程。
关键下一步:
config.json
:Sometimes we might change the API_KEY in a config file and need to ensure we don't accidentally upload it via
git add *
.config.json
:Solution (for example:
config.json
)If
git add --force config.json
is not having the expected effect, please try the following steps to resolve the issue:Ensure
.gitignore
correctly ignoresconfig.json
:Make sure your
.gitignore
file containsconfig.json
:Remove
config.json
from version control (this step is crucial):If
config.json
was previously tracked, you need to remove it from Git's index and then re-add it:Commit the changes (this step will remove the current version of
config.json
):Force-add
config.json
:Now you can modify/update the
config.json
and force-add it:Commit again:
Commit the force-added
config.json
:Push to the remote repository:
Push the changes to GitHub:
These steps should solve your issue. If you still cannot force-add config.json, ensure your Git client has no configuration issues and that there are no Git hooks or settings interfering with the process.
Critical Next:
config.json
again: