I have a trunk based branching workflow. 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.
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!
I have a trunk based branching workflow. 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.
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!
Share Improve this question edited Mar 31 at 15:32 Dutt asked Mar 31 at 13:25 DuttDutt 4033 silver badges13 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 2The 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>
git cherry-pick b
pick intotest_branch
? It must be only one commit,b
. – phd Commented Mar 31 at 15:55git cherry-pick
adds commits to a branch, so if you start withgit checkout main
, then naturally your branch will include everything frommain
plus what you cherry-pick. Are you trying to somehow delete commits from the history? The only way to do that in git is to create a new history copying the changes you want (which will get new commit hashes). But you need to tell git where that new history should start, which according to your diagram isn'tmain
. – IMSoP Commented Mar 31 at 16:20