如何 .gitignore 并删除已提交的文件而不影响其他工作副本?

发布于 2024-09-25 10:54:22 字数 618 浏览 5 评论 0原文

我有一个裸存储库和两个工作副本 - 一个在我的计算机上,另一个在服务器上。
事实证明,我必须 .gitignore 某个特定于每台机器的文件。我们将其命名为“settings.py”。该文件已经提交。

我确实将 'settings.py' 放入 .gitignore 中以忽略它。当我现在更改机器上的文件时,git status 仍然告诉我

modified:  settings.py

,我发现我必须像这样删除 settings.py:

git rm --cached settings.py

然后 git add .,然后是 git commit

但是,当我现在将其推送到裸存储库并将其拉到服务器上的工作副本时,settings.py 会在那里被删除 - 这很糟糕,因为我必须保留这个特定的设置。 py.

我想我可以制作 settings.py 的副本,并在删除后将其放回原处,但我觉得必须有更好的方法来做到这一点。

I have a bare repository and two working copies - one on my machine, the other on the server.
It turned out that I have to .gitignore a certain file that has to be specific for every machine. Let's call it 'settings.py'. This file is already committed.

I did put 'settings.py' in .gitignore to ignore it. When I now change the file on my machine git status still tells me

modified:  settings.py

I figured out that I have to remove settings.py like this:

git rm --cached settings.py

Then git add ., followed by git commit.

But when I now push this to the bare repo and pull it to the working copy on the server, settings.py is deleted there - which is bad because I have to keep this specific settings.py.

I figured that I just could make a copy of settings.py and put it back in once it is deleted, but I feel like there has to be a better way of doing this.

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

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

发布评论

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

评论(2

小…红帽 2024-10-02 10:54:22

您可以告诉 git 完全忽略对跟踪文件的更改,而不必删除它们。使用此命令:

git update-index --assume-unchanged [FILENAME]

然后,如果您想稍后跟踪该文件:

git update-index --no-assume-unchanged [FILENAME]

You can tell git to completely ignore changes to tracked files without having to remove them. Use this command:

git update-index --assume-unchanged [FILENAME]

Then if you want to track the file later:

git update-index --no-assume-unchanged [FILENAME]
不爱素颜 2024-10-02 10:54:22

您是否愿意将 settings.py 永久删除(本地和远程),并且:

  • 为远程端创建一个 setting_remote 文件,
  • 为本地端创建一个 setting_local 文件(即具有本地特定设置)
  • 添加过滤器驱动程序能够在结账时重建正确的settings.py文件。

alt text

这样,settings.py 就保持“私有”(非版本化)。但每个环境的具体值都是版本化的,每个值都在自己的文件中设置,没有任何合并问题。

Could you rather keep settings.py deleted permanently (both locally and on remote), and:

  • version a setting_remote file for the remote side
  • version a setting_local file for the local side (ie with local specific settings)
  • add a filter driver able to rebuild the right settings.py file on checkout.

alt text

That way, settings.py is kept "private" (non-versioned). But the specific values for each environment are versioned, each set in its own file, without any merging issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文