Quick Notes

Things that came on the way

Making a Git Pull Request for Specific Commits

Time to time when working on a project using a fork from a git repository, situations arise that a pull request need to be made to the master repository for a sub set of commits you made to the fork. The following are a set of steps which can help with accomplishing these scenarios.

  • On the git repository in your local machine created from your fork, create a new branch
1
git branch cherry-branch origin/master
  • The assumption is that there is only one branch in your fork and the master repository and origin is pointing to your github fork.

  • Switch to the new branch created

1
git checkout cherry-branch
  • You can verify that you are on the correct branch by running the command
1
git branch
  • Identify ids of the commits you want to include into the pull request from your github fork.

  • Issue a git cherry-pick command to include the commits into the new branch

1
git cherry-pick COMMIT-ID
  • Push the new branch to your fork
1
git push origin cherry-branch
  • On github you can now see the new branch in your fork with only the commits selected. Now a pull request to the master repository.

What if there are multiple branches on the master repository and your fork and you want to send a pull request from a particular branch of your fork to a branch in the master repository.

  • Create a clone of the master repository branch you are interested in
1
git clone https://github.com/master-owner/project-repo -b interested-branch
  • Create a remote reference to the local repository of the your fork where all the commits are
1
git remote add -t local-branch-name mylocalrepo /location/of/local/gitrepo
  • Do a fetch of all the changes available in the local repository from your fork
1
git fetch mylocalrepo
  • Identify ids of all the commits you want to include into the pull request from your github fork.

  • Issue a git cherry-pick command to include the commits you want to get pulled

1
git cherry-pick COMMIT-ID
  • Create a branch
1
git branch cherry-branch
  • Switch to the new branch
1
git checkout cherry-branch
  • Push the changes into your fork in github
1
git push https://github.com/yourid/project-repo cherry-branch
  • Now the branch “cherry-branch” will be show up in github UI with the changes selected. A new pull request can be created for the changes to be committed into the master repository.

  • Once the changes are merged, the branch can be deleted and the local repository created can be removed.

1
git push origin :cherry-branch

Comments