尽管有 .gitignore 文件,仍强制添加

发布于 2024-12-13 09:41:11 字数 42 浏览 3 评论 0原文

有没有办法强制 git 添加文件,尽管有 .gitignore 文件?

Is there a way to force git to add a file despite the .gitignore file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

玩物 2024-12-20 09:41:11

请参阅 man git-add

   -f, --force
       Allow adding otherwise ignored files.

所以运行这个

git add --force my/ignore/file.foo

See man git-add:

   -f, --force
       Allow adding otherwise ignored files.

So run this

git add --force my/ignore/file.foo
顾忌 2024-12-20 09:41:11

尽管 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

别靠近我心 2024-12-20 09:41:11

如何忽略忽略文件夹中的某些选定内容(文件或文件夹)

如果您忽略了如下目录的内容

/ignored_dir/*    # ignore the **contents of** this dir!

那么您可以un忽略其中的文件或目录,如下所示:

!/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!

但是,如果您直接忽略了“ignored_dir”:

/ignored_dir/     # ignore this directory **directly**

则上面的!< /code> 尝试的规则忽略某些文件或文件夹将不起作用!因此,在这种情况下,请从使用此样式:/ignored_dir/ 切换到此样式:/ignored_dir/*,然后添加您的 !如上所示的规则un忽略该目录中的某些内容!

参考文献:

  1. 在 Git 中忽略被忽略目录的子目录
  2. [我的 eRCaGuy_dotfiles 存储库,其中包含包含这些注释和演示的示例 .gitignore 文件]:eRCAGuy_dotfiles/.gitignore

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:

  1. Unignore subdirectories of ignored directories in Git
  2. [my eRCaGuy_dotfiles repo with an example .gitignore file containing these notes and demos]: eRCaGuy_dotfiles/.gitignore
挽你眉间 2024-12-20 09:41:11

实现它的另一种方法是临时编辑 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

像你 2024-12-20 09:41:11

如果忽略的文件是由 git hook 使用 git update-index --assume-unchanged强制执行的,则强制 git add (git add -f;) 不起作用。在这种情况下,您将需要手动更新索引,如下所示。

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

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
尤怨 2024-12-20 09:41:11

如果您使用的是 eclipse/STS,那么您可以执行以下操作:

  1. 右键单击要取消忽略的文件。
  2. 点击“团队”发布该内容。
  3. 选择“高级”。
  4. 最后单击“不假设不变
  5. 现在您可以看到您的文件未被忽略。

If you are using eclipse/STS, then you can do this:

  1. Right click on the file you want to unignore.
  2. Click on 'Team' post that.
  3. Select 'Advanced'.
  4. And finally click on 'No Assume Unchanged'
  5. Now you can see that your file is unignored.
断桥再见 2024-12-20 09:41:11

更好的问题可能是 - 你为什么要这样做?如果您想要跟踪某个文件,请取消忽略它(在 .gitignore 文件中为其模式添加 ! 前缀,例如 !dont/ignore/this/file)。 ——奥哈德·施奈德

有时我们可能会更改配置文件中的 API_KEY,并且需要确保我们不会意外地通过 git add * 上传它。

config.json

{
    "DEBUG": true,
    "TESTCASE_DIR": "testcases/",
    "OUTPUT_DIR": "output",  <--- a new attribute, we want to modify it 
                                  and update to the git repository
    "API_KEY": "********-****-****-****-************",
    "API_URL": "https://api-ffm-stg.twcc.ai/api/models/conversation",
    ...
}

解决方案(例如:config.json

如果git add --force config.json未达到预期效果,请尝试以下步骤解决问题:

  1. 确保.gitignore正确忽略config.json

    确保您的 .gitignore 文件包含 config.json

    <前><代码>__pycache__/
    *。日志
    配置.json

  2. 从版本控制中删除config.json(此步骤至关重要)

    如果 config.json 之前被跟踪,您需要将其从 Git 索引中删除,然后重新添加:

    git rm --cached config.json
    
  3. 提交更改(此步骤将删除 config.json 的当前版本)

    git commit -m "从版本控制中删除 config.json"
    
  4. 强制添加 config.json

    现在您可以修改/更新config.json并强制添加它:

    git add --force config.json
    
  5. 再次提交:

    提交强制添加的config.json

    git commit -m “尽管 .gitignore 仍强制添加 config.json”
    
  6. 推送到远程仓库

    将更改推送到 GitHub:

    git Push origin master
    

在此处输入图像描述

这些步骤应该可以解决您的问题。如果您仍然无法强制添加 config.json,请确保您的 Git 客户端没有配置问题,并且没有 Git 挂钩或设置干扰该过程。

关键下一步:

  1. 不要再次跟踪文件 config.json
    $ git status
    # 尝试再次修改`config.json`,例如编辑API_KEY并保存
    # 再次检查状态是否被追踪
    $ git 状态
    
    # 关键动作
    $ git update-index --assume-unchanged config.json
    
    # 再次检查状态是否被追踪
    $ git 状态
    

A better question may be - why would you want to do that? If you want a file tracked, unignore it (prefix its pattern in the .gitignore file with !, e.g. !dont/ignore/this/file). – Ohad Schneider

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:

{
    "DEBUG": true,
    "TESTCASE_DIR": "testcases/",
    "OUTPUT_DIR": "output",  <--- a new attribute, we want to modify it 
                                  and update to the git repository
    "API_KEY": "********-****-****-****-************",
    "API_URL": "https://api-ffm-stg.twcc.ai/api/models/conversation",
    ...
}

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:

  1. Ensure .gitignore correctly ignores config.json:

    Make sure your .gitignore file contains config.json:

    __pycache__/
    *.log
    config.json
    
  2. 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:

    git rm --cached config.json
    
  3. Commit the changes (this step will remove the current version of config.json):

    git commit -m "Remove config.json from version control"
    
  4. Force-add config.json:

    Now you can modify/update the config.json and force-add it:

    git add --force config.json
    
  5. Commit again:

    Commit the force-added config.json:

    git commit -m "Force add config.json despite .gitignore"
    
  6. Push to the remote repository:

    Push the changes to GitHub:

    git push origin master
    

enter image description here

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:

  1. DO NOT track the file config.json again:
    $ git status
    # try to modify `config.json` again, e.g. edit the API_KEY and save it
    # check the status again to see if it is be tracked
    $ git status
    
    # the critical action
    $ git update-index --assume-unchanged config.json
    
    # check the status again to see if it is be tracked
    $ git status
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文