Git: How to cherrypick commit from one branch and create pull request for another branch?
Asked Answered
K

2

23

I have two branches dev & master. I am using bitbucket.

I need to cherrypick some of the commit from dev branch.

Then need to create pull request for the master branch.

As my dev environment has so many things, some of them not able to directly merge to master.

So take commit <id 1>, <id 2> from dev branch. Take them together. Create pull request those to merge with master branch.

Kathrynkathryne answered 8/7, 2020 at 6:21 Comment(7)
Using GIT allows you to cherry-pick specific commit(s), by commit id, regardless of the branch. For instance: git cherry-pick <id 1>Committeewoman
So after git checkout dev, git cherry-pick <id 1>, git cherry-pick <id 2>. How to create PR for those cherry-pick for master branch?Kathrynkathryne
You can create a new branch on you master branch. Cherry pick the desired commits and then make a PR for this new branch. In your case it will contain your cherry-picked commitsCommitteewoman
So I will create new branch from dev, cherry-pick those commits and merge that branch or create pull request for master?Kathrynkathryne
No merge is involved. Cherry-pick will take the desired commits and place them on top of your master. You then can push those commits to the new branch and make a PRCommitteewoman
Are you able to provide full list of steps?Kathrynkathryne
Let us continue this discussion in chat.Kathrynkathryne
C
38

You can use

git cherry-pick <commit id>

to pick specific commits

To close the cycle and make a PR from master. We can do the following steps:

Assume being on the master branch:

git checkout -b myNewBranch // this will create a new branch named myNewBranch
git cherry-pick <commitID 1> // this will take the commit with the commit ID 1 and 
                             // attempt to place it on top of the master branch. 
                             // Note however, there might be conflicts to resolve
git cherry-pick <commitID 2> // this will take the commit with the commit ID 2 and place on top of the master branch
git push origin/<some branch name> // will push the changes to remote. Usually origin/<local branch name>

Then you can make a pull request depending on your platform. So it can be from the GUI. Be it on a GitHub platform or DevAzure, etc. In your case via BitBucket GUI.

Side note: the steps above are made for simplicity. It is also possible to make the cherry-pick with one line. Like so:

git cherry-pick <commitID 1> <commitID 2>
Committeewoman answered 8/7, 2020 at 6:44 Comment(3)
We need to pick commit from dev branch. So will create new branch from dev. Then cherry-pick commit from them. Then merge this new branch via PR to master. Correct?Kathrynkathryne
If you need to pick commits from dev to master. Then you make new branch on master. You cherry-pick the commits from dev. You then push the branch to remote and you use the Bitbucket GUI procedure to make the PR. As outlined in the answer. If further assistance is required (regarding merge). I believe you can find the answer here of StackOverflow or ask another question if no suitable answer is found.Committeewoman
If the answer solved your question. Please mark it as the correct answerCommitteewoman
B
6
Fork original-repo as "your-repo"

git clone your-repo
cd your-repo
git checkout dev
git pull //to get all your commits to local
git checkout master
git pull //to make sure branch is upto date
git cherry-pick commit-id
git push //commits the cherry-picked commits to the master branch of forked repo

Raise PR from Forked repo "your-repo" master branch to "original-repo" master branch
Bombshell answered 13/7, 2020 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.