2

tl;dr

  • I've forked a repo and made many commits on many files, all on the master branch.
  • Now I want to create a pull request for all commits on select files, to send back to the original repo's author. For example, I want to send back some improvements, but not files I've added.
  • I don't care if all the commits get squashed into a whole new one.

What I've tried...

I used Perforce most of my career and am still getting used to git, so I think I'm either not realizing that my scenario doesn't match the scenarios in the following posts, or I'm missing some implicit steps that are obvious to regular git users but not to me.

What I'm seeing...

It seemed whenever I cherry-picked, I ended up with a branch with no changes (which makes sense though, since they're already committed to master, right?—then how do I create this "topic branch" I see being mentioned?) And now I have run several commands I don't fully understand (e.g. git remote add ...) and also possibly interchanging the original author's repo versus my forked repo in them, so it's becoming hard to tell whether things aren't working because I've messed my environment up.

Cleaning Up

@Omer's answer worked great. I then did a

git reset --hard HEAD

to clean up. The original author merged the PR, and I sync'ed to it:

git remote add upstream git://github.com/<author>/<repo>.git
git pull upstream master
slackwing
  • 25,894
  • 12
  • 72
  • 124
  • For a specific select file, do you want all its changes or part of them that you have made? – ElpieKay Jul 31 '20 at 09:29
  • For my case it works to get all changes. (If I'm making changes to an original repo file at all, it's something worth sending back. My own customizations will always be in separate, new files.) – slackwing Jul 31 '20 at 09:35

1 Answers1

4

In your repository, use git log to figure out the commit hash of the latest commit that does not belong to you (the commit that was latest in master when you forked). From now on I'll refer to this hash as abcd.

Checkout and create a new branch on YOUR latest commit (NOT abcd), by doing:

git checkout -b andrew_pr

Now perform a mixed reset so the andrew_pr branch will branch off of abcd, but you'll still have all your previously committed changes as local changes:

# Remember to replace "abcd" with the commit hash from the beginning!
git reset --mixed abcd # <---- Replace abcd

Now that all your changes are as simple local changes, you can perform an interactive git add using:

git add -p

This will guide you with a prompt through each of your changes, and you can knock some changes off and keep some other changes. Of-course, you'll want to keep all the changes you wish to see in the PR.

Once you're done, commit

git commit -m "Andrew's changes"

And push

git push --set-upstream origin andrew_pr

The push logs should give you a convenient link to create a pull request. Follow that link and submit your PR.

Omer Tuchfeld
  • 2,466
  • 1
  • 11
  • 22
  • Hm. When I did this another time, the final step gave me a link to create a PR in my forked repo, instead of the original repo. Any idea what might have happened? – slackwing Aug 10 '20 at 16:07
  • Not sure, the link comes from your git host - be it Github, BitBucket, GitLab etc. It's a convenience function often implemented by those services. You can instead visit the original repo on the git host website and create a new PR from the UI. It should allow you to select branches from your forked repo as source branches – Omer Tuchfeld Aug 10 '20 at 17:09