-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-remaster
executable file
·49 lines (41 loc) · 1.33 KB
/
git-remaster
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
#!/bin/bash
#------------------------------------------------------------------------------#
#
# git-remaster
#
# Rebases the current branch and updates the main branch at the same time,
# preserving staged and unstaged changes in both.
#
# git checkout my-branch
# git remaster
#
#------------------------------------------------------------------------------#
set -e
MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD --short | sed 's,origin/,,')
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ -z $MAIN_BRANCH ]; then
echo "No master/trunk branch found."
exit 1
fi
# save current state to a detached commit object and reset
RSTASH=$(git stash create --include-untracked)
if [[ $RSTASH ]]; then
echo "Stashed $RSTASH"
git stash store -m "Remaster on branch $BRANCH $(date)" "$RSTASH"
git reset --hard
fi
# fetch updates
if git fetch --no-tags origin $MAIN_BRANCH; then
# if not on master branch, check it out, update and return to branch
if [[ $BRANCH != $MAIN_BRANCH ]]; then
git checkout $MAIN_BRANCH
git rebase origin/HEAD --rebase-merges --autostash
git checkout -
fi
# rebase changes on top of origin/master
git rebase origin/$MAIN_BRANCH --rebase-merges
fi
# bring back stash if available, hide errors when empty
if test $RSTASH; then
git stash apply $RSTASH
fi