简体   繁体   English

如何取消母版上的提交并将其移到分支

[英]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_aCOMMIT_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.

相关问题 如何安全地撤消已在远程主分支中的提交并将它们移至另一个分支? - How to safely undo commits that already in remote master branch & move them into another branch? 如何将 Git 提交移动到另一个分支并在原始分支中删除它们? - How to move Git commits to another branch and delete them in the original branch? Git-如何将母版上的最后X个提交移动到功能分支? - Git - how to move last X commits on master to a feature branch? 如何将一组提交从主服务器移动到单独的分支? - How can I move a set of commits from master to a separate branch? 如何将提交移动到另一个分支? - How to move commits to another branch? 如何将一个分支从分支的头提交到新分支中,而合并后又不丢弃它们呢? - How does one Move Commits from the Head of a Branch into a New Branch and not have them be Discarded when Merged back in? 使用git,如何在提交提交之前将master分支移回状态? - With git, How do I move the master branch back to the state before I made commits? 如何在git中将我的十个提交从master转移到一个新的分支? - How can I move my ten commits from master to a new branch in git? 如何允许某些git提交到分支但阻止它们合并到master中? - How do I allow certain git commits to a branch but prevent them being merged into master? 使用git将提交从master移到分支上 - Move commits from master onto a branch using git
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM