gitgit-flow

Creating a release picking a few commits from the Main branch


I have a trunk based branching workflow. enter image description here What I want to do is to create a release branch where I can pick a few commits (Not all commits) from the main branch such that it should keep the version history till v1.0 release and add these few commits to create another release of v1.1. enter image description here

Is it possible and what are the git commands step by step?

I am testing the cherry picking

git checkout main
git checkout -b test_branch
git cherry-pick <commits>

but its picking up all commits from main when I am creating a new branch. Appreciate the help!


Solution

  • The problem is how you're creating the new branch:

    git checkout main
    git checkout -b test_branch
    

    That means you're creating a branch named test_branch and pointing it to the same commit main is pointing to. Note you don't even need to checkout main first, the checkout command takes a second argument with the commit you wish to point the new branch to. For example, your two lines above can be done in a single line, like this:

    git checkout -b test_branch main
    

    (That creates a new branch called test_branch and points it to main.)

    And now, to fix your problem, you simply need to change what commit test_branch points to:

    git checkout -b test_branch <commit-b>
    

    After creating your new branch from the desired starting commit, you can cherry-pick the commits you'd like to bring into the new branch:

    git cherry-pick <commit-d>
    

    Side Note: There's a newer way to do this using the switch command. The equivalent command to create the branch is:

    git switch -c test_branch <commit-b>