Guiste
Guiste

Reputation: 469

How to revert last commits on branch, merge with other branch and re-add the commits?

I have the following problem. A git repo was forked a while ago. Starting from this fork, changes were made, but at the same time the original repo was also modified a lot more even, somehow like this:

A-B-C-D-E master
   \[fork]
    C'-D'-E'-F'-G'-H'-I'-J'----X' remote/master

It is important to note that the fork basically decoupled master and remote/master. The problem now is, I can create a local branch from remote/master and merge it directly with the local master branch. However, this will bring up a lot of merge conflicts which I would like to avoid, because most of these conflicts are related to changes in the main software package before B. So what I eventually would need to do is to take the last few commits of master since the fork and put them on top of the remote/master branch. Is that possible?

Upvotes: 1

Views: 46

Answers (1)

Guiste
Guiste

Reputation: 469

If anyone comes to this question, I solved the issue by the following procedure:

  • Created a new branch containing only all commits A-E and remove the latest commits C-E from the history of master:

    git checkout master
    git branch new_branch
    git reset --hard HEAD~5
    
        C-D-E new_branch
       /
    A-B       master
       \[fork]
        C'-D'-E'-F'-G'-H'-I'-J'----X' remote/master
    
  • Merged the master branch with remote/master accepting all changes from remote/master:

    git checkout remote/master
    git merge -Xours master
    
        C-D-E new_branch
       /
    A-B---------------------------      
       \[fork]                    \
        C'-D'-E'-F'-G'-H'-I'-J'----X' remote/master, master
    
  • Checked-out master and performed a rebase with remote/master

    git checkout master
    git rebase remote/master
    
    A-B---------------------------      
       \[fork]                    \
        C'-D'-E'-F'-G'-H'-I'-J'----X' remote/master, master
                                    \
                                     C-D-E new_branch
    

Upvotes: 2

Related Questions