6

I'm working on a free proyect over Github. I fork it, made some changes, did a PR and everything worked ok (PR was accepted).

Then I update my fork with upstream, did some other changes, and made another PR.

Then I realice that my PR included all the commits I pushed earlier, and every file I modify, even the ones I already sent.

I made the upstream to be up to date, but I failed somewhere, or my fork, never understood that I was up to date (I even made the push).

So to avoid that, I made a branch only with the file I changed. However, the PR still want to send all the commits I made to the repo. The files in this case, are ok, only modified files are on the PR

So, what is the correct way to work with a fork?

Did I missed a step after upstream and push? or should I delete my fork after every PR and start all over again?

  • You can't do a pull request on the original repo? – Robert Harvey Apr 12 '18 at 20:22
  • I forked it. So i did a sync using upstream according to git manual. – gbianchi Apr 12 '18 at 20:26
  • OK, but what if you tried to do a PR on the original repo instead of using a fork? – Robert Harvey Apr 12 '18 at 20:31
  • 3
    Failing that, yeah, I think you do have to use a fresh fork for each PR. Creating a feature branch off the main repo and PR'ing that seems like its a lot simpler, and avoids all of the problems you are having. See yangsu.github.io/pull-request-tutorial – Robert Harvey Apr 12 '18 at 20:36
  • 3
    Your problem can be solved without deleting your fork (e.g. by doing a git rebase, or creating a branch off the upstream master). These issues tend to happen when PRs aren't merged as a real merge, but are rebased or squashed. However, this is not the place for this kind of help. Ask on Stack Overflow if you need help with using Git. Ask on Open Source if you are interested customs and common practices of open source development. – amon Apr 12 '18 at 21:44
9

Here are the steps you need to do to get a clean, nice pull request. I generally suggest this workflow to people with less previous exposure to git. You could make a rebase on auth repo master, but this might lead to non-trivial merging, depending on the delta. Therefore:

  1. Do not delete your fork. That was bad advice in the comments.

  2. Fetch the latest master branch from the target repo to your local clone (on your computer, not your fork).

    git fetch --all

  3. Create a new branch based on that latest official master branch

    git checkout -b new-feature origin/master (assuming the authoritative repo is origin)

  4. Cherry-pick all the changes for your second pull request onto your new branch (ideally with git cherry-pick -n, so you can make a new nice topical commit).

    git cherry-pick -n 923c223 (assuming your new changes are in commit 923c223)

  5. Push your new clean branch to your fork

    git push myfork new-feature

  6. Make a pull request from your new clean branch on your fork to the official repo's master branch.

Yay - Now you have a pull request with only your new changes!

On a sidenote: A bit of advice for you.

Generally, you should look at your local clone for all the work. The fork is just a place where to put your finished or half-finished work so it doesn't vanish when your local computer dies, and to collaborate with others. Always start new changes based on the latest master of the official/authoritative repository. With more experience, you most likely want to rebase your changes daily on authoritative master to always keep only a small delta in order to facilitate merges and simplify your cognitive load when developing and bugfixing.

  • Seems like a lot of work when you could have just branched off a clone of the target repo. – Robert Harvey Apr 13 '18 at 15:27
  • 1
    @RobertHarvey Steps 4-6 are required even on a clone. Or how else would he get the new changes into git? And steps 2-3 are really just git fetch --all; git checkout origin/master. – Wilbert Apr 17 '18 at 5:59
  • I didn't know you could "cherry-pick" your changes. I would have simply cloned the master, applied the changes, and done the pull request. – Robert Harvey Apr 17 '18 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.