Git重置和/或删除远程分支上的提交 [英] Git reset and/or delete commit on remote branch

查看:589
本文介绍了Git重置和/或删除远程分支上的提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于git,我有一个小问题.在项目开始时,我的工作流程整洁(图1和2).图1来自Intellij,而图2是运行gitk &命令之后的.正如预期的那样,我们看到了相同的状态.

I have a small issue regarding git. At the beginning of the project i have clean flow (figure 1 and 2). Figure 1 is from Intellij, while figure 2 is after running gitk & command. As expected we see same states.

但是在将"branch3"与"master"合并之后,我们意识到它不好,因此想要删除该提交.我们执行以下命令:git reset --hard HEAD~1.不出所料,执行此命令后,我们处于以下状态:

But after merging "branch3" with "master", we realize that it is not good and want to delete that commit. We execute following command: git reset --hard HEAD~1. As expected, after executing this command we are at following state:

然后从该状态创建新的分支"branch4".至此,一切都按预期进行.但是在我们推送"branch4"并将其与"master"分支合并后,我们会收到以下问题:

And from that state we create new branch "branch4". Till this point everything is working as expected. But after we push "branch4" and merge it with "master" branch we receive following issue:

如您所见,我们仍然具有"Commit on branch3"和"Merged in branch 3".请告诉我如何删除那些提交,因为我不想拥有这些提交的任何记录(类似于第一个图像).因为,显然git reset --hard不会删除远程提交.我想强调一点,推送是使用以下命令git push --force

As you can see we still have "Commit on branch3" and "Merged in branch 3". Please advise me how can i delete those commits, as I don't want to have any record of these commits (to be similar like the first image). Because, obviously git reset --hard does not delete remote commits. I would like to emphasize that pushing was done using following command git push --force

我应该改用什么命令?

P.S.使用git reset --soft我得到了相同的结果.

P.S. With git reset --soft I got the same.

推荐答案

获取看起来完全像您想要的本地提交历史记录.然后,将每个分支(push --forcepush -f)强行推入遥控器.这将为您强制按下的每个分支重写在远程站点上的历史记录,使其看起来与本地分支完全相同.

Get your local commit history looking exactly like you want it. Then force push (push --force or push -f) each branch to the remote. This will rewrite history on the remote for each branch you force push to look exactly as it does locally.

如果将远程git服务器或仓库配置为拒绝历史记录重写,则此操作将失败.您将不得不更改该配置. (默认情况下,GitHub不执行此操作,但GitLab会执行此操作.我不了解bitbucket)

If your remote git server or repo is configured to reject history rewriting this will fail. You will have to change that configuration. (GitHub doesn't do this by default, but GitLab does. I don't know about bitbucket)

获得分支后,您要尝试在本地和远程修复外观,您可能希望清除废弃分支的痕迹(您的branch3).请参阅如何在本地和远程删除Git分支?

After you've gotten the branches you're trying to fix looking right locally and on the remote, you may want to cleanup traces of the abandoned branch (your branch3). See How do I delete a Git branch locally and remotely?

⚠️请注意有关重写共享分支历史的注意事项:

  • 如果有人要对其分支的克隆进行更改,则您将不得不对其进行强制推送,将其合并可能会变得困难/混乱.
  • 如果有人对远程存储库提交了更改,则您的强制推送将覆盖它们.您可能需要考虑 git push --force-with-lease

这篇关于Git重置和/或删除远程分支上的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆