Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge - to squash or not to squash?

I'm currently working on implementing guidelines for git usage in a fairly complex development environment, and while I think I have the basics set up pretty well, there is one question in particular I would like some input on if that's at all possible. This isn't so much a purely technical question, as in that it's more about which of the available options is most appropriate.

Basically, I'm leaning towards a system that closely mirrors the common "git flow" structure (http://nvie.com/posts/a-successful-git-branching-model/), with some exceptions to adapt it to our development environment. In short:

  • A project will have at least a 'develop' and 'master' branch
  • 'master' will contain the latest production ready code
  • 'develop' will contain the latest merged code
  • Anything else will be in either a 'feature/ticket{number}' or 'hotfix/ticket{number}' branch
  • Feature branches will be made from 'develop', hotfix branches from 'master'
  • The number in the branch name will always correspond with the ticket number in our own change/bug system
  • Features can be tested individually, but also in combination, by building the appropriate branch or merging it to 'develop' first

So far, it's really helped us streamline our development and prevent conflicts between projects. The one detail that's spawned some debate here is; should we merge feature/ticket branches back into their respective origins with the '--squash' option? I'm somewhat of a fan of this, and what I like about it:

  • The git log for develop remains clean and readable, all commits will simply state the original branch name (which tells us if it was a hotfix or a feature, and the ticket number)
  • Even after clearing out the original feature or hotfix branch, the merge can cause no confusion afterwards

Maybe these reasons won't turn out to be good enough, and maybe there's good reasons not to use '--squash' in this scenario. Any thoughts?

like image 510
mephisto Avatar asked Nov 18 '14 17:11

mephisto


People also ask

Is it better to squash and merge?

As a general rule, when merging a pull request from a feature branch with a messy commit history, you should squash your commits. There are exceptions, but in most cases, squashing results in a cleaner Git history that's easier for the team to read.

Should you squash and merge github?

You can use squash and merge to create a more streamlined Git history in your repository. Work-in-progress commits are helpful when working on a feature branch, but they aren't necessarily important to retain in the Git history.

When to do squash and merge?

Squash merging is a merge option that allows you to condense the Git history of topic branches when you complete a pull request. Instead of each commit on the topic branch being added to the history of the default branch, a squash merge adds all the file changes to a single new commit on the default branch.

Should I squash commits when merging to master?

3 Answers. Show activity on this post. In my opinion, and bear in mind, this is just an opinion, and you will likely get different answers, you should NOT squash the commits when merging into master from the develop branch. Doing so would lose a lot of the history of the changes that have been made.

What does Git merge --squash do?

It squashes the commits on main (line 20 and line 14) into one commit and does not have a commit path from main. git merge --squash alters commit history but produces cleaner history. It seems that all development only happens on feature. This strategy is often used when a pull request is merged — merge to main from feature.

Should I squash all the small commits in Git?

Don’t squash: the small commits are useful especially for later tracking down of bugs with git bisect, and anyway you don’t want to change the history much. Just use a merge commit ( git merge --no-ff) to keep the history organized. Tracking down bugs become harder not easier when there are a lot of noisy meaningless intermediate commits.

What happens if I squash a commit before merging?

(a) if you decide to squash before merging, then all of those individual commits from your feature branch will be combined into a single commit. The main commit history, therefore, will only show a single commit for this integration.

When to use merge to main from feature in Git?

This strategy is often used when a pull request is merged — merge to main from feature. Our example is in the opposite direction, although the concept is the same. In a way, it does not matter how many merge commits you have on the feature branch. When they’re merged into the main branch with squash, it shows all feature development as one commit.


2 Answers

[S]hould we merge feature/ticket branches back into their respective origins with the --squash option?

It all depends how fine-grained you want the history of your repo to be. Keep in mind that version control, via commit messages, is a form of code documentation. Squashing willy-nilly is certainly not good practice. It may be fine for a hotfix branch, but rarely is for a substantial feature branch.

As an analogy, imagine if you asked me for a lend of my Lost DVD boxset (i.e. you cloned my repo), and I just gave you the box, no DVDs, and told you

Here, just read the summary of the series on the backcover. That should tell you enough about the plot.

So, by all means, squash your commits when you want to get rid of intermediate steps that are needlessly detailed or not self-contained enough, but not to the point that it obscures the evolution of your repository.


(I discuss this matter further in this Twitter thread.)

like image 101
jub0bs Avatar answered Oct 08 '22 00:10

jub0bs


Don’t squash: the small commits are useful especially for later tracking down of bugs with git bisect, and anyway you don’t want to change the history much. Just use a merge commit (git merge --no-ff) to keep the history organized.

like image 23
Marnen Laibow-Koser Avatar answered Oct 08 '22 00:10

Marnen Laibow-Koser