-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-squash
More file actions
executable file
·53 lines (38 loc) · 1.06 KB
/
git-squash
File metadata and controls
executable file
·53 lines (38 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
# from https://github.com/sheerun/git-squash/blob/e87fb1d410edceec3670101e2cf89297ecab5813/git-squash
set -e
CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
TEMP_BRANCH="temp$(date +%s)"
if [ "$1" == "" ]; then
echo "Squashes all commits just like GitHub squash merge"
echo ""
echo "Usage: git squash <branch>"
echo ""
echo "Examples"
echo " - git squash master"
exit 1
fi
if [ "$(git status -s -uno)" != "" ]; then
echo "Please commit all changes before squashing"
exit 1
fi
if ! git show-ref "$1" > /dev/null; then
echo "Branch $1 does not exist"
exit 1
fi
FIRST_COMMIT_ID=$(git log $1.. --no-merges --pretty=format:%h | tail -1)
if [ "$FIRST_COMMIT_ID" == "" ]; then
echo "There are no changes to be squashed"
exit 1
fi
git checkout -q -b "$TEMP_BRANCH" "$1"
function finish {
git checkout -q --force "$CURRENT_BRANCH"
git branch -q -D "$TEMP_BRANCH"
}
trap finish EXIT
git merge --squash "$CURRENT_BRANCH"
git add -A
git commit -q -c "$FIRST_COMMIT_ID"
git checkout -q "$CURRENT_BRANCH"
git reset -q --hard "$TEMP_BRANCH"