git Git推送到错误的分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6465699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git push to wrong branch
提问by Alessandro DS
Working with git, after some 'commit', and a couple of 'push', I realized that am using the wrong branch !
使用 git,经过一些“提交”和几次“推送”后,我意识到我使用了错误的分支!
Now I have to remove in some way my changes in wrong_branch and commit and push the changes in right_branch
现在我必须以某种方式删除我在 wrong_branch 中的更改并提交并推送 right_branch 中的更改
What's the best (and simple) way to do that ?
最好的(和简单的)方法是什么?
thank you
谢谢你
回答by Dhruva Sagar
switch to that branch, check the git log
and git revert
those commits individually. Once you have done that, switch back to the desired branch and there you can then use git cherry-pick
to pick specific commits from the git refs and merge it into the right branch.
切换到那个分支,分别检查git log
和git revert
那些提交。完成后,切换回所需的分支,然后您可以在那里git cherry-pick
从 git refs 中选择特定的提交并将其合并到正确的分支中。
git checkout wrong_branch
git revert commitsha1
git revert commitsha2
git checkout right_branch
git cherry-pick commitsha1
git cherry-pick commitsha2
If the commits are grouped together and there are no commits pushed after your dirty commits, you can even use git reset
to get that wrong branch to a state just before your commits and then follow that again using git cherry-pick
to get your commits into the right branch.
如果提交被分组在一起并且在你的脏提交之后没有推送提交,你甚至可以使用git reset
将错误的分支置于提交之前的状态,然后再次使用git cherry-pick
将提交放入正确的分支。
git checkout wrong_branch
git reset commitsha3 #commit just before commitsha2
git checkout right_branch
git cherry-pick commitsha1
git cherry-pick commitsha2
回答by Olivier Verdier
The simplest way is using git rebase
. Suppose that you have that setting:
最简单的方法是使用git rebase
. 假设你有这样的设置:
A -- B -- C -- C1 -- C2 # right branch
\
\-- D -- C3 -- C4 # wrong branch
You want to move change C3,C4 to the right branch.
您想将更改 C3,C4 移动到右侧分支。
git checkout -b new_wrong_branch D
git checkout wrong_branch
git rebase D --onto right_branch
git checkout right_branch
git merge right_branch wrong_branch
git branch -d wrong_branch
git branch rename new_wrong_branch wrong_branch
Now the setting is
现在设置是
A -- B -- C -- C1 -- C2 -- C3 -- C4 # right_branch
\
\ -- D # wrong_branch
Then you have to push your results with force (IF nobody has synchronized with your remote repo yet):
然后你必须用力推送你的结果(如果没有人与你的远程仓库同步):
git push -f remote:right_branch
回答by tarikakyol
A bit of shortcut adding to Dhruva's answer
添加到 Dhruva 的答案的一些捷径
git checkout wrong_branch
git revert commitsha1
git checkout right_branch
git push right_branch
git checkout wrong_branch
git reset commitsha2 #commit just before commitsha1
git push wrong_branch -f