[英]Removing commits from a branch
I am working on a React project that uses gh-pages
to deploy a website.我正在开发一个使用gh-pages
部署网站的 React 项目。 I accidentally merged my master
branch with my gh-pages
branch and brought all of the changes that were on master
to gh-pages
.我不小心将我的master
分支与我的gh-pages
分支合并,并将master
上的所有更改都带到了gh-pages
。 In total, it added 24 commits to my gh-pages
branch that weren't there before.总共,它向我的gh-pages
分支添加了 24 个以前没有的提交。
I was wondering if there was a way to revert these changes and to take my gh-pages
branch back to the 6 commits that were there to begin with?我想知道是否有办法恢复这些更改并将我的gh-pages
分支返回到一开始的 6 个提交?
If that is possible, is there a way to remove those 24 extra commits on the gh-pages
branch from GitHub?如果可能的话,有没有办法从 GitHub 中删除gh-pages
分支上的这 24 个额外提交?
I have looked at resources related to git reset --hard
and git rebase
and I am unsure if they would solve my problem.我查看了与git reset --hard
和git rebase
相关的资源,我不确定它们是否能解决我的问题。 I don't want to destroy the git structure of this project more than it already is.我不想破坏这个项目的 git 结构。
The safest thing to do is to git revert
the merge commit you made by accident.最安全的做法是git revert
您意外做出的合并提交。 By doing a git revert
it should be noted that you are adding a new commit which functionally undoes the merge commit from master
which you did by accident.通过执行git revert
,应该注意的是,您正在添加一个新提交,该提交在功能上撤消了您意外执行的来自master
的合并提交。 Here is how to do this:以下是如何做到这一点:
# from gh-pages
git log
# find the SHA-a of the merge commit, and then do
git revert -m 1 <merge commit hash>
Note that you probably want -m 1
here, which tells Git to revert back to the first parent, which typically would be your gh-pages
feature branch.请注意,您可能在这里需要-m 1
,它告诉 Git 恢复到第一个父级,这通常是您的gh-pages
功能分支。
Note that doing a revert commit here is preferable to trying to cut out the merge commit.请注意,在此处执行还原提交比尝试删除合并提交更可取。 The reason for this is that actually removing the merge commit means rewriting history, which can cause trouble for anyone else using your gh-pages
branch.这样做的原因是,实际上删除合并提交意味着重写历史记录,这可能会给使用您的gh-pages
分支的其他任何人带来麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.