简体   繁体   English

在Git中,如何重新排序(更改)推送提交?

[英]In Git, how can I reorder (changes from) pushed commits?

I have a repository with a single ( master ) branch: 我有一个包含单个( master )分支的存储库:

a > b > e > f > g > c > d

It turns out that I need the changes in c and d (two commits toward the end of the history so far) to occur earlier, so that I'd have: 事实证明,我需要更早地发生cd的变化(到目前为止的历史末尾的两次提交),以便我有:

a > b > c > d > e > f > g

Everything's been pushed. 一切都被推了。 I understand I'm not supposed to rebase pushed commits, so what can I do? 我知道我不应该推迟提交,所以我该怎么办?

Here's a quick and dirty solution that doesn't change history: 这是一个快速而肮脏的解决方案,不会改变历史记录:

  1. Use git revert to remove each commit from the branch. 使用git revert删除分支中的每个提交。
  2. Use git cherry-pick to reapply the commits in the order which you want. 使用git cherry-pick以您想要的顺序重新应用提交。

Push these changes. 推动这些变化。 This lets you change the order of the commits without changing the history. 这使您可以在不更改历史记录的情况下更改提交的顺序。

You cannot reorder pushed commits without git push -f . 如果没有git push -f你不能重新排序推送的提交。 If this repository is shared between you and other guys, then you shouldn't do anything. 如果您和其他人之间共享此存储库,那么您不应该做任何事情。 If this is your own repo or it's on your own branch, use git rebase -i <hash of a> , then reorder the commits as you want, then git push -f . 如果这是你自己的repo或它在你自己的分支上,使用git rebase -i <hash of a> ,然后根据需要重新排序提交,然后git push -f

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

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