Christoph Wintersteiger
Christoph Wintersteiger

Reputation: 8392

What does git rebase/squash touch?

I've just had an embarrassing incident because git touched commits that I did not intend or expect to touch. What I did was to run git rebase -i HEAD~10 and then change the last 4 commits from pick to s to squash them into one. That part worked fine, but git also changed (at least) the author of the 8 previous commits, which were then given as "original author and me" instead of just "original author".

So, my question is, does git rebase -i HEAD~10 touch all of the 10 last commits or just the ones that I change/edit in the interactive prompt in the text editor? (Bonus points if you can explain why it changed 8 of them, instead of the 6 ones before my 4.)

Upvotes: 0

Views: 76

Answers (1)

matt
matt

Reputation: 535890

git touched commits that I did not intend or expect to touch

Simple solution: change your expectations. Do that by understanding git:

Fundamental fact about git: commits cannot be changed.

Rebase / cherry-pick makes all new commits. The resulting effective changes in the new commit are the same as that of the old commit, but this is a new commit with a new hash and a new back-pointer — and a new author.

So every commit in the interactive rebase for which you said pick generates a new commit, and those are the commits that you are now seeing. The old commits are still in the repo, unchanged, and you can use their SHAs to see them. But the commits in the rebase result are new-and-different commits.

During the interactive rebase you were given the opportunity, if you wanted it, to override the author setting of each new commit as it was formed. But you didn't take that opportunity.


Footnote: If you reach in at the lowest level, you can do anything you want, manually, to a commit. You can see a commit as text, and alter that text. So it is perfectly possible to change the author of these new commits after the fact. Nevertheless, you are still not actually altering a commit, even though it seems that way; you are making a new commit that substitutes functionally for the old one, which remains.

Upvotes: 3

Related Questions