Luke
Luke

Reputation: 21236

GitHub Rebase & Merge creates new commit hashes

I want to selectively promote a number of commits from one branch to another with a PR review.

Imagine I have on my master branch the following commits:

8b08096 - mod4
97eff67 - mod3
b64891f - mod2
fa6e804 - mod1
956e388 - initial

On my staging branch I have only the following:

956e388 - initial

I now want to "promote" the commits for mod1, mod2 & mod3 from master to the staging branch. So, from staging I create a new temporary branch and merge the commits up until mod3:

git checkout staging
git checkout -b promote
git merge 97eff67

Doing a git log now shows me all the commits including mod3 on my promote branch:

git log --pretty=oneline

97eff67 (HEAD -> promote) mod3
b64891f mod2
fa6e804 mod1
956e388 (origin/staging, staging) initial

git push origin promote

I can now create my PR for the team to review the changes.

However, when it comes to merging this into the staging branch, GitHub gives me 3 options; Merge, Squash & Merge and Rebase & Merge. I don't want to do Merge since that gives me a merge commit. I don't want to do Squash & Merge since that will squash all my commits into a single commit so I'm left with Rebase & Merge.

When I do a Rebase & Merge, all the changes are applied to the staging branch. However, all the commits have now been given new hashes (except of course for the "initial" commit):

2d7177a - mod3
2831f46 - mod2
a8a2e15 - mod1
956e388 - initial

How can I merge the commits from my promote branch into my staging branch while keeping the commit hashes intact?

Upvotes: 13

Views: 4674

Answers (1)

torek
torek

Reputation: 487725

That's perfectly normal:1 rebase copies commits to new and (supposedly) improved commits, which means they will have different hash IDs.

How can I merge the commits from my promote branch into my staging branch while keeping the commit hashes intact?

Do not use REBASE AND MERGE.

GitHub do not provide a button that would do what I wish they allowed. This means that if you wish to merge a branch on GitHub, you must use the MERGE button.

To merge without a "merge bubble" (i.e., sans merge commit) as a fast-forward instead, you will have to do the fast-forward on your own machine, and then use git push to send the updated commit hash ID to GitHub. That is:

git fetch

(so that you have everything on your laptop or wherever—this step is often unnecessary, but never hurts), then:

git checkout staging
git merge --ff-only origin/staging   # if needed

git merge --ff-only origin/promote   # whatever fast forward you want here
git push origin staging

The checkout-and-fast-forward for staging is only needed if your own staging is behind origin/staging after the git fetch step. The git merge --ff-only commands achieve the fast-forward-instead-of-merge operation.


1Normal for GitHub, anyway. Some systems allow a fast-forward merge operation here, where this would not occur. GitHub is not one of these. REBASE AND MERGE does a forced rebase, making it "normal".

Upvotes: 12

Related Questions