Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "pull request" a specific commit

Tags:

git

github

I've got a specific commit which I would like to contribute to a repository I have forked on github. I assume the mechanism to do so is a "pull request". However when I try this I can only pull request my whole branch. I do not wish to pull request the other commits as they are not relevant. Any idea how I can do this.

repo I wish to pull request to.

The last commit b50b2e7 is the only commit I wish to pull request. Anyway I can do this or are all commits dependent on each other?

commit I wish to pull request

like image 324
pontikos Avatar asked Dec 01 '15 18:12

pontikos


People also ask

How do I refer to a specific commit in git?

If you want to go to a particular commit of a git repository with submodules you can use 2 git commands: reset or checkout. You will also need to synchronise the submodules after the working directory has been altered as that doesn't happen automatically.

How do I create a pull request for a specific commit in Azure DevOps?

From the Pull Requests view, select New Pull Request. Select the source and target branches, enter a title and optional description, and select Create. After the PR is created, select Open in browser to open the new PR in the Azure DevOps web portal.


2 Answers

Create a new branch with just that change:

# If you haven't set up your remote yet, run this line: # git remote add upstream https://github.com/konradjk/exac_browser.git git fetch --all                                   # Get the latest code git checkout -b my-single-change upstream/master  # Create new branch based on upstream/master git cherry-pick b50b2e7                           # Cherry pick the commit you want git push -u origin my-single-change               # Push your changes to the remote branch 

Then create the PR from that branch.

like image 56
Joseph Silber Avatar answered Sep 22 '22 17:09

Joseph Silber


I had the same error of alwaysCurious, so I did a little digging. 1

The regular case

A - B - C [master]          \           D - E - F - G [feature]  

You're working on a project, you use a separate branch (feature) for your committed changes (D-E-F-G) and you want to create a pull request. However you want only some of the commits to be included in the pull request (E and F)

The procedure here is the one from Joseph's answer

# optional: set upstream as remote if it's not git remote add upstream https://github.com/<upstream_github_username>/<upstream_github_repo_name>.git # fetch changes git fetch --all # create specific branch for your partial pull request git checkout -b partial-change upstream/master 

Now this is how it looks:

          [partial-change] A - B - C [master]          \           D - E - F - G [feature] 

Cherry-pick your specific commits and push the changes:

git cherry-pick <hash of commit E> git cherry-pick <hash of commit F> git push -u origin partial-change 

After fixing any conflict this is where you'll get:

          E1 - F1 [partial-change]          /  A - B - C [master]          \           D - E - F - G [feature] 

The consecutive case

If instead you just want to apply all the consecutive commits up to the last one (or two or three) you can just branch out at the specific commit. For instance here I just want the commits up to E and not the subsequent ones:

git checkout -b partial-consecutive-changes <hash of commit E> git push -u origin partial-consecutive-changes  A - B - C [master]          \           D - E [partial-consecutive-changes]                \                 F - G [feature] 

The rookie mistake

The last procedure can also help you if you just applied consecutive changes to master without using a specific branch for them and now you want to cherry-pick them after. This is relevant if you've forked a project at C and proceeded on master with the other commits. Here I am adding an asterisk to signal that new changes are happening on the fork:

A - B - C - D* - E* - F* - G* [master] 

What you shouldn't do is:

git checkout -b partial-change upstream/master git cherry-pick <hash of commit D> git cherry-pick <hash of commit E> git push -u origin partial-change 

In this case you're trying to branch out the master at G* and cherry picking the previous commits will get you the warning:

The previous cherry-pick is now empty, possibly due to conflict resolution.

since you're adding the same old commits on the new branch.

What you should do instead is:

git checkout -b partial-change <hash of commit E> git push -u origin partial-change  A - B - C - D* - E* - F* - G* [master]                   \               D* - E* [partial-change]                 

After this you're ready to make a pull request with only the selected commits.


Notes:

  1. Here I'm extending this great answer from Schwern.

  2. To get the last n commit hashes it may be useful to use: git log --pretty=oneline --abbrev-commit | head -n

like image 34
gibbone Avatar answered Sep 22 '22 17:09

gibbone