Squash several Git commits into a single commit
This note shows how to merge an ugly feature branch with multiple dirty WIP commits back into the master as one pretty commit.
Easy mode: Reset your feature branch to the master state
The easiest way to turn multiple commits in a feature branch into a single commit is to reset the feature branch changes in the master and commit everything again.
# Switch to the master branch and make sure you are up to date. git checkout master git fetch # this may be necessary (depending on your git config) to receive updates on origin/master git pull # Merge the feature branch into the master branch. git merge feature_branch # Reset the master branch to origin's state. git reset origin/master # Git now considers all changes as unstaged changes. # We can add these changes as one commit. # Adding . will also add untracked files. git add --all git commit
Note that this is not touching the feature branch at all. If you would merge the feature branch into the master again at a later stage all of its commits would reappear in the log.
You may also do it the other way round (merging master into the branch and resetting to the master state) but this will destroy your commits in the feature branch, meaning you can not push it to
Hard mode: Squash commits
This method is harder than using the
get reset method above. Also it doesn't work well if you merged the master into the feature branch previously (you'll need to resolve all conflicts again).
What we are describing here will destroy commit history and can go wrong. For this reason, do the squashing on a separate branch:
git checkout -b squashed_feature
This way, if you screw up, you can go back to your original branch, make another branch for squashing and try again.
To squash all commits since you branched away from master, do
git rebase -i master
Note that rebasing to the master does not work if you merged the master into your feature branch while you were working on the new feature. If you did this you will need to find the original branch point and call
git rebase with a SHA1 revision.
Your editor will open with a file like
pick fda59df commit 1 pick x536897 commit 2 pick c01a668 commit 3
Each line represents a commit (in chronological order, the latest commit will be at the bottom).
To transform all these commits into a single one, change the file to this:
pick fda59df commit 1 squash x536897 commit 2 squash c01a668 commit 3
This means, you take the first commit, and squash the following onto it. If you remove a line, the corresponding commit is actually really lost. Don't bother changing the commit messages because they are ignored. After saving the squash settings, your editor will open once more to ask for a commit message for the squashed commit.
You can now merge your feature as a single commit into the master:
git checkout master git merge squashed_feature
Author of this card:
- About this deck:
- We are makandra and do test-driven, agile Ruby on Rails software development.
- License for source code
License for source code
All source code included in the card Squash several Git commits into a single commit is licensed under the license stated below. This includes both code snippets embedded in the card text and code that is included as a file attachment. Excepted from this license are code snippets that are explicitely marked as citations from another source.
The MIT License (MIT) Copyright (c) 2010-2012 makandra GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.