14
14
PREVBRANCH=$( git rev-parse --symbolic-full-name --abbrev-ref HEAD)
15
15
TMPBRANCH=" tmp-$( cat /dev/urandom | tr -dc ' a-z0-9' | fold -w 16 | head -n 1) "
16
16
17
- # 0 - Commit changes
17
+ # Commit all staged changes
18
18
if ! git commit -m " MERGE $TARGETHASH "
19
19
then
20
20
echo " Failed to commit changes. Did you forget to add them?"
23
23
24
24
NEWCHANGES=$( git rev-parse HEAD)
25
25
26
- # 1 - Stash any other changes
26
+ # Stash any other changes
27
27
DIRTYFILECOUNT=$( git status --porcelain 2> /dev/null| grep " ^ M" | wc -l)
28
28
[ $DIRTYFILECOUNT -ne 0 ] && git stash save --keep-index
29
29
30
- # 2 - Switch to a temporary branch
30
+ # Switch to a temporary branch
31
31
if ! git checkout -b $TMPBRANCH
32
32
then
33
33
echo " Failed to checkout a temporary branch. Please check your repository's state and try again."
34
34
[ $DIRTYFILECOUNT -ne 0 ] && git stash pop
35
35
exit 1
36
36
fi
37
37
38
- # 3 - Rewind to the target commit
38
+ # Rewind to the target commit
39
39
if ! git reset --hard $TARGETHASH
40
40
then
41
41
echo " Failed to rewind to target commit."
@@ -45,7 +45,7 @@ if ! git reset --hard $TARGETHASH
45
45
exit 1
46
46
fi
47
47
48
- # 4 - Stage changes
48
+ # Use cherry-pick to stage the changes
49
49
if ! git cherry-pick -n $NEWCHANGES
50
50
then
51
51
echo " Failed to apply new changes."
@@ -55,7 +55,7 @@ if ! git cherry-pick -n $NEWCHANGES
55
55
exit 1
56
56
fi
57
57
58
- # 4 - Merge staged changes at this point
58
+ # Merge staged changes at this point
59
59
if ! git commit --amend
60
60
then
61
61
echo " Failed to merge changes into the target commit."
@@ -65,7 +65,7 @@ if ! git commit --amend
65
65
exit 1
66
66
fi
67
67
68
- # 5 - Finally rebase
68
+ # Finally we rebase to include the proceeding commits
69
69
if ! git rebase --onto $TMPBRANCH HEAD@{1} $PREVBRANCH
70
70
then
71
71
echo " Failed to perform the final rebase."
@@ -75,9 +75,11 @@ if ! git rebase --onto $TMPBRANCH HEAD@{1} $PREVBRANCH
75
75
exit 1
76
76
fi
77
77
78
+ # Pop any uncommitted changes from the start
78
79
[ $DIRTYFILECOUNT -ne 0 ] && git stash pop
79
80
git branch -d $TMPBRANCH
80
81
82
+ # Great success!
81
83
echo " Successfully merged changes with commit $TARGETHASH ."
82
84
exit 0
83
85
0 commit comments