Search code examples
gitbranchgit-branchbranching-and-mergingrevert

Revert one git branch to reflect another


Say I have a master branch and a defect branch created off master. The defect branch was adding changes on top of master but also the master branch was being merged to the defect branch in between those changes. When I do git diff master HEAD I see specific changes made on top of master. However, because these changes are scattered across various commits interleaved with merges from master, there is no single commit to which I could revert. I need to create a new commit that will undo all differences between master and the defect branch so that I can merge the defect branch into another (release) branch to undo changes made by that defect branch. In theory I could copy all files from master to the defect branch and commit as a new commit, as that effectively would undo those changes. But how can I achieve that with git?


Solution

  • So if I understand you correctly, you want to change defect so that its contents match master. If that is the case, do:

    git checkout master -b temp 
    git reset --soft defect 
    git commit -m "Updating defect to reflect master"
    git checkout defect
    git merge temp
    git branch -d temp