git 删除 gitlab 上的提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40245767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:35:20  来源:igfitidea点击:

Delete commit on gitlab

gitgitlab

提问by mhery

How can I delete a commit that I made on GitLab? This commit that I made is not the HEAD now.

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

If I can't delete it, can I edit?

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

When it was the HEAD, I tried:

当它是 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

I already tried to rebase it, but it still stays on the branch. Now I just removed it from the HEAD, but it is still there.

我已经尝试重新设置它,但它仍然保留在分支上。现在我刚刚从 HEAD 中删除了它,但它仍然存在。

There is another command?

还有其他命令吗?

回答by álvaro P.

Supose you have the following scenario:

假设您有以下场景:

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

Where you want to remove d258546 i.e. "bad commit".

您要删除 d258546 的位置,即“错误提交”。

You shall try an interactive rebase to remove it: git rebase -i 34c4f95

你应该尝试一个交互式 rebase 来删除它: git rebase -i 34c4f95

then your default editor will pop with something like this:

那么您的默认编辑器将弹出如下内容:

 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

just remove the line with the commit you want to strip and save+exit the editor:

只需删除带有要删除的提交的行并保存+退出编辑器:

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

git will proceed to remove this commit from your history leaving something like this (mind the hash change in the commits descendant from the removed commit):

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

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

Now, since I suppose that you already pushed the bad commit to gitlab, you'll need to repush your graph to the repository (but with the -foption to prevent it from being rejected due to a non fastforwardeable history i.e. git push -f <your remote> <your branch>)

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

Please be extra careful and make sure that none coworker is already using the history containing the "bad commit" in their branches.

请格外小心,并确保没有同事已经在其分支中使用包含“错误提交”的历史记录。

Alternative option:

替代选项:

Instead of rewrite the history, you may simply create a new commit which negates the changes introduced by your bad commit, to do this just type git revert <your bad commit hash>. This option is maybe not as clean, but is far more safe (in case you are not fully aware of what are you doing with an interactive rebase).

您可以简单地创建一个新提交来否定由您的错误提交引入的更改,而不是重写历史记录,只需键入git revert <your bad commit hash>. 此选项可能不那么干净,但更安全(以防您不完全了解您使用交互式 rebase 做什么)。

回答by ABHAY JOHRI

1.git reset --hard CommitId

2.git push -f origin master

1.git reset --hard CommitId

2.git push -f origin master

1st command will rest your head to commitid and 2nd command will delete all commit after that commit id on master branch.

第一个命令将使您的头脑停留在 commitid 上,第二个命令将删除 master 分支上该提交 id 之后的所有提交。

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

注意:不要忘记在 push 中添加 -f 否则它会被拒绝。

回答by Tadeusz Kleszcz

We've had similar problem and it was not enough to only remove commit and force push to GitLab.
It was still available in GitLab interface using url:

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

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

We've had to remove project from GitLab and recreate it to get rid of this commit in GitLab UI.

我们不得不从 GitLab 中删除项目并重新创建它以摆脱 GitLab UI 中的这个提交。