Search code examples
gitgit-squash

How fix my git branches after squashing is master behind dev


I have created 2 commits to remote dev branch. That I squashed and merge into remote master branch.

Now when I want to continue with working on dev branch - I don't know how can I correctly "repair" my branches - because after creating new pull request from dev to master I get list of all commits which were squashed in previous pull request.

I have something like this:

O ---- A ---- B ---- XY <--(master)
 \
  X ---- Y ---- Z <--(development)

How can I create correct pull request with commit Z from dev to master?


Solution

  • Before adding new commits to your dev branch, you should first reset it to origin/master, since you squashed/merge dev to it.

    In order to not break anything, create a new branch from origin/master:

    cd /local/repo
    git fetch
    git checkout -b newBranch origin/master
    

    Then report back your new commits on that new branch:

    git cherry-pick Y..Z
    

    Finally, reset your dev branch to said new branch:

    git checkout dev
    git reset --hard newBranch
    

    And force push: git push --force.

    The end result would be a new PR with only the new commits