Search code examples
gitrebasegit-commit

Push changes in current branch into other branch


I am working on dev branch right now. Earlier, I created a featureX branch from dev branch and since then I've made 15 commits to dev branch. Now I want all those 15 commits in featureX branch, so that my featureX branch can also have all the work I have done in last week in dev branch.

I am super confused with rebase, rebase --onto, etc. commands. Should I be using one of these?


Solution

  • It sounds like there are three git commands that might be helpful: rebase, cherry-pick, and merge.

    rebase

    The easiest way to integrate the branches...is the merge command. It performs a three-way merge between the two latest branch snapshots...and the most recent common ancestor of the two..., creating a new snapshot (and commit).

    However, there is another way: you can take the patch of the change that was introduced...and reapply it on top. In Git, this is called rebasing. With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch.

    In your example, you could do the following:

    git checkout featureX
    git rebase dev
    # continue your development on the featureX branch
    
    # if there are conflicts, resolve them, and then continue with
    git rebase --skip
    
    # when done with changes, do:
    git checkout dev
    git merge featureX
    

    cherry-pick

    Apply the changes introduced by some existing commits.

    In your example, you could do the following:

    git checkout dev
    git cherry-pick ^HEAD dev
    

    merge

    Merge changes from one branch into another.

    In your example, you could do the following:

    git checkout featureX
    git merge dev
    

    All examples and excerpts are from the documentation:

    Further reading: