-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-spinoff
executable file
·40 lines (34 loc) · 1.24 KB
/
git-spinoff
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
#! /bin/sh
# git-spinoff: spin current work off into a new branch
#
# usage is:
#
# git spinoff <branchname>
#
# which creates a new branch named <branchname> pointing to the
# current commit, ready for new commits if the index and/or work
# tree are dirty, and restoring the current branch to its upstream,
# provided that we are in fact on a branch now that has an upstream.
die() {
echo "fatal: $@" 1>&2
exit 1
}
# check arguments
case $# in
1) newbranch="$1";;
*) echo "usage: git spinoff <newbranch>" 1>&2; exit 1;;
esac
# make sure we are on a branch that has an upstream
fullbranch=$(git symbolic-ref -q HEAD) ||
die "existing branch is detached; there's nothing to restore"
branch=${fullbranch#refs/heads/}
upstream=$(git rev-parse -q --verify @{u}) ||
die "existing branch $branch has no upstream: there's nowhere to restore-to"
# now create and check out out the new branch, or quit if we can't
git checkout -b "$newbranch" || exit $?
# last, re-adjust the previous branch (which is no longer the current
# branch since we are on the new one we created) to point to its own
# upstream (if this fails, ignore the failure!)
git update-ref -m \
"git spinoff: re-point $branch to ${branch}@{upstream}" \
$fullbranch $upstream