简体   繁体   中英

git: remove single commit from the master branch

I accidentally put some changes (commit C) to master instead of branch. Later I created other branches basing on this changes ( branch1 and branch2 ). Now I'd like to remove changes introduced by commit C from all the branches (and move it to branch-C ). Is it possible?

Current situation:

A - B - C - D - - L - M - L - M master  
            |     \                     
            |       - N - O - P branch2                           
            \               
             - E - F - G        branch1 

Desidered situation:

         - C                    branch-C 
        /
    A - B - D - - L - M - L - M master  
            |     \                     
            |       - N - O - P branch2                           
            \               
             - E - F - G        branch1 

You have to rebase each branch, since every commit references the entire chain of parent commits via the hash.

You can do something like

for br in master branch1 branch2
do
  git rebase --onto B C "$br" 
done

git branch branch-C C

The loop as written will work seamlessly only if there are no conflicts. In general I'd recommend executing it by hand.

Also, if you're doing it by hand, you can optimize a bit. Eg

git rebase --onto B D master
git rebase --onto Dnew D branch1
git rebase --onto Lnew L branch2

where Dnew and Lnew are the rebased versions of D and L respectively.

Disclaimer: I haven't tested it; please test it on a copy of your repo first.

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