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 onto_name
=$
(cat "$merge_dir"/onto_name
) &&
65 end
=$
(cat "$merge_dir"/end
) &&
66 msgnum
=$
(cat "$merge_dir"/msgnum
)
68 state_dir
="$apply_dir"
70 head_name
=$
(cat "$state_dir"/head-name
) &&
71 onto
=$
(cat "$state_dir"/onto
) &&
72 orig_head
=$
(cat "$state_dir"/orig-head
) &&
73 GIT_QUIET
=$
(cat "$state_dir"/quiet
)
77 test -d "$merge_dir" || die
"$merge_dir directory does not exist"
79 unmerged
=$
(git ls-files
-u)
80 if test -n "$unmerged"
82 echo "You still have unmerged paths in your index"
83 echo "did you forget to use git add?"
87 cmt
=`cat "$merge_dir/current"`
88 if ! git diff-index
--quiet --ignore-submodules HEAD
--
90 if ! git commit
--no-verify -C "$cmt"
92 echo "Commit failed, please do not call \"git commit\""
93 echo "directly, but instead do one of the following: "
96 if test -z "$GIT_QUIET"
98 printf "Committed: %0${prec}d " $msgnum
100 echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
102 if test -z "$GIT_QUIET"
104 printf "Already applied: %0${prec}d " $msgnum
107 test -z "$GIT_QUIET" &&
108 GIT_PAGER
='' git log
--format=%s
-1 "$cmt"
110 # onto the next patch:
111 msgnum
=$
(($msgnum + 1))
112 echo "$msgnum" >"$merge_dir/msgnum"
116 cmt
="$(cat "$merge_dir/cmt.
$1")"
117 echo "$cmt" > "$merge_dir/current"
118 hd
=$
(git rev-parse
--verify HEAD
)
119 cmt_name
=$
(git symbolic-ref HEAD
2> /dev
/null ||
echo HEAD
)
120 msgnum
=$
(cat "$merge_dir/msgnum")
121 eval GITHEAD_
$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
122 eval GITHEAD_
$hd='$onto_name'
123 export GITHEAD_
$cmt GITHEAD_
$hd
124 if test -n "$GIT_QUIET"
126 GIT_MERGE_VERBOSITY
=1 && export GIT_MERGE_VERBOSITY
128 eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
132 unset GITHEAD_
$cmt GITHEAD_
$hd
136 git rerere
$allow_rerere_autoupdate
140 echo "Strategy: $rv $strategy failed, try another" 1>&2
144 die
"Unknown exit code ($rv) from command:" \
145 "git-merge-$strategy $cmt^ -- HEAD $cmt"
150 move_to_original_branch
() {
153 message
="rebase finished: $head_name onto $onto"
154 git update-ref
-m "$message" \
155 $head_name $
(git rev-parse HEAD
) $orig_head &&
156 git symbolic-ref HEAD
$head_name ||
157 die
"Could not move back to $head_name"
163 move_to_original_branch
164 git notes copy
--for-rewrite=rebase
< "$merge_dir"/rewritten
165 if test -x "$GIT_DIR"/hooks
/post-rewrite
&&
166 test -s "$merge_dir"/rewritten
; then
167 "$GIT_DIR"/hooks
/post-rewrite rebase
< "$merge_dir"/rewritten
178 interactive_rebase
=explicit
181 -p|
--preserve-merges)
182 interactive_rebase
=implied
188 if [ "$interactive_rebase" = implied
]; then
193 test -n "$interactive_rebase" ||
test -f "$merge_dir"/interactive
196 run_pre_rebase_hook
() {
197 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
198 test -x "$GIT_DIR/hooks/pre-rebase"
200 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
201 die
"The pre-rebase hook refused to rebase."
205 test -f "$apply_dir"/applying
&&
206 die
'It looks like git-am is in progress. Cannot rebase.'
208 is_interactive
"$@" && exec git-rebase--interactive
"$@"
214 OK_TO_SKIP_PRE_REBASE
=yes
217 OK_TO_SKIP_PRE_REBASE
=
220 test -d "$merge_dir" -o -d "$apply_dir" ||
221 die
"No rebase in progress?"
223 git update-index
--ignore-submodules --refresh &&
224 git diff-files
--quiet --ignore-submodules ||
{
225 echo "You must edit all merge conflicts and then"
226 echo "mark them as resolved using git add"
230 if test -d "$merge_dir"
233 while test "$msgnum" -le "$end"
241 git am
--resolved --3way --resolvemsg="$RESOLVEMSG" &&
242 move_to_original_branch
246 test -d "$merge_dir" -o -d "$apply_dir" ||
247 die
"No rebase in progress?"
249 git
reset --hard HEAD ||
exit $?
251 if test -d "$merge_dir"
254 msgnum
=$
(($msgnum + 1))
255 while test "$msgnum" -le "$end"
263 git am
-3 --skip --resolvemsg="$RESOLVEMSG" &&
264 move_to_original_branch
268 test -d "$merge_dir" -o -d "$apply_dir" ||
269 die
"No rebase in progress?"
275 git symbolic-ref HEAD
$head_name ||
276 die
"Could not move back to $head_name"
279 git
reset --hard $orig_head
284 test 2 -le "$#" || usage
288 -M|
-m|
--m|
--me|
--mer|
--merg|
--merge)
291 -X*|
--strategy-option*)
293 1,-X|
1,--strategy-option)
295 *,-X|
*,--strategy-option)
298 *,--strategy-option=*)
299 newopt
="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
301 newopt
="$(expr " $1" : ' -X\(.*\)')" ;;
305 strategy_opts
="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
308 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
309 --strateg=*|
--strategy=*|\
310 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
313 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
335 git_am_opt
="$git_am_opt -q"
340 git_am_opt
="$git_am_opt $1"
342 --whitespace=fix|
--whitespace=strip
)
348 git_am_opt
="$git_am_opt $1"
350 --committer-date-is-author-date|
--ignore-date)
351 git_am_opt
="$git_am_opt $1"
355 git_am_opt
="$git_am_opt $1"
360 -f|
--f|
--fo|
--for|
--forc|
--force|
--force-r|
--force-re|
--force-reb|
--force-reba|
--force-rebas|
--force-rebase|
--no-ff)
363 --rerere-autoupdate|
--no-rerere-autoupdate)
364 allow_rerere_autoupdate
="$1"
375 test $# -gt 2 && usage
377 if test $# -eq 0 && test -z "$rebase_root"
379 test -d "$merge_dir" -o -d "$apply_dir" || usage
380 test -d "$merge_dir" -o -f "$apply_dir"/rebasing
&&
381 die
'A rebase is in progress, try --continue, --skip or --abort.'
384 # Make sure we do not have $apply_dir or $merge_dir
385 if test -z "$do_merge"
387 if mkdir
"$apply_dir" 2>/dev
/null
392 It seems that I cannot create a rebase-apply directory, and
393 I wonder if you are in the middle of patch application or another
394 rebase. If that is not the case, please
395 rm -fr '"$apply_dir"'
396 and run me again. I am stopping in case you still have something
401 if test -d "$merge_dir"
403 die
"previous rebase directory $merge_dir still exists." \
404 'Try git rebase (--continue | --abort | --skip)'
408 require_clean_work_tree
"rebase" "Please commit or stash them."
410 if test -z "$rebase_root"
412 # The upstream head must be given. Make sure it is valid.
415 upstream
=`git rev-parse --verify "${upstream_name}^0"` ||
416 die
"invalid upstream $upstream_name"
418 upstream_arg
="$upstream_name"
420 test -z "$newbase" && die
"--root must be used with --onto"
424 upstream_arg
="$root_flag"
427 # Make sure the branch to rebase onto is valid.
428 onto_name
=${newbase-"$upstream_name"}
431 if left
=${onto_name%...*} right
=${onto_name#*...} &&
432 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
436 die
"$onto_name: there are more than one merge bases"
439 die
"$onto_name: there is no merge base"
443 die
"$onto_name: there is no merge base"
447 onto
=$
(git rev-parse
--verify "${onto_name}^0") ||
exit
451 # If a hook exists, give it a chance to interrupt
452 run_pre_rebase_hook
"$upstream_arg" "$@"
454 # If the branch to rebase is given, that is the branch we will rebase
455 # $branch_name -- branch being rebased, or HEAD (already detached)
456 # $orig_head -- commit object name of tip of the branch before rebasing
457 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
461 # Is it "rebase other $branchname" or "rebase other $commit"?
465 if git show-ref
--verify --quiet -- "refs/heads/$1" &&
466 branch
=$
(git rev-parse
-q --verify "refs/heads/$1")
468 head_name
="refs/heads/$1"
469 elif branch
=$
(git rev-parse
-q --verify "$1")
471 head_name
="detached HEAD"
473 echo >&2 "fatal: no such branch: $1"
478 # Do not need to switch branches, we are already on it.
479 if branch_name
=`git symbolic-ref -q HEAD`
481 head_name
=$branch_name
482 branch_name
=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
484 head_name
="detached HEAD"
485 branch_name
=HEAD
;# detached
487 branch
=$
(git rev-parse
--verify "${branch_name}^0") ||
exit
492 # Now we are rebasing commits $upstream..$branch (or with --root,
493 # everything leading up to $branch) on top of $onto
495 # Check if we are already based on $onto with linear history,
496 # but this should be done only when upstream and onto are the same.
497 mb
=$
(git merge-base
"$onto" "$branch")
498 if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
500 ! (git rev-list
--parents "$onto"..
"$branch" | sane_grep
" .* ") > /dev
/null
502 if test -z "$force_rebase"
504 # Lazily switch to the target branch if needed...
505 test -z "$switch_to" || git checkout
"$switch_to" --
506 say
"Current branch $branch_name is up to date."
509 say
"Current branch $branch_name is up to date, rebase forced."
513 # Detach HEAD and reset the tree
514 say
"First, rewinding head to replay your work on top of it..."
515 git checkout
-q "$onto^0" || die
"could not detach HEAD"
516 git update-ref ORIG_HEAD
$branch
518 if test -n "$diffstat"
520 if test -n "$verbose"
522 echo "Changes from $mb to $onto:"
524 # We want color (if set), but no pager
525 GIT_PAGER
='' git
diff --stat --summary "$mb" "$onto"
528 # If the $onto is a proper descendant of the tip of the branch, then
529 # we just fast-forwarded.
530 if test "$mb" = "$branch"
532 say
"Fast-forwarded $branch_name to $onto_name."
533 move_to_original_branch
537 if test -n "$rebase_root"
539 revisions
="$onto..$orig_head"
541 revisions
="$upstream..$orig_head"
544 if test -z "$do_merge"
546 git format-patch
-k --stdout --full-index --ignore-if-in-upstream \
547 --src-prefix=a
/ --dst-prefix=b
/ \
548 --no-renames $root_flag "$revisions" |
549 git am
$git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
550 move_to_original_branch
552 test 0 != $ret -a -d "$apply_dir" &&
553 echo $head_name > "$apply_dir/head-name" &&
554 echo $onto > "$apply_dir/onto" &&
555 echo $orig_head > "$apply_dir/orig-head" &&
556 echo "$GIT_QUIET" > "$apply_dir/quiet"
560 # start doing a rebase with git-merge
561 # this is rename-aware if the recursive (default) strategy is used
563 mkdir
-p "$merge_dir"
564 echo "$onto_name" > "$merge_dir/onto_name"
565 echo "$head_name" > "$merge_dir/head-name"
566 echo "$onto" > "$merge_dir/onto"
567 echo "$orig_head" > "$merge_dir/orig-head"
568 echo "$GIT_QUIET" > "$merge_dir/quiet"
571 for cmt
in `git rev-list --reverse --no-merges "$revisions"`
573 msgnum
=$
(($msgnum + 1))
574 echo "$cmt" > "$merge_dir/cmt.$msgnum"
577 echo 1 >"$merge_dir/msgnum"
578 echo $msgnum >"$merge_dir/end"
583 while test "$msgnum" -le "$end"