Git:使用恢复或签出来撤消推送的更改?

发布于 2024-10-14 11:20:52 字数 493 浏览 3 评论 0原文

Git 是一个非凡的工具,但我还没有想好撤消推送更改的最佳方法。情况是这样的。

我在一个分支上,并已将多个提交推送到 GitHub。从那以后我就决定我已经在兔子洞里走得太远了,我们需要废弃我已经完成的一些提交,然后重新开始。本质上,我需要反转所有推送的提交,回到上一个提交。这是我认为合适的两个命令

git revert  # - creates a new commit that "undoes" the changes of one specific commit
git checkout 'commit SHA' # - sets the head to that specific commit, wherein I will re-push to the repo, undoing my changes... I think

那么,我是对的吗?我是否需要对要返回的特定提交进行 git checkout?或者说这个复杂的过程中有什么我不明白的地方?

谢谢。

Git is a phenomenal tool, but I have yet to wrap my mind around the best way to undo pushed changes. Here's the situation.

I'm on a branch, and have pushed several commits to GitHub. It has since been decided that I've gone too far down the rabbit hole, and we need to scrap several of the commits I've done, and start over. Essentially, I need to reverse all of the pushed commits, back to a previous one. Here are the two commands that I think are appropriate

git revert  # - creates a new commit that "undoes" the changes of one specific commit
git checkout 'commit SHA' # - sets the head to that specific commit, wherein I will re-push to the repo, undoing my changes... I think

So, am I right? Do I need to do a git checkout on the specific commit I want to return to? Or is there something in this convoluted process that I am not understanding?

Thanks.

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

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

发布评论

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

评论(3

滥情空心 2024-10-21 11:20:52

根据您推送的状态,有两种情况:

  • 尚未有人使用推送的分支进行任何操作。在这种情况下,您可以使用 git reset 强制本地分支进行特定提交 - 然后您可以使用 --forcegit push 分支code> 参数。

  • 但是,如果有人将他的工作基于您不小心推送的分支,那么您无法真正重置它,因为他将基于一个不确定的分支进行更改。这就是 git revert 发挥作用的地方。它将记录一个反向补丁,有效地撤消早期的更改。这里的优点是其他人可以轻松地将他们的工作基于该分支。

方法的选择取决于您的存储库的情况以及意外补丁在那里存在的时间。如果开发人员很少,沟通和重置可能就是答案。但如果这个东西存在了很长一段时间,最好还是恢复——除非你想重写整个历史!

另一种看待它的方式是: git revert 是持久性的,而 git reset / git push --force 是对历史记录的破坏性重写。两者都有一个合适的时间。

最后,当我深入跟随爱丽丝进入兔子洞并调查兔子洞之外的情况时,我通常在本地创建的分支上进行。然后,如果我喜欢这些更改,我通常会将它们合并到测试分支中,并让它们在测试分支上稍微搅拌一下,然后再将它们合并到 master 中。这样你就可以在很多时候避免这个问题。简而言之,我通常有 20-30 个本地分支机构,每个分支机构负责我正在开发的每个功能。他们往往首先进行单独测试。有时,我会创建一个新分支 test 并将所有内容合并到该分支中,然后一起进行所有测试。为了跟踪跨分支的冲突,我使用 git rerere。优点是我可以决定某个功能何时足够稳定以推送给其他人。

There are two scenarios depending on the state of what you pushed:

  • Nobody has used the pushed branch for anything yet. In that case you can use git reset to force the local branch to a specific commit - and then you can git push the branch with a --force parameter.

  • If, however, there are someone who has based his work off on the branch you accidentally pushed, then you can't really reset it, as he will then be basing his changes off of a branch in limbo. This is where git revert comes into play. It will record an inverse patch effectively undoing earlier changes. The advantage here is that other can base their work on the branch with ease.

The choice of method depends on how your repository is and for how long the accidental patches have been up there. If there are few developers, communication and a reset is probably the answer. But have the thing lived for a long time, it is probably better to revert - unless you want to rewrite the whole history!

Another way to look at it is this: git revert is persistent whereas git reset / git push --force is destructive rewriting of the history. There is an appropriate time for both.

Finally, when I delve to follow Alice down the Rabbit hole and investigate what lies beyond, I usually do it on locally created branches. Then if I like the changes, I usually merge them into a test branch and let them stir a bit on the test branch, before I merge them to master. That way you avoid the problem a lot of the time. In Short, I often have 20-30 local branches, one for each feature I am working on. They tend to be tested individually first. Occasionally, I create a new branch test and merge everything into that branch and do all the tests together. To track conflicts across branches I use git rerere. The advantage is that I can decide when a feature is stable enough to push to others.

我还不会笑 2024-10-21 11:20:52

正如 Horia 正确指出的那样,你需要 git重置。

Git checkout 意味着您切换到另一个分支,或者通过使用 checkout -b 您可以创建一个新分支并立即切换到它。

As Horia pointed out correctly you'll need git reset.

Git checkout means you switch to another branch or by using checkout -b you can create a new branch and switch to it right away.

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