├── LICENSE ├── README.md └── git-squash /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020-present Adam Stankiewicz 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # git squash [![Maintenance](https://img.shields.io/maintenance/yes/2021.svg?maxAge=2592000)]() 4 | 5 | Locally squash commits on a branch, without needing to resolve any conflicts 🧈 6 | 7 | It works just like GitHub's "Squash and merge" or GitLab's "Squash commits". 8 | 9 | ## Installation 10 | 11 | With [Homebrew](https://brew.sh/) on MacOS and Linux: 12 | 13 | ``` 14 | brew install sheerun/git-squash/git-squash 15 | ``` 16 | 17 | With `curl` on MacOS and Linux, including Windows Subsystem for Linux: 18 | 19 | ``` 20 | curl https://raw.githubusercontent.com/sheerun/git-squash/master/git-squash > /usr/local/bin/git-squash && chmod a+x /usr/local/bin/git-squash 21 | ``` 22 | 23 | ## Usage 24 | 25 | ```sh 26 | # This tool requires that target branch is mergable to current one 27 | # The easiest way to ensure it is to merge it and resolve any conflicts 28 | git merge master 29 | # Squash all changes on current branch that happened since master branch 30 | git squash master 31 | ``` 32 | 33 | ## License 34 | 35 | MIT 36 | -------------------------------------------------------------------------------- /git-squash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)" 6 | TEMP_BRANCH="temp$(date +%s)" 7 | 8 | if [ "$1" == "" ]; then 9 | echo "Squashes all commits just like GitHub squash merge" 10 | echo "" 11 | echo "Usage: git squash " 12 | echo "" 13 | echo "Examples" 14 | echo " - git squash master" 15 | exit 1 16 | fi 17 | 18 | if [ "$(git status -s -uno)" != "" ]; then 19 | echo "Please commit all changes before squashing" 20 | exit 1 21 | fi 22 | 23 | if ! git show-ref "$1" > /dev/null; then 24 | echo "Branch $1 does not exist" 25 | exit 1 26 | fi 27 | 28 | FIRST_COMMIT_ID=$(git log $1.. --no-merges --pretty=format:%h | tail -1) 29 | 30 | if [ "$FIRST_COMMIT_ID" == "" ]; then 31 | echo "There are no changes to be squashed" 32 | exit 1 33 | fi 34 | 35 | git checkout -q -b "$TEMP_BRANCH" "$1" 36 | 37 | function finish { 38 | git checkout -q --force "$CURRENT_BRANCH" 39 | git branch -q -D "$TEMP_BRANCH" 40 | } 41 | trap finish EXIT 42 | 43 | git merge --squash "$CURRENT_BRANCH" 44 | 45 | git add -A 46 | 47 | git commit -q -c "$FIRST_COMMIT_ID" 48 | 49 | git checkout -q "$CURRENT_BRANCH" 50 | 51 | git reset -q --hard "$TEMP_BRANCH" 52 | --------------------------------------------------------------------------------