Git Tip: merge –no-squash

| 4 Comments

Often, when I develop a feature, I push to a temporary repository on Github very frequently. As an example, for implementing Fluid caching, we did around 60 commits to the GitHub repository for implementing this feature.

However, when pushing it to the TYPO3 Review System, we only want a single commit instead of many. There are two ways  to archieve this, which I want to highlight here:

git rebase –interactive

If you have a quite linear history, you can do an interactive rebase, as described in this tutorial, and then squash all commits into a single one.

This works especially nice with a mostly-linear history.

The Problem: In-Between Merge Commits

While the above concept works nicely in many cases, in our example we had a non-linear history, as we merged updates from the official Fluid repository into our development branch.

Now, this meant I could not just use squash all my commits into one, but needed a different technique. When reading the man page of git merge, I found a solution:

git merge –no-squash

git checkout master
git merge --no-squash dev
# delete .git/MERGE_HEAD if it exists
git commit

The trick is to use git merge –no-squash, as it just updates the staging area but does not commit the merge yet. It thus gives you all changes; and by deleting the MERGE_HEAD file, git will commit all changes as single commit.

You of course loose connection to the intermediate commits in the dev branch then, so make sure to record at which point in the dev branch you merged back!

Author: Sebastian

Sebastian is a co-founder of Sandstorm Media and its chief Software Architekt. He is a long-time member of the TYPO3 Project, particularily focussing on FLOW3 and TYPO3 Phoenix. He loves clean code, design patterns and agile methodologies, but tries to stay pragmatic at the same time.
He's also committed to making great technologies available to more people, that's why he is a member of the Extbase team and lead developer of the Fluid Template Engine. You can generally find him on most of the TYPO3 events, where he's speaking about many of the topics which interest him. If you meet, make sure to say hello :-)
When he's not spending his workdays in front of the computer, he can be found climbing in the Saxon Swizerland.

4 Comments

  1. Cool, thanks for that hint!

  2. Hi Sebastian,

    thanks for the hint.

    I just wonder if this is good if more than one person committed to the temporary branch since all changes will be then “merged” back to the development branch by one user. So only the merger can be asked at the review or later on about the committed code.

    Or did I get it wrong and this author information are tracked, even with no-squash.

    Regards,

    Tim

  3. @Tim: You are right, the author information is not tracked. You need to coordinate this with your collaborator via different ways.

    However, in practice, that does not tend to be a problem as you communicate often with your development partner anyways :-)

    Greets, Sebastian

Leave a Reply

Required fields are marked *.

*