简体   繁体   English

GIT:将更改从一个分支移动到另一个分支

[英]GIT: Move changes from one branch to other

I have 2 branches A and B. I made some changes to A and committed them.我有 2 个分支 A 和 B。我对 A 进行了一些更改并提交了它们。 I then made more changes to A (by mistake).然后我对 A 进行了更多更改(错误地)。 I pushed them to B and committed them.我把它们推到 B 并提交了它们。

But now I see changes from A (old) and B (new) being committed.但现在我看到 A(旧)和 B(新)的变化正在提交。 How do I revert this?我如何恢复这个?

If you have pushed commits, that means the remote branch is impacted (not just your local branch)如果您推送了提交,则意味着远程分支受到影响(不仅仅是您的本地分支)

You will need to cherry-pick the commit from B to A (assuming only one commit was done on B by mistake):您将需要挑选从 B 到 A 的提交(假设仅错误地在 B 上进行了一次提交):

git switch A
git cherry-pick B
git switch B
git reset --hard B~
git push --force

That would override the B history, which can be problematic if several collaborators are working from the remote repo.这将覆盖 B 历史记录,如果多个协作者正在远程存储库中工作,这可能会出现问题。
Another option is to revert B HEAD, to add an additional commit which cancels the content of the last one.另一种选择是恢复 B HEAD,添加一个额外的提交来取消最后一个的内容。

git switch B
git revert @
git push

No --force needed there.那里不需要--force。

git checkout <branchname>
git reset --hard <commitid>

This restores everything to it's original state.这会将所有内容恢复到原始状态。 If you meant something else, please elaborate.如果你的意思是别的,请详细说明。

Regards, Zenima问候,泽尼玛

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

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