Search code examples
gitmergegit-mergerebasegit-rebase

Git Rebase already merged branch


I created feature branch from main develop branch called my-feature there is also another feature branch created by another developer another-feature.

We both are doing changes in different files

Another developer already create PR but, not merged yet to develop. But, I need his changes to my branch so, I did git merge another-feature to my my-feature branch and create Pull request.

But, main problem is now, I can see changes of another-feature branch to my Pull request of my-feature. So, how can I rebase that? I don't want to show changes from another branch.

I checked another post but, some of them are less understandable to me.

Any help would greatly appreciated.

Edit:

git log Display like following. (Target branch is develop)

commit: f0fdfddsdsde
Author: Me (As my last commit before creating PR)
Date: blah blah

commit message....


commit: a4dsdsdssdds
Author: Another developer (whose changes I merged)
Date: blah blah

commit message....

rest of another commits ....

git log ––all ––decorate ––oneline ––graph

f0acaed (HEAD -> feature/my-feature, origin/my-feature) commit message..
a4f4dab (origin/another-feature, feature/another-feature) commit message..
* db30503 (origin/HEAD, origin/develop, develop) merge pull request from another branch

blah blah blah

Solution

  • The changes will be visible on your PR unless the another-feature is merged.
    If you just want to not see those changes in your PR while reviewing, just change the base branch during the review (if the version control system you're using, allows that).

    Another solution (that I prefer) is to rebase your branch with another-feature instead of merging them.
    In this case you'll have less headache when the another feature is merged, and also while reviewing you'll be able to just review the commits that you've pushed (most of the systems allow you to select the set of commits while reviewing).

    Update: (for the second approach mentioned above)
    In order to jump to the original state of my-feature, you'l need to use reflog.
    Here's how to do that.

    1. Make sure there is no pending changes by typing git status
    2. Type git reflog and you'll see something like this: enter image description here There you can see the history of the head references. Just find the commit that was before the merge (in the picture it's the one with HEAD@{1} with commit hash e919ec6).
    3. Now just checkout to that commit with the command: git checkout HASH_HERE (e.g. git checkout e919ec6)
    4. Now just create a branch from that commit (for convenience) checkout -b my-feature.ORIGINAL

    Now the my-feature.ORIGINAL contains your branch state before merging and you're on that branch.
    In order to do rebase instead of merge, follow these steps:

    1. Do a backup of your branch (again just for convenience) git branch my-feature.BKP
    2. Rebase the branch with the another-feature git rebase another-feature
    3. Rename your old bad branch (for convenience) git rename my-feature my-feature.BAD
    4. Change your current branch name from my-feature.ORIGINAL to my-feature (to match the remote branch name) git rename my-feature.ORIGINAL my-feature
    5. Now you can force push your branch to your remote branch and the PR will be updated.