user1283776
user1283776

Reputation: 21764

Develop features in different branches, but work with the accumulated code change?

I have understood that it is best to develop each feature and bug fix in a separate branch, to make it easier for the repository owner to evaluate pull requests.

My workflow for this is that I

This works OK. But what if I when I am working on my second, third or fourth feature, want to work with the code base that has the accumulated changes from previous branches? But I don't want to include those changes when I commit this branch.

It might be that the first branch fixed something that had broken. When I am working on the second branch, I need to be working with the accumulated code base from the previous branch, but when I commit the second branch, I just want to commit the changes that I am working on now in my second branch.

Does this type of workflow make sense?
How can I use git to work like this?

Upvotes: 3

Views: 94

Answers (1)

CodeWizard
CodeWizard

Reputation: 142164

The are few ways to do it:

  • Patches

    Patches are the diffs generated per commit.
    In other words: patch will include the changes in each commit as separate diff file.
    Once the patches are generated you can apply them to any given branch of your wish.

    git format-patch HEAD~X # generate the last X commits as patch
    

now you will have separate file with the commit code and you can change branch and apply the patch

    git checkout <new branch>
    git apply *.patch
    #or:
    git am *.patch
  • cherry-pick

    cherry-pick is simply picking any desired commit to any branch.
    Due to the way git uses commits it does not attach them you can simply pick them to any given branch.
    You can use many commit per cherry-pick.

    git checkout <branch name>
    git cherry-pick commit1 commit2 commit3 ...
    

This will "import" the desired commits into your current branch.

Upvotes: 1

Related Questions