git-branch: -f to forcibly reset branch head.
A new usage, 'git-branch -f branch [start]', resets the branch head at start (or current head). Should be considered a dangerous operation, but if you are like me to keep rewinding branches it is handy. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Nov 11, 2005 at 11:12 UTC
eb777612f085fc42afd0ac6986ed8062f8b75cbd
1 file changed
+18
-7
git-branch.sh
+18
-7
@@ -3,7 +3,7 @@
3
. git-sh-setup || die "Not a git archive"
4
5
usage () {
6
- echo >&2 "usage: $(basename $0)"' [-d <branch>] | [<branch> [start-point]]
6
+ echo >&2 "usage: $(basename $0)"' [-d <branch>] | [[-f] <branch> [start-point]]
7
8
If no arguments, show available branches and mark current branch with a star.
9
If one argument, create a new branch <branchname> based off of current HEAD.
@@ -12,11 +12,12 @@ If two arguments, create a new branch <branchname> based off of <start-point>.
12
exit 1
13
}
14
15
+headref=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD |
16
+ sed -e 's|^refs/heads/||')
17
+
18
delete_branch () {
19
option="$1"
20
shift
18
- headref=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD |
19
- sed -e 's|^refs/heads/||')
21
for branch_name
22
do
23
case ",$headref," in
@@ -52,6 +53,7 @@ delete_branch () {
53
exit 0
54
}
55
56
+force=
57
while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
58
do
59
case "$1" in
@@ -59,6 +61,9 @@ do
61
delete_branch "$@"
62
exit
63
;;
64
+ -f)
65
+ force="$1"
66
+ ;;
67
--)
68
shift
69
break
@@ -72,8 +77,6 @@ done
77
78
case "$#" in
79
0)
75
- headref=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD |
76
- sed -e 's|^refs/heads/||')
80
git-rev-parse --symbolic --all |
81
sed -ne 's|^refs/heads/||p' |
82
sort |
@@ -97,10 +100,18 @@ branchname="$1"
100
101
rev=$(git-rev-parse --verify "$head") || exit
102
100
-[ -e "$GIT_DIR/refs/heads/$branchname" ] &&
101
- die "$branchname already exists."
103
git-check-ref-format "heads/$branchname" ||
104
die "we do not like '$branchname' as a branch name."
105
106
+if [ -e "$GIT_DIR/refs/heads/$branchname" ]
107
+then
108
+ if test '' = "$force"
109
+ then
110
+ die "$branchname already exists."
111
+ elif test "$branchname" = "$headref"
112
+ then
113
+ die "cannot force-update the current branch."
114
+ fi
115
+fi
116
git update-ref "refs/heads/$branchname" $rev
117