简体   繁体   English

撤消功能分支合并

[英]undo feature branch merge

I've been working on a new feature on a branch for the last while.最近一段时间,我一直在研究一个分支上的新功能。 When the feature was finished, I squash-merged the feature branch back to master, and deleted the feature branch.功能完成后,我将功能分支压缩合并回 master,并删除了功能分支。 The hash of the merge commit is 7ba3dc2fea43cd33c4d5ba11baf2765cbbdb0284.合并提交的 hash 为 7ba3dc2fea43cd33c4d5ba11baf2765cbbdb0284。

I now need to revert this merge.我现在需要恢复这个合并。 Specifically, what I want to achieve is:具体来说,我想要实现的是:

  1. the changes for the new feature are removed from master新功能的更改已从 master 中删除
  2. the changes for the new feature are on a feature branch which could subsequently be merged to master新功能的更改位于功能分支上,随后可以合并到主分支

Some additional information which may be relevant:一些可能相关的附加信息:

  • there have been a few commits to master subsequent to the merge commit, but it doesn't matter if undoing the merge loses these commits, because the changes are trivial to re-apply在合并提交之后有一些提交要掌握,但是撤消合并是否会丢失这些提交并不重要,因为重新应用这些更改是微不足道的
  • I don't have the ability to commit directly to master, so the process of undoing the merge will have to be achieved by creating branches, raising a PR, then merging the branch to master once the PR has been approved.我没有能力直接提交给master,所以撤消合并的过程必须通过创建分支,提出PR,然后在PR获得批准后将分支合并到master来实现。

How can I move the changes for the new feature back to a feature branch?如何将新功能的更改移回功能分支?

For once, the fact that you used a squash merge rather than a true merge is going to make your life easier.这一次,您使用 squash 合并而不是真正的合并这一事实将使您的生活更轻松。 (I'm assuming that by "squash-merge", you mean the --squash argument to git merge , or equivalent functionality in other UIs.) (我假设“squash-merge”是指git merge--squash参数,或其他 UI 中的等效功能。)

A "squash merge" doesn't actually reference the original commits in any way, it just creates a new commit which applies all the changes they would have brought in. So to undo that commit, you can create a new branch from master, and use git revert to create a new commit that undoes all the changes from the squash merge commit. “壁球合并”实际上并没有以任何方式引用原始提交,它只是创建一个新提交,应用他们将带来的所有更改。因此,要撤消该提交,您可以从 master 创建一个新分支,并且使用git revert创建一个新的提交,撤消来自壁球合并提交的所有更改。

To get the old branch back, you need to find the commit hash it was pointing at when you deleted it.要恢复旧分支,您需要找到删除它时它指向的提交 hash。 If you used a "Pull Request" or "Merge Request" in a web UI (Github / Gitlab / BitBucket), it will be listed there (and might even have a "restore branch" link).如果您在 web UI(Github / Gitlab / BitBucket)中使用了“拉取请求”或“合并请求”,它将在此处列出(甚至可能有“恢复分支”链接)。 If you had it checked out locally, it will probably be visible in git reflog .如果您在本地检查它,它可能会在git reflog中可见。 Once you've found it, just type git branch feature-foo-resurrected abc123def , replacing "abc123def" with the commit hash you've found.找到它后,只需键入git branch feature-foo-resurrected abc123def ,将“abc123def”替换为您找到的提交 hash。

As a last resort, you could create a branch based on the squash commit, by using git cherry-pick to create a new commit with the same content.作为最后的手段,您可以基于 squash 提交创建一个分支,方法是使用git cherry-pick创建一个具有相同内容的新提交。 (Just pointing a branch at that commit won't help, because that's already in the history of "master", so will show as "nothing to merge".) (仅将分支指向该提交将无济于事,因为这已经在“master”的历史记录中,因此将显示为“nothing to merge”。)

So the whole sequence might look like:所以整个序列可能看起来像:

git switch master
git pull --ff-only origin

# create a branch which undoes the squash merge
git switch -c revert-feature-foo
git revert 7ba3dc2fea
git push origin

# now create a branch which *redoes* the squash merge on top of that
# only needed if you can't find the original feature branch
git switch -c feature-foo-resurrected
git cherry-pick 7ba3dc2fea
git push origin

You can try the below approach您可以尝试以下方法

Revert a commit恢复提交

$ git revert --no-commit '<commitnumber>'
# Files that were modified in the commit will be changed in your working directory
$ git commit -a -m "Revert commit <commit number>"

Create a feature branch and cherry pick commit创建一个功能分支和樱桃选择提交

$ git checkout -b 'Feature Branch name'
$ git cherry-pick '<commitnumber>'
$ git commit

Most of the time you would end up with some conflicts based on the number files which have been changed in the git repo in subsequent commits大多数情况下,基于后续提交中 git 存储库中已更改的文件数量,您最终会遇到一些冲突

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM