3

I forked a git repo, and then created a branch called "strlen."

After submitting a PR and making suggested changes: .

The following was my attempt to merge in upstream changes:

Part A: fetch and merge from upstream:

git fetch upstream
git merge upstream/master

Part B: squash the upstream commits, retaining only a single commit comment for my particular changes:

git rebase --interactive HEAD~2

Note: for the interactive part, I did a s(quash) on all of the commits except for the latest one (mine) at the top.

Part C: push the changes back to my fork on github:

 git push origin strlen

The "squash" did not seem to do as advertised this time: all of the intervening commits **as well as the intervening file changes ** are visible on the strlen branch. The new Pull Request shows everything, whereas the intention was to display only my file changes.

So , any hints on

  • (a) should I have performed the rebase somewhat differently
  • or (b) take another approach altogether

UPDATE From VonC's great answer, I added some more details and here is the current solution:

git remote add upstream https://github.com/foo/bar.git
git checkout master
git fetch upstream
git checkout strlen
# Make changes. If using IJ you can then create CL and commit the CL locally.
#   Alternatively simply use command line and do commit -m "Made some updates"
git rebase -i upstream/master  # Squash the newer commits into 
                               # the original one used to create the PR
git push -f origin

The above workflow worked really well. Well enough in fact to fix the problems (merge errors) caused by my original workflow.

StephenBoesch
  • 46,509
  • 64
  • 237
  • 432

1 Answers1

2

You don't merge upstream/master when updating a PR branch.

You rebase your local PR branch on top of the updated upstream/master:

git fetch upstream
git checkout strlen
git rebase -i upstream/master # you can squash if you want there
git push -f origin

You force the push, but your PR will be updated automatically, taking into account the new history of the strlen branch.

See more at:

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283