Skip to content

Commit b528531

Browse files
committed
Initial commit
0 parents  commit b528531

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# git squash
2+
3+
Squash commits on branch just like GitHub's "Squash and merge".
4+
5+
Useful if for some reason you need to use this functionality but you don't have permission or you don't use GitHub.
6+
7+
## Usage
8+
9+
```
10+
# Squash all changes on current branch that happened since master branch
11+
git squash master
12+
```
13+
14+
## License
15+
16+
MIT

git-squash

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 <branch>"
12+
echo ""
13+
echo "Examples"
14+
echo " - git squash master"
15+
exit 1
16+
fi
17+
18+
if [ "$(git status -s)" != "" ]; 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 master.. --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"

0 commit comments

Comments
 (0)