[英]How to cancel commits on master and move them to branch
I got a problem. 我有问题 I have done a couple of commits on master.
我已经对master进行了两次提交。 But I made a mistake.
但是我犯了一个错误。 I need to do these commits on branch_a.
我需要在branch_a上执行这些提交。 How can I cancel the commits on master and move them to branch_a.
如何取消master上的提交并将其移到branch_a。
Assuming no one else has already seen your changes to master and built something on top of them: 假设没有其他人看到您对master所做的更改,并在此基础上构建了一些东西:
git checkout branch_a
git cherry-pick <commit sha> # for each commit you want on branch a
git checkout master
git reset <commit sha you want to move master back to>
If someone has already built stuff on top of your accidental master commits, then you'll want to git revert
them instead of using reset
. 如果有人已经建立在你的主意外的提交顶部的东西,那么你要
git revert
他们,而不是使用reset
。
If you just have a couple commits, then cherry picking them might be the easiest option. 如果您只有几次提交,那么樱桃选择它们可能是最简单的选择。 You can find the hashes of the commits via
git log master
. 您可以通过
git log master
找到提交的哈希值。 Then do the following. 然后执行以下操作。
git checkout branch_a
git cherry-pick COMMIT_HASH_1
git cherry-pick COMMIT_HASH_2
...
git checkout master
git reset --hard COMMIT_HASH_BEFORE
Where all COMMIT_HASH_#
are the commits you wish to move to branch_a
and COMMIT_HASH_BEFORE
is the commit hash from before your erroneous commits. 所有
COMMIT_HASH_#
是提交要移动到branch_a
和COMMIT_HASH_BEFORE
是从你的错误提交之前提交哈希值。 (Note that this will throw away any uncommitted changes in your working directory.) (请注意,这将丢弃工作目录中所有未提交的更改。)
Note that if you've already pushed the changes to master
and other people might have worked with the branch, you'll instead want to git revert
the same commits you cherry picked, otherwise you'll cause problems for others working on the branch. 请注意,如果您已经将更改推送到
master
并且其他人可能已经在分支中使用过,那么您将想git revert
原来选择的提交,否则会给在该分支上工作的其他人带来问题。 (Revert simply creates a new commit which undoes the previous commit.) (还原只是创建一个新的提交,它会撤消之前的提交。)
Best thing imo is to reset the master branch but keep your changes. imo最好的办法是重置master分支,但保留您的更改。 Using cherry-pick will taint the repo with new commits.
使用cherry-pick将使用新提交污染回购。 Assuming you have made two commits:
假设您进行了两次提交:
git checkout master
git reset --soft HEAD~2
git checkout branch_a
git commit -m "foo"
Resetting with --soft means your changes will be kept staged and it will put git in the state as it looked before you made the commits, so you only need to switch branch and re-commit your changes. 使用--soft重置意味着您的更改将被保留,并且会将git置于提交提交之前的状态,因此您只需要切换分支并重新提交更改即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.