July 6th, 2021

What happens if I use a squash instead of a true merge when performing one of the git tricks?

A customer wanted to know which of the git tricks I’ve shared still work if you use a squash instead of a true merge. For example, in the case of splitting a file in two while preserving line history, can this be done with squashing?

Many of the git tricks exploit the way git walks the commit graph, and squashing creates a different graph from merging. Specifically, squashing throws away all our hard work in building a specific commit graph. Instead, squashing takes all the changes and squashes them together into a single commit. It’s as if you made all the changes at one sitting and committed them as a unit.

On the other hand, squashing works just fine with the tricks that involve a single non-merge commit. It just replaces one non-merge commit with an identical one.

Trick Squashable? Notes
Merging instead of cherry-picking No Merging is the point
Building a commit manually out of a tree Yes Creates a single non-merge commit
Building a merge commit manually out of a tree No Result is a merge commit
Building a throwaway commit in order to perform a combined cherry-pick-squash Yes Creates a single non-merge commit
Changing a squash to a merge No Result is a merge commit (that’s the whole point)
Squashing without git rebase Yes Creates a single non-merge commit
Resetting by reusing an earlier tree No Result is a commit that needs to be merged
Combining two files into one while preserving line history No Creates merge commits
Combining two files into one while preserving line history, via manual octopus merging No Creates merge commits
How do I split a file into two while preserving git line history? No Creates merge commits
How to split out pieces of a file while preserving git line history v1 No Creates merge commits
How to split out pieces of a file while preserving git line history v2 No Creates merge commits
How to duplicate a file while preserving git line history No Creates merge commits
Converting a rebase to a merge No Creates merge commits
How can I bulk-revert an entire repo to an earlier commit? It depends Method 1 uses a single non-merge commit, but method 2 uses a merge
How can I bulk-revert a subdirectory of a repo to an earlier commit? Yes Creates a single non-merge commit
Topics
Other

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.