I forked a project on github and am successfully making changes to my local master and pushing to origin on github. I want to send a pull request, but only want to include t
Based on @kevin-hakanson's answer, I wrote this little bash script to make this process easier. It will add the upstream repo if it doesn't already exist (prompting you for the URL) then prompt for both the name of the new branch to create and the tag / SHA of the commit to cherry pick onto that branch. It checks what branch or commit you're currently on then stashes any changes so you can checkout the new branch. The merge strategy keeps the changes from the cherry-picked commit. After pushing the new branch to origin
(assumed to be the name of your remote repo), the branch or commit you were on before is checked out again and your previous changes popped from the stash.
if ! git remote | grep -q upstream; then
read -p "Upstream git repo URL: " upstream
git remote add upstream $upstream
git remote update
fi
read -p "Feature branch name: " feature_branch
# note: giving "master" is the same as giving the SHA it points to
read -p "SHA of commit to put on branch: " sha
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" == "HEAD" ]; then
# detached HEAD; just get the commit SHA
current_branch=$(git rev-parse --short HEAD)
fi
git stash
git checkout -b $feature_branch upstream/master
git cherry-pick --strategy=recursive -X theirs $sha
git push origin $feature_branch
git checkout $current_branch
git stash pop
(This has worked for me in a couple of simple tests, but I'm not a bash programmer or git expert, so let me know if there are cases I've missed that could be automated better!)
Create a new branch starting from the latest commit, which is also in the origin repository:
git branch new-branch origin/master
git checkout new-branch
Then use git cherry-pick
to get the single commit you want the pull request for. If the branch with this commit is called feature
and the commit you want is the latest commit in this branch, this will be
git cherry-pick feature
Assuming this patch applies without conflict, you got now a branch for which you can do your pull request.
In a second step, you now need to decide what to do with your feature
branch. If you haven't published your changes on this branch yet, the best procedure is probably rebasing this branch upon new-branch (and removing the last commit, if this is not done automatically by git rebase
).
I ended up in a situation where I had forked a fork and wanted to submit a pull request back to the original project.
I had:
To do this, I:
The git commands were something like:
Then I picked my-feature-request as the branch for my pull request to the original project.
The solution to create a new (temporary) branch, cherry-pick and creating the pull request for that branch did not satisfy me. I did not want to change my repository to make a set of commits available, so I came up with the following alternative:
First create patch files for all commits of interest:
git format-patch -1 <sha>
If the commit of interest happens to be the last one you can use HEAD
instead <sha>
.
Now, you can send the patches to the maintainer of the source repository, who can apply them:
git branch new-branch <master or some older commit where the fork diverged>
git checkout new-branch
git am < <the patch>
...
git checkout master
git merge new-branch
Finally this should look the same as if a temporary branch was merged by a pull request, but without having that additional branch in the fork-repository.
This almost worked for me:
git checkout -b upstream upstream/master
git cherry-pick <SHA hash of commit>
git push origin upstream
The only difference was this:
git push origin upstream:upstream
I needed to change that last line so that git push would make the upstream branch in my GitHub repo so that I could make PR from it.
I had already made the commit which I wanted to be able to isolate as a pull request back onto the current branch.
So I checked out a new branch
git checkout -b isolated-pull
And here's where my solution differs from @Kevin Hakanson's, as I need to reset this branch to the place in the history I want to diff from
git reset --hard [sha-to-diff-by]
And cherry-pick the commit from which I want to create an isolated pull request
git cherry-pick [my-isolated-commit-sha]
Finally push it up to the remote
git push origin isolated-pull
And pull request dat shi.