这是通过 github 恢复恢复更改的正确方法吗?

发布于 2025-01-14 13:20:32 字数 796 浏览 5 评论 0原文

在开发时,我将分支 a_feature 中的 pr 合并到 develop 分支中。然后我删除了我的电脑和 github 中的 a_feature 分支。

但我合并错了,我想把它改回来。因此,我做了一个新的 Pr,通过 github 的内置功能恢复更改并将其合并回来。 (我这样做是为了防止意外部署不需要的代码)

但我想继续将恢复的更改开发到新分支并进行新的公关。那么我采取的这种方法是正确的吗?

  1. 我拉动 develop 分支:
git checkout develop
git pull origin develop
  1. 我创建一个新分支:
git checkout a_feature_with_nessesary_changes
  1. 我查看包含恢复之前的更改的提交日志:
git revert ^hash^
  1. 然后我提交更改:
git add .
git commit -am "Un-reverted changes"
  1. 一旦准备好,我就会重新执行一个新的公关。

但是,一旦我通过 github 恢复一些合并更改,这是取消恢复的更改并丢失大量代码的方法吗?

Whilst I was developing I merged a pr from branch a_feature into develop branch. Then I deleted the a_feature branch both in my computer and in github.

But I mismerged it and I wanted to change it back. Therefore I did a new Pr that reverts the changes via github's build in feature and merged it back. (I did it in order to preventing unwanted code to accidentally be deployed)

But I want to continue the development of the reverted changes into a new branch and do a new pr. So is this approach I do the correct one?

  1. I pull the develop branch:
git checkout develop
git pull origin develop
  1. I do a new branch:
git checkout a_feature_with_nessesary_changes
  1. The I look upon the logs for the commit that contains the changes prior to revert:
git revert ^hash^
  1. Then I commit the changes:
git add .
git commit -am "Un-reverted changes"
  1. Once it is ready I re-do a new PR.

But is this the way to un-revert the reverted changes and lot lose code, once I revert some merge changes through github?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2025-01-21 13:20:32

有多种方法可以实现此目的,但关于 Git revert 最重要的是要认识到,恢复 PR 与恢复 commit 略有不同。嗯>。当您恢复常规提交时,您会撤消该特定提交的更改。当您恢复PR时,您正在恢复合并提交,并且该提交将撤消所有提交中的所有更改的总和是由那个公关带来的。

这很重要,因为如果您选择继续在同一个分支上工作,当需要合并分支的固定版本时,如果您希望重新合并的任何提交仍然具有与它们相同的 ID在之前的合并中,这些提交不会被再次引入。它们需要重新编写,所以我的偏好是始终从目标的最新版本开始(就像您建议的那样),这将强制它。这是一种稍微更有效的方法来创建该分支,而无需先签出 develop

# make a new branch off of the latest develop
git fetch
git switch -c a_feature_with_nessesary_changes origin/develop --no-track

现在您可以恢复恢复提交:

git revert <commit-id-of-revert-commit>`

或者,您可以挑选分支上的所有原始提交:

git cherry-pick <commit-id-a_feature-branched-from>..a_feature

请注意樱桃选择范围中的第一个提交是要选择的第一个提交的。您需要的父提交很可能等于此命令的输出:

git merge-base origin/develop a_feature

在 merge-base 正确的情况下,您可以将cherry-pick更改为类似这样的内容(使用Bash):

git cherry-pick $(git merge-base origin/develop a_feature)..a_feature

为了完整性,我会提到如果您确实想重用 a_feature 上的原始提交,而不从最新的 develop 开始(有些人不喜欢更改原始合并基础),那么您可以简单地强制重写分支以仅更改ID:

git switch a_feature
git rebase -f $(git merge-base origin/develop a_feature) # -f forces the rewrite

无论您选择上述哪个选项,现在您的分支都处于旧的(仍然损坏的)代码可以修复的状态。此时,您可以像平常一样添加新的提交以及您希望进行的更改并 PR 到 develop 中。

There are multiple ways to go here, but the most important thing to realize about Git revert is that reverting a PR is a little different than reverting a commit. When you revert a regular commit you undo the changes of that specific commit. When you revert a PR you are reverting a merge commit, and that commit will undo the sum of all the changes in all of the commits that were brought in by that PR.

This matters because if you choose to continue working on the same branch, when it comes time to merge in the fixed-up version of the branch, if any of the commits you wish to re-merge in still have the same ID as they did in the previous merge, those commits will not be brought in a second time. They need to be re-written, so my preference is to always start from the latest version of the target (like you suggested), which will force it. Here's a slightly more efficient way to create that branch without checking out develop first:

# make a new branch off of the latest develop
git fetch
git switch -c a_feature_with_nessesary_changes origin/develop --no-track

Now you can either revert the revert commit:

git revert <commit-id-of-revert-commit>`

Or, you can cherry-pick all of the original commits on the branch:

git cherry-pick <commit-id-a_feature-branched-from>..a_feature

Note that the first commit in the cherry-pick range is the parent of the first commit to be picked. It's likely that parent commit you need there is equal to the output of this command:

git merge-base origin/develop a_feature

In the case that merge-base is correct, you could change your cherry-pick to something like this (using Bash):

git cherry-pick $(git merge-base origin/develop a_feature)..a_feature

For completeness, I'll mention if you really wanted to reuse the original commits on a_feature without starting from the latest develop (some people don't like changing the original merge base), then you could simply force a rewrite of the branch in place to only change the IDs:

git switch a_feature
git rebase -f $(git merge-base origin/develop a_feature) # -f forces the rewrite

Regardless of which of the above options you selected, now your branch is in a state with the old (still broken) code that is ready to be fixed. At this point you can add your new commits with the changes you wish to make and PR into develop like you normally would.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文