【问题标题】:How do I edit past git commits to remove my password from the commit logs?如何编辑过去的 git 提交以从提交日志中删除我的密码?
【发布时间】:2012-01-13 17:34:22
【问题描述】:

我的问题:cygwin git 在使用 https:// URL 时似乎没有正确提示输入凭据,所以我在 URL 中使用了用户名和密码。不幸的是,当我执行“get pull”时,它会自动提交一条带有完整 URL 的消息,包括密码。直到我推送更改后才注意到这一点。

如何编辑旧的提交消息以消除 URL 中的密码?

我的共享 git 存储库在我自己的服务器上。如有必要,我可以对 repo 进行手术。

关于如何更改我的配置(即不要使用 Cygwin,不要使用 https)的说明是不必要的——我正在尝试处理已经完成的事情。

是的,我可以并且会烧掉密码,但我仍然想修复它。

【问题讨论】:

标签: git commit commit-message


【解决方案1】:

要从 git 存储库及其历史记录中完全删除文件,请使用这些命令。

# Check out the remote repo
git clone git://host/path/repo.git
cd repo

# Clobber the file in your checkout
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch file-to-purge.txt' --prune empty --tag-name-filter cat -- --all

# Make sure you don't accidentally commit the file again
echo file-to-purge.txt >> .gitignore
git add .gitignore
git commit -m "Prevent accidentally committing this again" .gitignore

# Push the edited repo. This will break other people's clones, if any.
git push origin master --force

如需了解更多信息,remove sensitive data GitHub 指南将为您提供帮助。

【讨论】:

    【解决方案2】:

    在 git-hub 上删除敏感数据的链接很有用。但是,我发现了一个非常易于使用的工具:Eric Raymond reposurgeon

    这个工具让我可以轻松地导入我的 repo,列出带有问题的提交,编辑它们(我是单独进行的)并写出我的 repo 的 git fast-import 流。我将该流导入一个新的 repo 并 rsync'd 到位。

    缺点是我的旧回购完全死了——我改变了历史。根据文档,使用“git filter-branch”也是如此。

    【讨论】:

    • 一段时间以来一直在寻找使用该工具的借口,但文档非常单一 - 没有示例。很高兴有一个使用 reposurgeon 的示例“只删除一个该死的文件”。
    【解决方案3】:

    如果您可以编辑服务器,则可以将分支头重置为上一个(HEAD^)。

    • 首先,获取要“还原”到的 HEAD^ 哈希。
    • 转到(git 裸存储库在服务器中的目录)/refs/heads,更改为用户 git(或任何 git 服务),运行“echo (hash) > (branch name)”进行重置。

    就是这样。顺便说一句,在执行上述操作之前,您无法更改拉取的回购

    【讨论】:

      猜你喜欢
      • 2012-10-27
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 2014-05-16
      相关资源
      最近更新 更多