3 # Copyright (c) 2005 Junio C Hamano.
6 USAGE
='[--interactive | -i] [-v] [--force-rebase | -f] [--no-ff] [--onto <newbase>] (<upstream>|--root) [<branch>] [--quiet | -q]'
7 LONG_USAGE
='git-rebase replaces <branch> with a new branch of the
8 same name. When the --onto option is provided the new branch starts
9 out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
10 It then attempts to create a new commit for each commit from the original
11 <branch> that does not exist in the <upstream> branch.
13 It is possible that a merge failure will prevent this process from being
14 completely automatic. You will have to resolve any such merge failure
15 and run git rebase --continue. Another option is to bypass the commit
16 that caused the merge failure with git rebase --skip. To restore the
17 original <branch> and remove the .git/rebase-apply working files, use the
18 command git rebase --abort instead.
20 Note that if <branch> is not specified on the command line, the
21 currently checked out branch is used.
23 Example: git-rebase master~1 topic
25 A---B---C topic A'\''--B'\''--C'\'' topic
27 D---E---F---G master D---E---F---G master
33 set_reflog_action rebase
39 OK_TO_SKIP_PRE_REBASE
=
41 When you have resolved this problem run \"git rebase --continue\".
42 If you would prefer to skip this patch, instead run \"git rebase --skip\".
43 To restore the original branch and stop rebasing run \"git rebase --abort\".
49 merge_dir
="$GIT_DIR"/rebase-merge
50 apply_dir
="$GIT_DIR"/rebase-apply
54 test "$(git config --bool rebase.stat)" = true
&& diffstat
=t
58 allow_rerere_autoupdate
=
61 if test -d "$merge_dir"
63 state_dir
="$merge_dir"
64 prev_head
=$
(cat "$merge_dir"/prev_head
) &&
65 onto_name
=$
(cat "$merge_dir"/onto_name
) &&
66 end
=$
(cat "$merge_dir"/end
) &&
67 msgnum
=$
(cat "$merge_dir"/msgnum
)
69 state_dir
="$apply_dir"
71 head_name
=$
(cat "$state_dir"/head-name
) &&
72 onto
=$
(cat "$state_dir"/onto
) &&
73 orig_head
=$
(cat "$state_dir"/orig-head
) &&
74 GIT_QUIET
=$
(cat "$state_dir"/quiet
)
78 test -n "$prev_head" || die
"prev_head must be defined"
79 test -d "$merge_dir" || die
"$merge_dir directory does not exist"
81 unmerged
=$
(git ls-files
-u)
82 if test -n "$unmerged"
84 echo "You still have unmerged paths in your index"
85 echo "did you forget to use git add?"
89 cmt
=`cat "$merge_dir/current"`
90 if ! git diff-index
--quiet --ignore-submodules HEAD
--
92 if ! git commit
--no-verify -C "$cmt"
94 echo "Commit failed, please do not call \"git commit\""
95 echo "directly, but instead do one of the following: "
98 if test -z "$GIT_QUIET"
100 printf "Committed: %0${prec}d " $msgnum
102 echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
104 if test -z "$GIT_QUIET"
106 printf "Already applied: %0${prec}d " $msgnum
109 test -z "$GIT_QUIET" &&
110 GIT_PAGER
='' git log
--format=%s
-1 "$cmt"
112 prev_head
=`git rev-parse HEAD^0`
113 # save the resulting commit so we can read-tree on it later
114 echo "$prev_head" > "$merge_dir/prev_head"
116 # onto the next patch:
117 msgnum
=$
(($msgnum + 1))
118 echo "$msgnum" >"$merge_dir/msgnum"
122 cmt
="$(cat "$merge_dir/cmt.
$1")"
123 echo "$cmt" > "$merge_dir/current"
124 hd
=$
(git rev-parse
--verify HEAD
)
125 cmt_name
=$
(git symbolic-ref HEAD
2> /dev
/null ||
echo HEAD
)
126 msgnum
=$
(cat "$merge_dir/msgnum")
127 eval GITHEAD_
$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
128 eval GITHEAD_
$hd='$onto_name'
129 export GITHEAD_
$cmt GITHEAD_
$hd
130 if test -n "$GIT_QUIET"
132 GIT_MERGE_VERBOSITY
=1 && export GIT_MERGE_VERBOSITY
134 eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
138 unset GITHEAD_
$cmt GITHEAD_
$hd
142 git rerere
$allow_rerere_autoupdate
146 echo "Strategy: $rv $strategy failed, try another" 1>&2
150 die
"Unknown exit code ($rv) from command:" \
151 "git-merge-$strategy $cmt^ -- HEAD $cmt"
156 move_to_original_branch
() {
159 message
="rebase finished: $head_name onto $onto"
160 git update-ref
-m "$message" \
161 $head_name $
(git rev-parse HEAD
) $orig_head &&
162 git symbolic-ref HEAD
$head_name ||
163 die
"Could not move back to $head_name"
169 move_to_original_branch
170 git notes copy
--for-rewrite=rebase
< "$merge_dir"/rewritten
171 if test -x "$GIT_DIR"/hooks
/post-rewrite
&&
172 test -s "$merge_dir"/rewritten
; then
173 "$GIT_DIR"/hooks
/post-rewrite rebase
< "$merge_dir"/rewritten
184 interactive_rebase
=explicit
187 -p|
--preserve-merges)
188 interactive_rebase
=implied
194 if [ "$interactive_rebase" = implied
]; then
199 test -n "$interactive_rebase" ||
test -f "$merge_dir"/interactive
202 run_pre_rebase_hook
() {
203 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
204 test -x "$GIT_DIR/hooks/pre-rebase"
206 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
207 die
"The pre-rebase hook refused to rebase."
211 test -f "$apply_dir"/applying
&&
212 die
'It looks like git-am is in progress. Cannot rebase.'
214 is_interactive
"$@" && exec git-rebase--interactive
"$@"
220 OK_TO_SKIP_PRE_REBASE
=yes
223 OK_TO_SKIP_PRE_REBASE
=
226 test -d "$merge_dir" -o -d "$apply_dir" ||
227 die
"No rebase in progress?"
229 git update-index
--ignore-submodules --refresh &&
230 git diff-files
--quiet --ignore-submodules ||
{
231 echo "You must edit all merge conflicts and then"
232 echo "mark them as resolved using git add"
236 if test -d "$merge_dir"
239 while test "$msgnum" -le "$end"
247 git am
--resolved --3way --resolvemsg="$RESOLVEMSG" &&
248 move_to_original_branch
252 test -d "$merge_dir" -o -d "$apply_dir" ||
253 die
"No rebase in progress?"
255 git
reset --hard HEAD ||
exit $?
257 if test -d "$merge_dir"
260 msgnum
=$
(($msgnum + 1))
261 while test "$msgnum" -le "$end"
269 git am
-3 --skip --resolvemsg="$RESOLVEMSG" &&
270 move_to_original_branch
274 test -d "$merge_dir" -o -d "$apply_dir" ||
275 die
"No rebase in progress?"
281 git symbolic-ref HEAD
$head_name ||
282 die
"Could not move back to $head_name"
285 git
reset --hard $orig_head
290 test 2 -le "$#" || usage
294 -M|
-m|
--m|
--me|
--mer|
--merg|
--merge)
297 -X*|
--strategy-option*)
299 1,-X|
1,--strategy-option)
301 *,-X|
*,--strategy-option)
304 *,--strategy-option=*)
305 newopt
="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
307 newopt
="$(expr " $1" : ' -X\(.*\)')" ;;
311 strategy_opts
="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
314 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
315 --strateg=*|
--strategy=*|\
316 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
319 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
341 git_am_opt
="$git_am_opt -q"
346 git_am_opt
="$git_am_opt $1"
348 --whitespace=fix|
--whitespace=strip
)
354 git_am_opt
="$git_am_opt $1"
356 --committer-date-is-author-date|
--ignore-date)
357 git_am_opt
="$git_am_opt $1"
361 git_am_opt
="$git_am_opt $1"
366 -f|
--f|
--fo|
--for|
--forc|
--force|
--force-r|
--force-re|
--force-reb|
--force-reba|
--force-rebas|
--force-rebase|
--no-ff)
369 --rerere-autoupdate|
--no-rerere-autoupdate)
370 allow_rerere_autoupdate
="$1"
381 test $# -gt 2 && usage
383 if test $# -eq 0 && test -z "$rebase_root"
385 test -d "$merge_dir" -o -d "$apply_dir" || usage
386 test -d "$merge_dir" -o -f "$apply_dir"/rebasing
&&
387 die
'A rebase is in progress, try --continue, --skip or --abort.'
390 # Make sure we do not have $apply_dir or $merge_dir
391 if test -z "$do_merge"
393 if mkdir
"$apply_dir" 2>/dev
/null
398 It seems that I cannot create a rebase-apply directory, and
399 I wonder if you are in the middle of patch application or another
400 rebase. If that is not the case, please
401 rm -fr '"$apply_dir"'
402 and run me again. I am stopping in case you still have something
407 if test -d "$merge_dir"
409 die
"previous rebase directory $merge_dir still exists." \
410 'Try git rebase (--continue | --abort | --skip)'
414 require_clean_work_tree
"rebase" "Please commit or stash them."
416 if test -z "$rebase_root"
418 # The upstream head must be given. Make sure it is valid.
421 upstream
=`git rev-parse --verify "${upstream_name}^0"` ||
422 die
"invalid upstream $upstream_name"
424 upstream_arg
="$upstream_name"
426 test -z "$newbase" && die
"--root must be used with --onto"
430 upstream_arg
="$root_flag"
433 # Make sure the branch to rebase onto is valid.
434 onto_name
=${newbase-"$upstream_name"}
437 if left
=${onto_name%...*} right
=${onto_name#*...} &&
438 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
442 die
"$onto_name: there are more than one merge bases"
445 die
"$onto_name: there is no merge base"
449 die
"$onto_name: there is no merge base"
453 onto
=$
(git rev-parse
--verify "${onto_name}^0") ||
exit
457 # If a hook exists, give it a chance to interrupt
458 run_pre_rebase_hook
"$upstream_arg" "$@"
460 # If the branch to rebase is given, that is the branch we will rebase
461 # $branch_name -- branch being rebased, or HEAD (already detached)
462 # $orig_head -- commit object name of tip of the branch before rebasing
463 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
467 # Is it "rebase other $branchname" or "rebase other $commit"?
471 if git show-ref
--verify --quiet -- "refs/heads/$1" &&
472 branch
=$
(git rev-parse
-q --verify "refs/heads/$1")
474 head_name
="refs/heads/$1"
475 elif branch
=$
(git rev-parse
-q --verify "$1")
477 head_name
="detached HEAD"
479 echo >&2 "fatal: no such branch: $1"
484 # Do not need to switch branches, we are already on it.
485 if branch_name
=`git symbolic-ref -q HEAD`
487 head_name
=$branch_name
488 branch_name
=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
490 head_name
="detached HEAD"
491 branch_name
=HEAD
;# detached
493 branch
=$
(git rev-parse
--verify "${branch_name}^0") ||
exit
498 # Now we are rebasing commits $upstream..$branch (or with --root,
499 # everything leading up to $branch) on top of $onto
501 # Check if we are already based on $onto with linear history,
502 # but this should be done only when upstream and onto are the same.
503 mb
=$
(git merge-base
"$onto" "$branch")
504 if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
506 ! (git rev-list
--parents "$onto"..
"$branch" | sane_grep
" .* ") > /dev
/null
508 if test -z "$force_rebase"
510 # Lazily switch to the target branch if needed...
511 test -z "$switch_to" || git checkout
"$switch_to" --
512 say
"Current branch $branch_name is up to date."
515 say
"Current branch $branch_name is up to date, rebase forced."
519 # Detach HEAD and reset the tree
520 say
"First, rewinding head to replay your work on top of it..."
521 git checkout
-q "$onto^0" || die
"could not detach HEAD"
522 git update-ref ORIG_HEAD
$branch
524 if test -n "$diffstat"
526 if test -n "$verbose"
528 echo "Changes from $mb to $onto:"
530 # We want color (if set), but no pager
531 GIT_PAGER
='' git
diff --stat --summary "$mb" "$onto"
534 # If the $onto is a proper descendant of the tip of the branch, then
535 # we just fast-forwarded.
536 if test "$mb" = "$branch"
538 say
"Fast-forwarded $branch_name to $onto_name."
539 move_to_original_branch
543 if test -n "$rebase_root"
545 revisions
="$onto..$orig_head"
547 revisions
="$upstream..$orig_head"
550 if test -z "$do_merge"
552 git format-patch
-k --stdout --full-index --ignore-if-in-upstream \
553 --src-prefix=a
/ --dst-prefix=b
/ \
554 --no-renames $root_flag "$revisions" |
555 git am
$git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
556 move_to_original_branch
558 test 0 != $ret -a -d "$apply_dir" &&
559 echo $head_name > "$apply_dir/head-name" &&
560 echo $onto > "$apply_dir/onto" &&
561 echo $orig_head > "$apply_dir/orig-head" &&
562 echo "$GIT_QUIET" > "$apply_dir/quiet"
566 # start doing a rebase with git-merge
567 # this is rename-aware if the recursive (default) strategy is used
569 mkdir
-p "$merge_dir"
570 echo "$onto_name" > "$merge_dir/onto_name"
572 echo "$prev_head" > "$merge_dir/prev_head"
573 echo "$head_name" > "$merge_dir/head-name"
574 echo "$onto" > "$merge_dir/onto"
575 echo "$orig_head" > "$merge_dir/orig-head"
576 echo "$GIT_QUIET" > "$merge_dir/quiet"
579 for cmt
in `git rev-list --reverse --no-merges "$revisions"`
581 msgnum
=$
(($msgnum + 1))
582 echo "$cmt" > "$merge_dir/cmt.$msgnum"
585 echo 1 >"$merge_dir/msgnum"
586 echo $msgnum >"$merge_dir/end"
591 while test "$msgnum" -le "$end"