Search code examples
gitmergegit-merge

How to make a new git branch but exclude the changes from one PR


In my project there are two branches named release-5.0 and integration-5.0 Our workflow is the merge our feature branches into integration and do internal QA and once that's done we put in PRs for release and the client has their own review/QA. One of the PRs that made it into integration was not approved and we decided to back burner the changes. I now need to make a new branch that is the same as integration-5.0 but with a new name and exclude just the changes from that one PR (there were PRs that were merged before and after the PR I want to exclude. What's the easiest way to do this?


Solution

  • Figured it out using rebase, I had actually forgotten there were two PRs I needed to exclude, one had QA revisions for the same feature. Here is how it's done:

    Make sure you have the latest changes by pulling the integration-5.0 branch:

    git checkout integration-5.0
    git pull origin integration-5.0
    

    Create a new branch based on integration-5.0:

    git checkout -b integration-5.0-2
    

    Run git log and note the commit hash for the earliest commit from your feature branch.

    Initiate an interactive rebase starting from that commit:

    git rebase -i commit-hash^
    

    Replace commit-hash with the commit hash previously mentioned. (The ^ symbol is part of the command, leave that there.)

    An interactive rebase file will open, showing a list of commits. Locate the lines corresponding to the commits you want to exclude and delete them or change the commands from pick to drop.

    Save and close the file to continue the rebase process.

    Git will automatically apply the changes, excluding the commits you dropped. If there are any conflicts, resolve them manually.

    Once the rebase is complete, your new branch new-branch-name will have all the changes from integration-5.0 except for the changes in the excluded pull requests.

    Now you can push your new branch to the remote repository:

    git push origin integration-5.0-2
    

    Your new branch is ready with the desired changes excluded.