Git - 删除并排除配置文件

发布于 2024-08-13 07:58:18 字数 98 浏览 9 评论 0原文

两周前,我提交了包含密码的应用程序的配置,但它不是很有用。 如何从提交历史记录中删除该文件并确保它不会被重新提交?

我想从所有提交树中删除该文件,因为它包含我的密码。

2 weeks ago, I commited config of my application which had my password, it's not very useful.
How can I remove the file from the commit history and make sure it doesn't get re-commited?

I want to remove the file from all commits tree because it contains my passwords.

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

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

发布评论

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

评论(4

残龙傲雪 2024-08-20 07:58:18

您可以

git rm myConfigFile
echo myConfigFile > .gitignore
git add .gitignore
git commit -m "from now on, no more myConfigFile"

另一种极端的方法(特别危险,特别是如果您已经将存储库推送到远程存储库)是从所述存储库的历史记录中完全删除该文件:(

git filter-branch --index-filter 'git update-index --remove myConfigFile' HEAD

小心使用,并首先进行备份)

问题< a href="https://stackoverflow.com/questions/872565/how-do-i-remove-sensitive-files-from-gits-history">如何从 git 历史记录中删除敏感文件 有更多内容关于这个敏感话题。

此过程存在双重问题:

  1. 如果您的存储库已被克隆,您永远无法保证机密信息会真正从其他存储库中“消失”。
  2. 当其他人在此之后尝试下拉您的最新更改时,他们会收到一条消息,指示无法应用更改,因为它不是快进。
    要解决此问题,他们必须删除现有存储库并重新克隆它,或者按照 git-rebase 手册页
    在这两种情况下,您的机密信息都不会被“悄悄”替换或删除......

You can

git rm myConfigFile
echo myConfigFile > .gitignore
git add .gitignore
git commit -m "from now on, no more myConfigFile"

The other extreme approach (dangerous especially if you have already pushed your repo to a remote one) would be to entirely remove that file from the history of said repo:

git filter-branch --index-filter 'git update-index --remove myConfigFile' HEAD

(to use with care, and with a backup first)

The question How do I remove sensitive files from git’s history has more on that sensitive topic.

The problems with this process are twofold:

  1. If your repo has already be cloned, you can never guarantee the confidential information will be really "gone" from every other repo.
  2. When others try pull down your latest changes after this, they'll get a message indicating that the the changes can't be applied because it's not a fast-forward.
    To fix this, they'll have to either delete their existing repository and re-clone it, or follow the instructions under "RECOVERING FROM UPSTREAM REBASE" in the git-rebase manpage.
    In both case, your confidential information will not be "quietly" replaced or erased...
铜锣湾横着走 2024-08-20 07:58:18
cp my-config config.tmp
git rm my-config
git commit -m 'removed config'
mv config.tmp my-config
echo my-config >> .gitignore
git add .gitignore
git commit -m 'ignore config'
cp my-config config.tmp
git rm my-config
git commit -m 'removed config'
mv config.tmp my-config
echo my-config >> .gitignore
git add .gitignore
git commit -m 'ignore config'
守不住的情 2024-08-20 07:58:18

将文件名添加到 .gitignore 文件中。

链接

Add the file name to a .gitignore file.

Link

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