删除gitlab上的提交

Delete commit on gitlab

如何删除我在 GitLab 上所做的提交?我所做的这个提交现在不是 HEAD。

如果我不能删除它,我可以编辑吗?

当它是 HEAD 时,我试过:

git reset  --soft HEAD

git reset  --soft HEAD^1

git revert HEAD

git rebase -i HEAD

git rebase -i HEAD~1

git reset --hard HEAD

git reset --hard Id-Code

我已经尝试对其进行变基,但它仍然留在分支上。现在我只是将它从 HEAD 中删除,但它仍然存在。

还有一个命令?

假设您遇到以下情况:

* 1bd2200 (HEAD, master) another commit
* d258546 bad commit
* 0f1efa9 3rd commit
* bd8aa13 2nd commit
* 34c4f95 1st commit

您要删除 d258546 的位置,即 "bad commit"。

您应尝试使用交互式变基将其删除:git rebase -i 34c4f95

然后您的默认编辑器将弹出类似这样的内容:

 pick bd8aa13 2nd commit
 pick 0f1efa9 3rd commit
 pick d258546 bad commit
 pick 1bd2200 another commit

 # Rebase 34c4f95..1bd2200 onto 34c4f95
 #
 # 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

只需删除包含要删除的提交的行并保存并退出编辑器:

 pick bd8aa13 2nd commit
 pick 0f1efa9 3rd commit
 pick 1bd2200 another commit
 ...

git 将继续从您的历史记录中删除此提交,留下如下内容(注意已删除提交的提交后代中的哈希更改):

 * 34fa994 (HEAD, master) another commit
 * 0f1efa9 3rd commit
 * bd8aa13 2nd commit
 * 34c4f95 1st commit

现在,因为我假设您已经将错误的提交推送到 gitlab,您需要将您的图形重新推送到存储库(但使用 -f 选项来防止它由于不可快速转发的历史记录而被拒绝,即 git push -f <your remote> <your branch>)

请格外小心,并确保 none 同事已经在其分支机构中使用包含 "bad commit" 的历史记录。

替代选项:

您可以简单地创建一个新的提交来否定由您的错误提交引入的更改,而不是重写历史,为此只需键入 git revert <your bad commit hash>。这个选项可能不那么干净,但更安全(如果你不完全清楚你在用交互式变基做什么)。

  1. git reset --hard CommitId
  2. git push -f origin master

第一个命令会让你的头脑停留在 commitid 上,第二个命令会删除 master 分支上那个提交 id 之后的所有提交。

Note: Don't forget to add -f in push otherwise it will be rejected.

我们遇到过类似的问题,仅删除提交并强制推送到 GitLab 是不够的。
它仍然可以在 GitLab 界面中使用 url:

https://gitlab.example.com/<group>/<project>/commit/<commit hash>

我们不得不从 GitLab 中删除项目并重新创建它以在 GitLab 中删除此提交 UI。

我假设您想编辑所有提交以从中删除一些字符串,例如泄露的凭据。

如前所述,真正从 Gitlab 服务器清除旧提交是一项棘手的任务 在 Gitlab purge doc 中,因为它涉及从中删除 all 内部引用 服务器。这是可能的 Gitlab 版本高于或等于 11.6(参见所述文档)。

主要步骤如下:

  • 安装git-filter-repo
  • 通过导出项目从 Gitlab 下载所有 git 引用
  • 然后循环这些步骤:
    • 使用 git filter-repo
    • 更新存储库内容
    • 上传修改后的内容
    • 清理悬挂引用 完成后,Gitlab 项目将清除所有对这些文件的引用 旧提交。

一句警告

请注意,在此之前制作的回购协议的任何本地副本仍将包含 删除的提交。此外,试图从那些旧的本地副本中提取的用户将 搞得一团糟。

详细过程

  • 安装git-filter-repo
  • Gitlab export doc 之后从 Gitlab 导出项目:转到 Settings->Advanced,点击Export project button,关注link 您通过电子邮件收到下载导出
  • 创建一个临时文件夹并跳转到该文件夹​​:mkdir tmp && cd tmp
  • 解压导出的存档:tar xf path/to/downloaded...export.tar.gz
  • 克隆项目包:git clone --bare --mirror export.bundle
  • 复制您想要更改的所有表达式为expressions.txt

示例内容:

a_sample_password==>DELETED
  • 跳转到克隆的仓库:cd project.git
  • 更新原始遥控器:git remote set-url origin YOUR_REMOTE_ORIGIN
  • 运行 git filter-repo --replace-text ../expressions.txt
  • 转到 Gitlab Settings->Repository->Protected 并取消保护任何受保护的 分支机构
  • git push origin --force 'refs/heads/*'上传结果
  • 使用 git push origin --force 'refs/replace/*'
  • 删除悬空提交
  • 等待 30 分钟
  • 在 Gitlab Settings->Repository、运行 中使用文件清理存储库 filter-repo/commit-map
  • 重新保护Gitlab中未受保护的分支Settings->Repository->Protected

git-filter-repo examples 查看更多示例。 Gitlab 清理细节在 Gitlab docs on repo cleanup. I made a sample repository 来说明这个过程。