rebase: allow rebasing onto different base.
This allows you to rewrite history a bit more flexibly, by separating the other branch name and new branch point. By default, the new branch point is the same as the tip of the other branch as before, but you can specify where you graft the rebased branch onto. When you have this ancestry graph: A---B---C topic / D---E---F---G master $ git rebase --onto master~1 master topic would rewrite the history to look like this: A'\''--B'\''--C'\'' topic / D---E---F---G master Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Feb 14, 2006 at 14:42 UTC
e646c9c8c0aa995eac284ea0a2117add19c4461c
1 file changed
+71
-18
git-rebase.sh
+71
-18
@@ -3,10 +3,48 @@
3
# Copyright (c) 2005 Junio C Hamano.
4
#
5
6
-USAGE='<upstream> [<head>]'
6
+USAGE='[--onto <newbase>] <upstream> [<branch>]'
7
+LONG_USAGE='If <branch> is specified, switch to that branch first. Then,
8
+extract commits in the current branch that are not in <upstream>,
9
+and reconstruct the current on top of <upstream>, discarding the original
10
+development history. If --onto <newbase> is specified, the history is
11
+reconstructed on top of <newbase>, instead of <upstream>. For example,
12
+while on "topic" branch:
13
+
14
+ A---B---C topic
15
+ /
16
+ D---E---F---G master
17
+
18
+ $ '"$0"' --onto master~1 master topic
19
+
20
+would rewrite the history to look like this:
21
+
22
+
23
+ A'\''--B'\''--C'\'' topic
24
+ /
25
+ D---E---F---G master
26
+'
27
+
28
. git-sh-setup
29
9
-case $# in 1|2) ;; *) usage ;; esac
30
+unset newbase
31
+while case "$#" in 0) break ;; esac
32
+do
33
+ case "$1" in
34
+ --onto)
35
+ test 2 -le "$#" || usage
36
+ newbase="$2"
37
+ shift
38
+ ;;
39
+ -*)
40
+ usage
41
+ ;;
42
+ *)
43
+ break
44
+ ;;
45
+ esac
46
+ shift
47
+done
48
49
# Make sure we do not have .dotest
50
if mkdir .dotest
@@ -30,37 +68,52 @@ case "$diff" in
68
;;
69
esac
70
33
-# The other head is given. Make sure it is valid.
34
-other=$(git-rev-parse --verify "$1^0") || usage
35
-
36
-# Make sure the branch to rebase is valid.
37
-head=$(git-rev-parse --verify "${2-HEAD}^0") || exit
71
+# The upstream head must be given. Make sure it is valid.
72
+upstream_name="$1"
73
+upstream=`git rev-parse --verify "${upstream_name}^0"` ||
74
+ die "invalid upsteram $upstream_name"
75
76
# If the branch to rebase is given, first switch to it.
77
case "$#" in
78
2)
79
+ branch_name="$2"
80
git-checkout "$2" || usage
81
+ ;;
82
+*)
83
+ branch_name=`git symbolic-ref HEAD` || die "No current branch"
84
+ branch_name=`expr "$branch_name" : 'refs/heads/\(.*\)'`
85
+ ;;
86
esac
87
+branch=$(git-rev-parse --verify "${branch_name}^0") || exit
88
45
-mb=$(git-merge-base "$other" "$head")
89
+# Make sure the branch to rebase onto is valid.
90
+onto_name=${newbase-"$upstream_name"}
91
+onto=$(git-rev-parse --verify "${onto_name}^0") || exit
92
47
-# Check if we are already based on $other.
48
-if test "$mb" = "$other"
93
+# Now we are rebasing commits $upstream..$branch on top of $onto
94
+
95
+# Check if we are already based on $onto, but this should be
96
+# done only when upstream and onto are the same.
97
+if test "$upstream" = "onto"
98
then
50
- echo >&2 "Current branch `git-symbolic-ref HEAD` is up to date."
51
- exit 0
99
+ mb=$(git-merge-base "$onto" "$branch")
100
+ if test "$mb" = "$onto"
101
+ then
102
+ echo >&2 "Current branch $branch_name is up to date."
103
+ exit 0
104
+ fi
105
fi
106
54
-# Rewind the head to "$other"
55
-git-reset --hard "$other"
107
+# Rewind the head to "$onto"; this saves our current head in ORIG_HEAD.
108
+git-reset --hard "$onto"
109
57
-# If the $other is a proper descendant of the tip of the branch, then
110
+# If the $onto is a proper descendant of the tip of the branch, then
111
# we just fast forwarded.
59
-if test "$mb" = "$head"
112
+if test "$mb" = "$onto"
113
then
61
- echo >&2 "Fast-forwarded $head to $other."
114
+ echo >&2 "Fast-forwarded $branch to $newbase."
115
exit 0
116
fi
117
65
-git-format-patch -k --stdout --full-index "$other" ORIG_HEAD |
118
+git-format-patch -k --stdout --full-index "$upstream" ORIG_HEAD |
119
git am --binary -3 -k