
Question:
Scenario: being on master branch
<ol><li>git checkout <strong><em>target_branch</em></strong>
</li> <li>git checkout <strong><em>new_branch</em></strong>
</li> <li>git commit -m: "TARGET BRANCH TASK NUMBER,NEW BRANCH TASK NUMBER: Message"
</li> <li>git push origin <strong><em>new_branch</em></strong>
</li> <li>git checkout <strong><em>target_branch</em></strong>
</li> <li>git push origin <strong><em>target_branch</em></strong>
</li> </ol><blockquote>merge request (target - <strong><em>target_branch</em></strong>)
</blockquote>As I understand my <strong><em>new_branch</em></strong> isn't created from <strong><em>target_branch</em></strong> because firstly I made push from <strong><em>new_branch</em></strong> and only then I pushed <strong><em>target_branch</em></strong> (just forgot it).
So help me please do properly the next:
I'm going to work in <strong><em>target_branch</em></strong> with my friend, and we will create our branches from it.
I need now make my <strong><em>new_branch</em></strong> to be created from <strong><em>target_branch</em></strong> and delete old <strong><em>new_branch</em></strong> (that, I suppose, now seems to be created from master not from target_branch).
How can I do this properly (for Git history)?
Answer1:To change new_branch
checked out from target_branch
(or master
you can use same method):
git checkout target_branch
git cherry-pick target_branch..new_branch
git branch -f new_branch
git checkout new_branch
<strong>Note:</strong>
-If there has cherry-pick conflicts, you can modify and save the conflict files, then use git add .
and git cherry-pick --continue
.
-If you don’t want to rebase new_branch
on the top of target_branch
, you can use git checkout <an history commit of target_branch>
, and then use above commands to rebase new_branch
. As below graphs, if you use git checkout B, and after above commands, the branches will look like:
# Original branch structure
A---B---C target_branch
D---E---F new_branch
# After rebase new_branch from commit B
A---B---C target_branch
\
D'---E'---F' new_branch
Answer2:To Resolve your issue, Go through below steps :
<ol><li>git checkout <strong><em>target_branch</em></strong></li> <li>git merge <strong><em>new_branch</em></strong></li> <li>git push origin <strong><em>target_branch</em></strong></li> </ol>You are now at <strong><em>target_branch</em></strong>, Let's Delete Old Branch : <strong><em>new_branch</em></strong>
<ol start="4"><li>git branch -D <strong><em>new_branch</em></strong></li> <li>git branch <strong><em>new_branch</em></strong></li> </ol>Now you & your friend can make new branches from <strong><em>target_branch</em></strong> & work on <strong><em>new_branch</em></strong>