18

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.

Mighty
  • 317
  • 1
  • 2
  • 11
  • Using GIT allows you to cherry-pick specific commit(s), by commit id, regardless of the branch. For instance: git cherry-pick – MaD Jul 08 '20 at 06:27
  • So after `git checkout dev`, `git cherry-pick `, `git cherry-pick `. How to create PR for those cherry-pick for master branch? – Mighty Jul 08 '20 at 06:30
  • 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 commits – MaD Jul 08 '20 at 06:31
  • So I will create new branch from dev, cherry-pick those commits and merge that branch or create pull request for master? – Mighty Jul 08 '20 at 06:32
  • 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 PR – MaD Jul 08 '20 at 06:33
  • Are you able to provide full list of steps? – Mighty Jul 08 '20 at 06:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217432/discussion-between-mighty-and-mad). – Mighty Jul 08 '20 at 06:38

2 Answers2

31

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>
MaD
  • 780
  • 7
  • 19
  • 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? – Mighty Jul 08 '20 at 08:25
  • 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. – MaD Jul 08 '20 at 10:09
  • If the answer solved your question. Please mark it as the correct answer – MaD Jul 09 '20 at 05:45
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
Ravi
  • 91
  • 1
  • 8