-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsquashbranch
executable file
·58 lines (49 loc) · 2.21 KB
/
squashbranch
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
54
55
56
57
58
#!/bin/bash
# Squash all changes on the current branch in one go
# Copyright (c) 2018 Benjamin Holt -- MIT License
if [ "$1" == "-h" -o "$1" == "--help" ]; then
cat <<USAGE
usage: squashbranch [PARENT] Squash all changes on the current branch; PARENT
defaults to "main". Current branch must have
clean status so local changes don't get scooped
up in the squash. To undo the squash prior to
committing:
$ git reset --hard && git pull
squashbranch -h|--help Print this message and exit
USAGE
exit 0
fi
Bold=`tput bold`
Off=`tput sgr0`
Status=`git status -s`
if [ "$Status" ]; then
echo "${Bold}Current branch has uncommitted changes:${Off}"
echo "This cannot distinguish between changes to be squashed and local or untracked changes; commit changes you want or stash changes you don't (including untracked files)"
echo "$Status"
exit 1
fi
Parent="${1:-main}"
BranchName=`git rev-parse --abbrev-ref HEAD`
if [ "$BranchName" == "main" -o "$BranchName" == "master" ]; then
echo "${Bold}Squashing the $BranchName branch is not generally a good idea${Off}" 1>&2
exit 1
fi
if [ "$Parent" == "$BranchName" ]; then
echo "${Bold}Cannot squash $BranchName relative to itself${Off}" 1>&2
exit 1
fi
BranchPoint=`git merge-base "$Parent" "$BranchName"`
BranchHashes=`git log "$BranchPoint..HEAD" --format=format:%h | tr "\n" " "`
echo "----------------------------------------------------------------------"
echo "${Bold}Squashing back to $BranchPoint:${Off}"
git log "$BranchPoint~1..HEAD" --format=oneline --abbrev-commit # Include branched commit
echo "----------------------------------------------------------------------"
# echo "${Bold}DIDN'T REALLY SQUASH${Off}" && exit 0 # Early-exit for testing
git reset "$BranchPoint"
git add -A
echo "----------------------------------------------------------------------"
git status
echo "----------------------------------------------------------------------"
echo "${Bold}Squashed:${Off} $BranchHashes"
echo "Now git commit with a message for the single commit and force-push (or undo with 'git reset --hard && git pull')"
###