简体   繁体   中英

GIT: Move changes from one branch to other

I have 2 branches A and B. I made some changes to A and committed them. I then made more changes to A (by mistake). I pushed them to B and committed them.

But now I see changes from A (old) and B (new) being committed. 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):

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.
Another option is to revert B HEAD, to add an additional commit which cancels the content of the last one.

git switch B
git revert @
git push

No --force needed there.

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

This restores everything to it's original state. If you meant something else, please elaborate.

Regards, Zenima

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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