Reputation: 131445
I've just merged a PR for my GitHub repository. But - I want to squash the different commits that have gone into that PR. I thought I would be able to do it by simply
rebase -i <hash-of-first-commit-in-the-PR>^
but that doesn't do what I expected - the commits I see in the list in the rebasing editor are not the ones I see in git log
; and, specifically, the commits I want to squash together aren't there.
Looking further back, here is a part of the commit graph:
* hash11 (HEAD -> development, origin/development) Lorem ipsum dolor sit amet
* hash10 Merge pull request #123 from someone/development
|\
| * hash09 Merge branch 'development' into development-fork
| |\
| |/
|/|
* | hash08 consectetur adipiscing elit
* | hash07 sed do eiusmod tempor incididunt
* | hash06 ut labore et dolore magna aliqua
* | hash05 Ut enim ad minim veniam
* | hash04 quis nostrud exercitation ullamco laboris nisi
* | hash03 Merge pull request #101 from someone/development
|\ \
| | * hash02 ut aliquip ex ea commodo consequat.
| |/
| * hash01 Duis aute irure dolor in reprehenderit in voluptate velit esse
|/
* hash00 cillum dolore eu fugiat nulla pariatur.
The PR merge is commit hash10. It brought in two commits by "someone", hash09 and hash02. The meat of what I care about is hash02. Looking at this graph, I realize that my situation is messier than I initially described. Perhaps I should even be trying to squash hash09 into hash02, even though on "someone"'s branch they were simple consecutive commits?
PS - Naturally I don't mind force-pushing into GitHub afterwards.
Upvotes: 1
Views: 1181
Reputation: 30166
let's assume that the PR's tip is called PRx... let's also assume that the revision of the other branch (master?) before merging is on pre-merge (another branch... or you can use the revision ID).
You can do it like this:
git checkout PRx
git reset --soft $( git merge-base HEAD pre-merge ) # set the branch pointer where I started working on my PR, all changes are on index ready to be committed
git commit -m "Squashed commits"
# PRx is now on this new squashed revision by the way
# and you don't want to do another merge effort, right? Maybe there were conflicts and so on... so
git branch new-merge $( git commit-tree -p PRx -p pre-merge -m "Merge" old-merge-revision^{tree} ) # old merge revision is the original merge revision you did when the branch was not squashed
# now we have a new merge revision on branch new-merge
# feel free to point another branch over here and do push -f
Upvotes: 2