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
=
59 # Non-empty if a rebase was in progress when 'git rebase' was invoked
61 # One of {am, merge, interactive}
63 # One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
67 if test "$type" = merge
69 onto_name
=$
(cat "$state_dir"/onto_name
) &&
70 end
=$
(cat "$state_dir"/end
) &&
71 msgnum
=$
(cat "$state_dir"/msgnum
)
73 head_name
=$
(cat "$state_dir"/head-name
) &&
74 onto
=$
(cat "$state_dir"/onto
) &&
75 orig_head
=$
(cat "$state_dir"/orig-head
) &&
76 GIT_QUIET
=$
(cat "$state_dir"/quiet
)
80 test -d "$merge_dir" || die
"$merge_dir directory does not exist"
82 unmerged
=$
(git ls-files
-u)
83 if test -n "$unmerged"
85 echo "You still have unmerged paths in your index"
86 echo "did you forget to use git add?"
90 cmt
=`cat "$merge_dir/current"`
91 if ! git diff-index
--quiet --ignore-submodules HEAD
--
93 if ! git commit
--no-verify -C "$cmt"
95 echo "Commit failed, please do not call \"git commit\""
96 echo "directly, but instead do one of the following: "
99 if test -z "$GIT_QUIET"
101 printf "Committed: %0${prec}d " $msgnum
103 echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
105 if test -z "$GIT_QUIET"
107 printf "Already applied: %0${prec}d " $msgnum
110 test -z "$GIT_QUIET" &&
111 GIT_PAGER
='' git log
--format=%s
-1 "$cmt"
113 # onto the next patch:
114 msgnum
=$
(($msgnum + 1))
115 echo "$msgnum" >"$merge_dir/msgnum"
119 cmt
="$(cat "$merge_dir/cmt.
$1")"
120 echo "$cmt" > "$merge_dir/current"
121 hd
=$
(git rev-parse
--verify HEAD
)
122 cmt_name
=$
(git symbolic-ref HEAD
2> /dev
/null ||
echo HEAD
)
123 msgnum
=$
(cat "$merge_dir/msgnum")
124 eval GITHEAD_
$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
125 eval GITHEAD_
$hd='$onto_name'
126 export GITHEAD_
$cmt GITHEAD_
$hd
127 if test -n "$GIT_QUIET"
129 GIT_MERGE_VERBOSITY
=1 && export GIT_MERGE_VERBOSITY
131 eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
135 unset GITHEAD_
$cmt GITHEAD_
$hd
139 git rerere
$allow_rerere_autoupdate
143 echo "Strategy: $rv $strategy failed, try another" 1>&2
147 die
"Unknown exit code ($rv) from command:" \
148 "git-merge-$strategy $cmt^ -- HEAD $cmt"
153 move_to_original_branch
() {
156 message
="rebase finished: $head_name onto $onto"
157 git update-ref
-m "$message" \
158 $head_name $
(git rev-parse HEAD
) $orig_head &&
159 git symbolic-ref HEAD
$head_name ||
160 die
"Could not move back to $head_name"
166 move_to_original_branch
167 git notes copy
--for-rewrite=rebase
< "$merge_dir"/rewritten
168 if test -x "$GIT_DIR"/hooks
/post-rewrite
&&
169 test -s "$merge_dir"/rewritten
; then
170 "$GIT_DIR"/hooks
/post-rewrite rebase
< "$merge_dir"/rewritten
181 interactive_rebase
=explicit
184 -p|
--preserve-merges)
185 interactive_rebase
=implied
191 if [ "$interactive_rebase" = implied
]; then
196 test -n "$interactive_rebase" ||
test -f "$merge_dir"/interactive
199 run_pre_rebase_hook
() {
200 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
201 test -x "$GIT_DIR/hooks/pre-rebase"
203 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
204 die
"The pre-rebase hook refused to rebase."
208 test -f "$apply_dir"/applying
&&
209 die
'It looks like git-am is in progress. Cannot rebase.'
211 is_interactive
"$@" && exec git-rebase--interactive
"$@"
213 if test -d "$apply_dir"
216 state_dir
="$apply_dir"
217 elif test -d "$merge_dir"
219 if test -f "$merge_dir"/interactive
222 interactive_rebase
=explicit
226 state_dir
="$merge_dir"
228 test -n "$type" && in_progress
=t
234 OK_TO_SKIP_PRE_REBASE
=yes
237 OK_TO_SKIP_PRE_REBASE
=
240 test -z "$in_progress" && die
"No rebase in progress?"
242 git update-index
--ignore-submodules --refresh &&
243 git diff-files
--quiet --ignore-submodules ||
{
244 echo "You must edit all merge conflicts and then"
245 echo "mark them as resolved using git add"
249 if test -d "$merge_dir"
252 while test "$msgnum" -le "$end"
260 git am
--resolved --3way --resolvemsg="$RESOLVEMSG" &&
261 move_to_original_branch
265 test -z "$in_progress" && die
"No rebase in progress?"
267 git
reset --hard HEAD ||
exit $?
269 if test -d "$merge_dir"
272 msgnum
=$
(($msgnum + 1))
273 while test "$msgnum" -le "$end"
281 git am
-3 --skip --resolvemsg="$RESOLVEMSG" &&
282 move_to_original_branch
286 test -z "$in_progress" && die
"No rebase in progress?"
292 git symbolic-ref HEAD
$head_name ||
293 die
"Could not move back to $head_name"
296 git
reset --hard $orig_head
301 test 2 -le "$#" || usage
305 -M|
-m|
--m|
--me|
--mer|
--merg|
--merge)
308 -X*|
--strategy-option*)
310 1,-X|
1,--strategy-option)
312 *,-X|
*,--strategy-option)
315 *,--strategy-option=*)
316 newopt
="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
318 newopt
="$(expr " $1" : ' -X\(.*\)')" ;;
322 strategy_opts
="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
325 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
326 --strateg=*|
--strategy=*|\
327 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
330 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
352 git_am_opt
="$git_am_opt -q"
357 git_am_opt
="$git_am_opt $1"
359 --whitespace=fix|
--whitespace=strip
)
365 git_am_opt
="$git_am_opt $1"
367 --committer-date-is-author-date|
--ignore-date)
368 git_am_opt
="$git_am_opt $1"
372 git_am_opt
="$git_am_opt $1"
377 -f|
--f|
--fo|
--for|
--forc|
--force|
--force-r|
--force-re|
--force-reb|
--force-reba|
--force-rebas|
--force-rebase|
--no-ff)
380 --rerere-autoupdate|
--no-rerere-autoupdate)
381 allow_rerere_autoupdate
="$1"
392 test $# -gt 2 && usage
394 # Make sure no rebase is in progress
395 if test -n "$in_progress"
398 It seems that there is already a '"${state_dir##*/}"' directory, and
399 I wonder if you are in the middle of another rebase. If that is the
401 git rebase (--continue | --abort | --skip)
402 If that is not the case, please
403 rm -fr '"$state_dir"'
404 and run me again. I am stopping in case you still have something
408 test $# -eq 0 && test -z "$rebase_root" && usage
410 require_clean_work_tree
"rebase" "Please commit or stash them."
412 if test -z "$rebase_root"
414 # The upstream head must be given. Make sure it is valid.
417 upstream
=`git rev-parse --verify "${upstream_name}^0"` ||
418 die
"invalid upstream $upstream_name"
420 upstream_arg
="$upstream_name"
422 test -z "$newbase" && die
"--root must be used with --onto"
426 upstream_arg
="$root_flag"
429 # Make sure the branch to rebase onto is valid.
430 onto_name
=${newbase-"$upstream_name"}
433 if left
=${onto_name%...*} right
=${onto_name#*...} &&
434 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
438 die
"$onto_name: there are more than one merge bases"
441 die
"$onto_name: there is no merge base"
445 die
"$onto_name: there is no merge base"
449 onto
=$
(git rev-parse
--verify "${onto_name}^0") ||
exit
453 # If a hook exists, give it a chance to interrupt
454 run_pre_rebase_hook
"$upstream_arg" "$@"
456 # If the branch to rebase is given, that is the branch we will rebase
457 # $branch_name -- branch being rebased, or HEAD (already detached)
458 # $orig_head -- commit object name of tip of the branch before rebasing
459 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
463 # Is it "rebase other $branchname" or "rebase other $commit"?
467 if git show-ref
--verify --quiet -- "refs/heads/$1" &&
468 branch
=$
(git rev-parse
-q --verify "refs/heads/$1")
470 head_name
="refs/heads/$1"
471 elif branch
=$
(git rev-parse
-q --verify "$1")
473 head_name
="detached HEAD"
475 echo >&2 "fatal: no such branch: $1"
480 # Do not need to switch branches, we are already on it.
481 if branch_name
=`git symbolic-ref -q HEAD`
483 head_name
=$branch_name
484 branch_name
=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
486 head_name
="detached HEAD"
487 branch_name
=HEAD
;# detached
489 branch
=$
(git rev-parse
--verify "${branch_name}^0") ||
exit
494 # Now we are rebasing commits $upstream..$branch (or with --root,
495 # everything leading up to $branch) on top of $onto
497 # Check if we are already based on $onto with linear history,
498 # but this should be done only when upstream and onto are the same.
499 mb
=$
(git merge-base
"$onto" "$branch")
500 if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
502 ! (git rev-list
--parents "$onto"..
"$branch" | sane_grep
" .* ") > /dev
/null
504 if test -z "$force_rebase"
506 # Lazily switch to the target branch if needed...
507 test -z "$switch_to" || git checkout
"$switch_to" --
508 say
"Current branch $branch_name is up to date."
511 say
"Current branch $branch_name is up to date, rebase forced."
515 # Detach HEAD and reset the tree
516 say
"First, rewinding head to replay your work on top of it..."
517 git checkout
-q "$onto^0" || die
"could not detach HEAD"
518 git update-ref ORIG_HEAD
$branch
520 if test -n "$diffstat"
522 if test -n "$verbose"
524 echo "Changes from $mb to $onto:"
526 # We want color (if set), but no pager
527 GIT_PAGER
='' git
diff --stat --summary "$mb" "$onto"
530 # If the $onto is a proper descendant of the tip of the branch, then
531 # we just fast-forwarded.
532 if test "$mb" = "$branch"
534 say
"Fast-forwarded $branch_name to $onto_name."
535 move_to_original_branch
539 if test -n "$rebase_root"
541 revisions
="$onto..$orig_head"
543 revisions
="$upstream..$orig_head"
546 if test -z "$do_merge"
548 git format-patch
-k --stdout --full-index --ignore-if-in-upstream \
549 --src-prefix=a
/ --dst-prefix=b
/ \
550 --no-renames $root_flag "$revisions" |
551 git am
$git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
552 move_to_original_branch
554 test 0 != $ret -a -d "$apply_dir" &&
555 echo $head_name > "$apply_dir/head-name" &&
556 echo $onto > "$apply_dir/onto" &&
557 echo $orig_head > "$apply_dir/orig-head" &&
558 echo "$GIT_QUIET" > "$apply_dir/quiet"
562 # start doing a rebase with git-merge
563 # this is rename-aware if the recursive (default) strategy is used
565 mkdir
-p "$merge_dir"
566 echo "$onto_name" > "$merge_dir/onto_name"
567 echo "$head_name" > "$merge_dir/head-name"
568 echo "$onto" > "$merge_dir/onto"
569 echo "$orig_head" > "$merge_dir/orig-head"
570 echo "$GIT_QUIET" > "$merge_dir/quiet"
573 for cmt
in `git rev-list --reverse --no-merges "$revisions"`
575 msgnum
=$
(($msgnum + 1))
576 echo "$cmt" > "$merge_dir/cmt.$msgnum"
579 echo 1 >"$merge_dir/msgnum"
580 echo $msgnum >"$merge_dir/end"
585 while test "$msgnum" -le "$end"