在GitHub上删除分支上的远程提交。

huangapple go评论67阅读模式
英文:

Remove remote commits on the branch in GitHub

问题

ALL,

我很久以前在我的分支上创建了一个本地分支,并提交了一些更改。然后,我提交了一个PR,通过了CI构建。

现在过了一段时间,我回到了同一台机器,我制作了PR,但出于某种原因,我没有检查我在哪个分支上,并在旧分支上进行了几次提交,然后推送了它们,因此搞砸了PR(它尚未合并,因为缺少测试代码)。

现在我想做的是进入GitHub Web界面,删除这些提交,但在本地保留它们,因为我可以在我的本地机器上生成一个补丁,删除这些提交,切换到新分支并将补丁应用到它上面。

或者甚至可能有更好的解决方案?

那么,我该如何解决这个混乱的情况呢?

请记住 - 我打算完成PR并进行测试,但这两者是完全无关的事情。

TIA!!

编辑:

一切都正常工作,我的原始笔记本上的旧分支恢复正常,PR现在也很好。

然而,为了进行单元测试,我不得不去另一台机器并执行 git pull。由于某种未知原因,之后该机器上的git树变得混乱,包括糟糕的提交。

我能够使用 git reset --hard N 撤消糟糕的提交,但我担心在尝试在所有不同的平台/不同的笔记本上测试我的单元测试时会发生同样的情况,这意味着我的更改将丢失,我将需要在所有不同的机器上重新进行更改以进行UT。

你能帮助我吗?

TIA!!

英文:

ALL,

I made a local branch in my fork a long time ago and pushed some changes to it. I then submitted a PR which was passed the CI build.

Now after some time I came back to the same machine I produced the PR but for some reason I didn't check which branch I was on and made couple of commits on the old branch and pushed them therefore screwing up the PR (it was not yet merged, due to the lack of the test code).

Now what I'd like to do is go to Github Web interface, remove those commits, but keep them locally, because I can just generate a patch on my local machine, remove those commits, switch to the new branch and apply the patch to it.

Or maybe even there is a better solution?

So how do I solve this mess?

Keep in mind - I intend to finish the PR with the test, but those are 2 completely unrelated things.

TIA!!

EDIT:

Everythig worked fine and my old branch on the original laptop is back to normal and the PR is now good.

However, in order to put the unit test I had to go to a different machine and do a git pull. For some unknown reason after that the git tree on that machine becomes clogged with everything including the bad commit.

I was able to revoke bad commits with git reset --hard N, but I fear that the same happen when I try to test my unit test on all platforms/different laptops which means my changes will be lost and I will need to redo them again for the UT on all different machines.

Can you help me here as well?

TIA!!

答案1

得分: 0

以下是翻译好的部分:

在一番思考后,我原来的回答比严格必要的更加复杂,但我将保留它如下。

要将你的原始分支恢复到旧状态并保留新提交的最简单方法是创建一个新分支,然后重置旧分支并强制推送。操作如下:

git checkout old-branch
git branch new-branch
git reset --hard <要在旧分支中保留的提交的哈希>
git push -f

或者,你可以使用以下命令:

git reset --hard HEAD~n

其中 n 是你想要从旧分支中移除的提交数。

现在,你可以对新分支进行任何操作,比如将其变基到 main 分支。这可能并非完全必要。例如,如果你的PR已合并,你需要在进行第二个PR之前将这些更改拉到新分支中。然而,如果你想在第一个PR合并之前提交第二个PR,那么最好保持它们分开,直到其中一个被合并。

TLDR(简而言之)

修复远程存储库的最简单方法是首先在本地进行更改,然后推送,可能需要强制推送到GitHub或其他远程存储库。

详细说明

你可以首先在本地执行所有操作,然后推送到GitHub以修复PR。首先,你应该创建一个新分支,并使用 git cherry-pick 操作复制你想要保留但要从其他分支中删除的提交。

首先,获取你想要的提交的哈希值:

git checkout old-branch
git log --oneline --graph

复制你想要移动的提交的哈希值,然后执行以下操作:

git checkout -b new-branch main

然后,对于你复制的每个哈希值:

git cherry-pick <哈希值>

或者,你可以使用 git rebase 更轻松地执行此操作。你只需要最旧提交的哈希值:

git checkout -b new-branch old-branch
git rebase --onto main <最旧提交的哈希值>~

现在返回到旧分支,并删除所有不再需要的提交:

git checkout old-branch
git reset --hard <要在此分支上保留的第一个提交的哈希值>

最后,强制推送:

git push -f

如果你在 git reset 命令中使用了正确的哈希值,这将自动将PR恢复到其原始状态。

英文:

After some thought, my original answer is more complicated than strictly necessary, but I'll leave it below.

The easiest way to get your original branch back to its old state and keep the new commits is to create a new branch then reset the old branch and force push. It looks like this:

git checkout old-branch
git branch new-branch
git reset --hard &lt;hash of commit you want to keep in old-branch&gt;
git push -f

Alternatively you can use

git reset --hard HEAD~n

where n is the number of commits you want to remove from the old branch.

Now you can do whatever you wish with the new branch, such as rebase it onto main. This might not be entirely necessary. If for example, your PR is merged, you will need to pull those changes into the new branch anyway before making the second PR. However, if you want to make a 2nd PR before the 1st is merged, then it is better to keep them separate until one of them is merged.

TLDR

The easiest way to fix a remote repository is to first make the changes locally and then push, possibly force push, to GitHub or other remote.

Details

You can do this all locally first, then push to GitHub to fix the PR. First, you should create a new branch and git cherry-pick the commits that you want to keep but remove from the other branch.

Start by getting the hashes of the commits you want:

git checkout old-branch
git log --oneline --graph

Copy the commit hashes for the commits you want to move. Then do

git checkout -b new-branch main

and for each of the hashes you copied:

git cherry-pick &lt;hash&gt;

Alternatively, you can do this more easily with git rebase. You only need the hash of the oldest commit you want to keep:

git checkout -b new-branch old-branch
git rebase --onto main &lt;hash of oldest commit&gt;~

Now go back to your old branch and get rid of all the commits you no longer want:

git checkout old-branch
git reset --hard &lt;hash of the first commit you want to keep on this branch&gt;

Finally force push:

git push -f

This will automatically update the PR back to its original state, if you used the correct hash for the git reset command.

huangapple
  • 本文由 发表于 2023年6月1日 11:19:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定