最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

git - Creating a release picking a few commits from the Main branch - Stack Overflow

programmeradmin4浏览0评论

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
  • 3 It's unclear what goes wrong when cherry-picking, it should be the correct approach. – mkrieger1 Commented Mar 31 at 13:30
  • Please edit and show your command(s), their text output and explain what did you expect and why you are not satisfied. – phd Commented Mar 31 at 13:56
  • 1 edited and added git commands. – Dutt Commented Mar 31 at 15:29
  • 1 @Dutt And what did git cherry-pick b pick into test_branch? It must be only one commit, b. – phd Commented Mar 31 at 15:55
  • 2 git cherry-pick adds commits to a branch, so if you start with git checkout main, then naturally your branch will include everything from main 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't main. – IMSoP Commented Mar 31 at 16:20
 |  Show 2 more comments

1 Answer 1

Reset to default 2

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>
发布评论

评论列表(0)

  1. 暂无评论