3

I was working on a feature branch for quite some time. Now, I created a pull request to merge my branch but because my branch is very old, so there are many tests that are failing.

I want to move the starting point of my branch to a new commit without causing any changes to my existing PR.

I tried this but its not what I want-> I created a new branch and cherry-picked my commits to it. It works but doing this, I will have to create a new PR & there is already some comments on my PR. So, I dont want to lose them.

What I have is something like this:

master
A--B--C--D--...--M--...--Y--Z--
       \                   /
        \                 /
feature  nA--nB--nC--nD--nE

What I want to do:

master
A--B-...--M--..............--Y--Z--
           \                   /
            \                 /
feature      nA--nB--nC--nD--nE

Basically move the starting point of feature branch from C to M.

I heard about rebase but I didnt quite understand how that works, is ituseful in this case? I am a beginner, so please explain/give commands in detail.

sharmaji
  • 53
  • 6

2 Answers2

2

I created a new branch and cherry-picked my commits to it. It works but doing this, I will have to create a new PR

Actually no: you can reset your PR branch to that new branch and force push it.
It should update the existing PR, while keeping the comments (which could refer to old obsolete files)

git switch -C pr_branch new_branch
git push -f origin pr_branch

A git rebase could also help in that it would replay your pr_branch commits on top of an updated remote branch, but, if you already did manually the work of cherry-picking the relevant commits in your new branch, that should be enough: reset your PR branch to it and force push.

You could experiment on a new PR branch done from an old commit, with a comment: try and force push there to check if this works as advertise.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

I hope I understood your question.

We suppose to have the following commit hashes:

  • M => A0000000
  • nA => B000000

The instructions you should issue are:

  1. git checkout A000000 -B my-fixed-feature
  2. git rebase B000000
  3. git branch -D feature

In other words:

  1. Create a new branch starting at commit M.
  2. Move (rebase) the branch starting at nA after M inside the new branch "my-fixed-feature".
  3. Remove the old "feature" branch.

Obviously the step 2 may generates conflicts you should solve.

Unfortunately I did not try that solution, but I applied it several times during my working experience. I hope it may help you.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74