Search code examples
gitgithubgitlabgit-commit

Apply range of commits from one branch to another


I got 2 branches

a -- b -- c -- d -- e -- f -- g -- h     <-- master

a -- b -- c                              <-- Branch1
            

i need to apply the commits e, f and g onto Branch1

Tried with:

git rebase --onto gSha1 eSha1 hSha1 after checkout on Branch1 and it didn't worked (as said here)

tried with git cherry-pick eSha1^..gSha1 but it dont worked too (As mentioned here)


Solution

  • If you want a -- b -- c -- e -- f -- g

    From

                               E---F---G  master
                              /            
                             D
                            /
                   A---B---C  Branch1
    
    git rebase --onto branch1 dSha1 gSha1
    

    will give you

                             E'--F'--G'  HEAD
                            /              
                           | D---E---F---G  master
                           |/
                   A---B---C  Branch1
    
    

    Next set Branch1 to HEAD:

    git branch -f Branch1 HEAD
    
                             D---E---F---G  master
                            /
                   A---B---C---E'--F'--G' Branch1
    
    

    Alternative

    Set Branch1 to master and rebase Branch1 on commit C without D:

    git co Branch1
    git reset --hard master
    git rebase --onto cSha1 dSha1 Branch1
    

    If you want a -- b -- c -- d -- e -- f -- g (First question asked)

    switch to Branch1

    git checkout Branch1
    

    then set Branch1 to g

    git reset --hard gSha1
    

    and here you are you can push or continue