Synchro
Synchro

Reputation: 37730

Merging upstream git changes with pending pull requests

I keep running into problems with git in a common workflow on GitHub.

  1. I fork a repo
  2. I commit some changes
  3. I make a pull request to upstream repo
  4. Twiddle thumbs while upstream developer sits on my pull request.
  5. Upstream developer makes changes to their repo
  6. They look at your pull request, but because of changes in their tree it will no longer apply cleanly, so they ask you to update your pull request.
  7. I fetch & merge changes from upstream into my repo, resolve conflicts, commit changes
  8. I rebase commits in my pull request to make it neat and tidy.

This is where the problem arises: my pull request now contains all the changes that occurred between steps 2 and 7, including the upstream developer's own changes. In a recent example this expanded a 10-line pull request to over 12,000 lines.

How should I reapply my original commits onto a later version of the upstream repo without their changes getting into my pull request?

Upvotes: 20

Views: 4787

Answers (2)

piedar
piedar

Reputation: 2721

Change this

7) I fetch & merge changes from upstream into my repo, resolve conflicts, commit changes.

8) I rebase commits in my pull request to make it neat and tidy.

to

I rebase my repo onto upstream, making it neat and tidy.

We'll assume you forked the feature branch from upstream/master, and we'll use a temporary branch to be safe. If things go wrong, just delete the feature-rebase branch and start over.

git checkout feature
git checkout -b feature-rebase
git rebase -i upstream/master

This will replay your commits on top of upstream/master, as if you had forked right now. Once everything looks good, replace the old feature branch with the rebased version.

git branch -m feature feature-old
git branch -m feature-rebase feature
git branch -d feature-old
git checkout feature
git push -f origin feature

Upvotes: 18

Ikke
Ikke

Reputation: 101231

I can't make up from your question if you are already doing this, but when you are making commits for a pull request, the easiest way is to make a separate branch for it.

That way, you have an easy way of defining what commits should go in the pull request, and can later also update the pull request to include new changes.

If you then want to incorporate new changes from upstream, you can fetch those, and rebase your topic branch on it.

When making a pull request in github, you can then select this branch, which makes sure the correct commits are pulled in.

Every time you then push to this branch (even push -f) will update the pull request automatically.

Upvotes: 2

Related Questions