Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase from one branch to another

I have the following case :

                          K---L new-feature
                         /
                H---I---J dev-team1
               /
              E---F---G dev-main
             /
A---B---C---D master

And I want to move only the new-feature (K---L) branch in dev-main branch without (H---I---J) form dev-team1

                H---I---J dev-team1
               /
              E---F---G---K---L dev-main
             /
A---B---C---D master
like image 895
radu c Avatar asked Jan 15 '14 21:01

radu c


People also ask

What does it mean to rebase onto another branch?

Git's rebase command reapplies your changes onto another branch. As opposed to merging, which pulls the differences from the other branch into yours, rebasing switches your branch's base to the other branch's position and walks through your commits one by one to apply them again.

Can I rebase a branch git?

By default, the git pull command performs a merge, but you can force it to integrate the remote branch with a rebase by passing it the --rebase option.

How do I rebase master to new branch?

To rebase, make sure you have all the commits you want in the rebase in your master branch. Check out the branch you want to rebase and type git rebase master (where master is the branch you want to rebase on).


2 Answers

git rebase has an --onto argument that does what you need.

git checkout new-feature
git rebase --onto dev-main dev-team1

# Now new-feature has commits K' and L' after G in dev-main.
git checkout dev-main
git merge --ff-only new-feature

See the man page for "git rebase" for more details. (I also like to add a -i just to double check that I'm moving the commits that I think I am.)

You could also use git cherry-pick, especially if the number of commits is small:

git checkout dev-main
git cherry-pick K L
like image 83
Ash Wilson Avatar answered Sep 20 '22 00:09

Ash Wilson


You can cherry-pick K and L commits.

git checkout dev-main
git cherry-pick K

If you have conflicts, fix the them, and do

git cherry-pick --continue
git cherry-pick L

Fix the conflicts.

You can also use interactive rebase.

git rebase -i head~5

In the opened editor replace H I and J commits lines with

pick F
pick G
like image 21
Erik Ghonyan Avatar answered Sep 22 '22 00:09

Erik Ghonyan