-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathgit-move-commits
executable file
·53 lines (49 loc) · 1.47 KB
/
git-move-commits
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
#!/usr/bin/env bash
# Original source: https://github.com/coordt/git-scripts/blob/master/git-move-commits
#
# git move-commits num-commits correct-branch
#
# moves the last n commits to correct-branch
# if correct-branch doesn't exist, it creates it
# if correct-branch does exist, it merges the commits
# shellcheck disable=SC2046,SC2143,SC2006,SC2086,SC2063
set -e
if [ "$1" ]
then
# shellcheck disable=SC2046,SC2143
if [ ! $(echo "$1" | grep -E "^[0-9]+$") ]
then
echo "$1 is not a number."
echo "Usage: git move-commits <num-commits> <correct-branch>";
exit 1;
else
# The count is 0 based, so to move the last 1 commit, we need HEAD~0
NUM_COMMITS="$1"
((NUM_COMMITS--))
echo "num commits $NUM_COMMITS"
fi
else
echo "Usage: git move-commits <num-commits> <correct-branch>";
exit 1;
fi
if [ -z "$2" ]
then
echo "Usage: git move-commits <num-commits> <correct-branch>";
exit 1;
else
BRANCH="$2"
fi
# shellcheck disable=SC2063
CURRENT_BRANCH=$(git branch | grep '*' | perl -e '<STDIN> =~ /^..(.+)$/;print $1')
# If the branch already exists, we have to merge it with the current branch
if [ -n "$(git branch -a | grep "$BRANCH")" ]
then
echo "$BRANCH already exists. Switching to it and merging $CURRENT_BRANCH"
git checkout "$BRANCH"
git merge "$CURRENT_BRANCH"
else
echo "Creating $BRANCH"
git branch "$BRANCH"
fi
git reset --hard "$CURRENT_BRANCH~$NUM_COMMITS"
git checkout "$BRANCH"