是否可以编辑Github提交历史记录?

时间:2017-08-02 12:05:43

标签: git github

在构建项目时,我使用的是私有仓库,为了加快测试速度,我使用数据库和其他凭据作为代码中的变量。

现在项目依赖于环境。因此,回购中没有秘密的东西。我想把它公之于众。

我不希望人们在提交历史记录中看到包含这些凭据的部分。

是否可以删除这些行或将所有提交历史记录设为私有?

3 个答案:

答案 0 :(得分:3)

您需要将上传到github的任何凭据视为不再保密。即使您重新绑定,删除它们,然后像​​其他人建议的那样强制推送,一旦您将存储库公开,仍然可以通过url(具有基于提交ID的简单格式)访问提交。

有两种方法可以公开代码,而无需让人们访问您的凭据。

  1. 更改您的凭据,公开存储库并让其他人从您的错误中吸取教训(我之前已经上传了秘密,并且看过其他有经验的开发人员也这样做了,所以不要感到孤独!)。

  2. 将存储库保密,制作本地副本,重新绑定(确保删除所有机密!),然后推送到新的公共存储库。

  3. 我会做选项1,但两者都是安全的。

答案 1 :(得分:0)

是的,它可能, 以下是一些资源:

Changing previous Commits messages with sourceTree [如果您想更改许多提交消息,则有用]

Changing Commit messages at github documentation

最后你可以编辑最后一次提交:
git commit --amend

然后用它推动它:

git push --force example-branch

答案 2 :(得分:0)

两个答案都已过时。现在可以更改历史记录了。

以以下为例,您有以下内容:

0c39034 commit you want removed
f7fde4a commit that you want to be in

请勿使用还原,因为还原会导致您:

e499d89 commit that supposedly reverts 0c39034

然后,您需要重新设置并压缩要删除的提交,如下所示:

git rebase -i HEAD~3


pick f7fde4a commit that you want to be in
squash 0c39034 commit you want removed
squash e499d89 commit that supposedly reverts 0c39034
# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

做一个 git log 来确认。

git log

推送您的更改:

git push --force

这样做将完全删除历史记录中的 0c39034 和 e499d89。如果(且仅当)您拥有完整的提交 ID,它们仍然可以在 web ui 中访问。它会说:

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

最后我想说,@instantepipany 所说的作为最佳实践是正确的,并确保永远不要在 github 上硬编码凭据。