简体   繁体   中英

Revert commits made by merging of a certain branch in master

I have a branch called tempMaster and my team is continuously merging their feature branch changed on this tempmaster branch and then merging this tempmaster into master.But i found that a certain user has merged his feature branch accidentally in the tempmaster and then he also merged this tempmaster with master. There are multiple merge made by the user in tempmaster and then tempmaster into master. I need to revert al the changes which has been made by merging his feature branch into tempmaster and then tempmaster into master.

I can see the history by following command

git log --author=imran.zafar@bharatconnect.com --pretty=one --since=30.days

But this only show me the commit made by the user in past one month but i am not sure when he merged his feature branch for the first time.Is there any git command by which i can revert all the commit which has been generated by the merging this feature branch into master?

To revert the merged commits on master branch by the user, you can use below steps:

1. Find which commits are merged into master branch by the user

Use any of the belwo commands to show the commit history as a graph, and then find the merged commits on master branch by the user.

gitk --all
git log --oneline --decorate --graph --all

2. Revert the found commits from new to old on master branch

Assume the commit history as below garph. The commits F2 and F4 on feature branch were committed by the user, the commits T3 and T4 on tempmaster branch were the merged commits that the user merge feature branch into tempmaster , the commits M2 and M3 on master branch were merged by the user.

       …---M1---M2------…-----M3---…    master
               /             /
…---T1---T2---T3-----…------T4 ---…     tempmaster
        /    /             /
…-----F1---F2---…---F3---F4---…         feature   

So you should revert the commits M2 and M3 on master branch.

First revert the newer commit M3 by git revert -m 1 <commit id for M3> . The -m 1 option means keep the changes from the first parent and revert the changes from second parent (commit T4 ) on master branch. Then the commit history will be:

       …---M1---M2------…-----M3---…---M3'    master
               /             /
…---T1---T2---T3-----…------T4 ---…          tempmaster
        /    /             /
…-----F1---F2---…---F3---F4---…              feature   

Now the changes introduced by T4 has been reverted as commit M3' on master branch.

Then revert the older merged commit M2 on master branch by git revert -m 1 <commit id for M2> . And the commit history will be:

       …---M1---M2------…-----M3---…---M3'---M2'    master
               /             /
…---T1---T2---T3-----…------T4 ---…          tempmaster
        /    /             /
…-----F1---F2---…---F3---F4---…              feature   

And the changes introduced by T3 has been reverted by the commit M2' on master branch.

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