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
53 test "$(git config --bool rebase.stat)" = true
&& diffstat
=t
57 allow_rerere_autoupdate
=
58 # Non-empty if a rebase was in progress when 'git rebase' was invoked
60 # One of {am, merge, interactive}
62 # One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
64 # One of {'', continue, skip, abort}, as parsed from command line
68 test "$(git config --bool rebase.autosquash)" = "true" && autosquash
=t
71 head_name
=$
(cat "$state_dir"/head-name
) &&
72 onto
=$
(cat "$state_dir"/onto
) &&
73 # We always write to orig-head, but interactive rebase used to write to
74 # head. Fall back to reading from head to cover for the case that the
75 # user upgraded git with an ongoing interactive rebase.
76 if test -f "$state_dir"/orig-head
78 orig_head
=$
(cat "$state_dir"/orig-head
)
80 orig_head
=$
(cat "$state_dir"/head)
82 GIT_QUIET
=$
(cat "$state_dir"/quiet
) &&
83 test -f "$state_dir"/verbose
&& verbose
=t
84 test -f "$state_dir"/strategy
&& strategy
="$(cat "$state_dir"/strategy)"
85 test -f "$state_dir"/strategy_opts
&&
86 strategy_opts
="$(cat "$state_dir"/strategy_opts)"
89 write_basic_state
() {
90 echo "$head_name" > "$state_dir"/head-name
&&
91 echo "$onto" > "$state_dir"/onto
&&
92 echo "$orig_head" > "$state_dir"/orig-head
&&
93 echo "$GIT_QUIET" > "$state_dir"/quiet
&&
94 test t
= "$verbose" && : > "$state_dir"/verbose
95 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
96 test -n "$strategy_opts" && echo "$strategy_opts" > \
97 "$state_dir"/strategy_opts
105 test $status != 0 && printf "%s\n" "$output"
114 move_to_original_branch
() {
117 message
="rebase finished: $head_name onto $onto"
118 git update-ref
-m "$message" \
119 $head_name $
(git rev-parse HEAD
) $orig_head &&
120 git symbolic-ref HEAD
$head_name ||
121 die
"Could not move back to $head_name"
126 run_specific_rebase
() {
127 if [ "$interactive_rebase" = implied
]; then
134 run_pre_rebase_hook
() {
135 if test -z "$ok_to_skip_pre_rebase" &&
136 test -x "$GIT_DIR/hooks/pre-rebase"
138 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
139 die
"The pre-rebase hook refused to rebase."
143 test -f "$apply_dir"/applying
&&
144 die
'It looks like git-am is in progress. Cannot rebase.'
146 if test -d "$apply_dir"
149 state_dir
="$apply_dir"
150 elif test -d "$merge_dir"
152 if test -f "$merge_dir"/interactive
155 interactive_rebase
=explicit
159 state_dir
="$merge_dir"
161 test -n "$type" && in_progress
=t
168 ok_to_skip_pre_rebase
=yes
171 ok_to_skip_pre_rebase
=
173 --continue|
--skip|
--abort)
174 test $total_argc -eq 1 || usage
178 test 2 -le "$#" || usage
183 interactive_rebase
=explicit
185 -p|
--preserve-merges)
187 test -z "$interactive_rebase" && interactive_rebase
=implied
195 -M|
-m|
--m|
--me|
--mer|
--merg|
--merge)
198 -X*|
--strategy-option*)
200 1,-X|
1,--strategy-option)
202 *,-X|
*,--strategy-option)
205 *,--strategy-option=*)
206 newopt
="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
208 newopt
="$(expr " $1" : ' -X\(.*\)')" ;;
212 strategy_opts
="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
214 test -z "$strategy" && strategy
=recursive
216 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
217 --strateg=*|
--strategy=*|\
218 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
221 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
243 git_am_opt
="$git_am_opt -q"
248 git_am_opt
="$git_am_opt $1"
250 --whitespace=fix|
--whitespace=strip
)
256 git_am_opt
="$git_am_opt $1"
258 --committer-date-is-author-date|
--ignore-date)
259 git_am_opt
="$git_am_opt $1"
263 git_am_opt
="$git_am_opt $1"
268 -f|
--f|
--fo|
--for|
--forc|
--force|
--force-r|
--force-re|
--force-reb|
--force-reba|
--force-rebas|
--force-rebase|
--no-ff)
271 --rerere-autoupdate|
--no-rerere-autoupdate)
272 allow_rerere_autoupdate
="$1"
283 test $# -gt 2 && usage
287 test -z "$in_progress" && die
"No rebase in progress?"
288 # Only interactive rebase uses detailed reflog messages
289 if test "$type" = interactive
&& test "$GIT_REFLOG_ACTION" = rebase
291 GIT_REFLOG_ACTION
="rebase -i ($action)"
292 export GIT_REFLOG_ACTION
299 git rev-parse
--verify HEAD
>/dev
/null ||
300 die
"Cannot read HEAD"
301 git update-index
--ignore-submodules --refresh &&
302 git diff-files
--quiet --ignore-submodules ||
{
303 echo "You must edit all merge conflicts and then"
304 echo "mark them as resolved using git add"
311 output git
reset --hard HEAD ||
exit $?
320 git symbolic-ref HEAD
$head_name ||
321 die
"Could not move back to $head_name"
324 output git
reset --hard $orig_head
330 # Make sure no rebase is in progress
331 if test -n "$in_progress"
334 It seems that there is already a '"${state_dir##*/}"' directory, and
335 I wonder if you are in the middle of another rebase. If that is the
337 git rebase (--continue | --abort | --skip)
338 If that is not the case, please
339 rm -fr '"$state_dir"'
340 and run me again. I am stopping in case you still have something
344 test $# -eq 0 && test -z "$rebase_root" && usage
346 if test -n "$interactive_rebase"
349 state_dir
="$merge_dir"
350 elif test -n "$do_merge"
353 state_dir
="$merge_dir"
356 state_dir
="$apply_dir"
359 if test -z "$rebase_root"
361 # The upstream head must be given. Make sure it is valid.
364 upstream
=`git rev-parse --verify "${upstream_name}^0"` ||
365 die
"invalid upstream $upstream_name"
366 upstream_arg
="$upstream_name"
368 test -z "$onto" && die
"You must specify --onto when using --root"
374 # Make sure the branch to rebase onto is valid.
375 onto_name
=${onto-"$upstream_name"}
378 if left
=${onto_name%...*} right
=${onto_name#*...} &&
379 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
383 die
"$onto_name: there are more than one merge bases"
386 die
"$onto_name: there is no merge base"
390 die
"$onto_name: there is no merge base"
394 onto
=$
(git rev-parse
--verify "${onto_name}^0") ||
395 die
"Does not point to a valid commit: $1"
399 # If the branch to rebase is given, that is the branch we will rebase
400 # $branch_name -- branch being rebased, or HEAD (already detached)
401 # $orig_head -- commit object name of tip of the branch before rebasing
402 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
406 # Is it "rebase other $branchname" or "rebase other $commit"?
410 if git show-ref
--verify --quiet -- "refs/heads/$1" &&
411 orig_head
=$
(git rev-parse
-q --verify "refs/heads/$1")
413 head_name
="refs/heads/$1"
414 elif orig_head
=$
(git rev-parse
-q --verify "$1")
416 head_name
="detached HEAD"
418 echo >&2 "fatal: no such branch: $1"
423 # Do not need to switch branches, we are already on it.
424 if branch_name
=`git symbolic-ref -q HEAD`
426 head_name
=$branch_name
427 branch_name
=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
429 head_name
="detached HEAD"
430 branch_name
=HEAD
;# detached
432 orig_head
=$
(git rev-parse
--verify "${branch_name}^0") ||
exit
436 require_clean_work_tree
"rebase" "Please commit or stash them."
438 # Now we are rebasing commits $upstream..$orig_head (or with --root,
439 # everything leading up to $orig_head) on top of $onto
441 # Check if we are already based on $onto with linear history,
442 # but this should be done only when upstream and onto are the same
443 # and if this is not an interactive rebase.
444 mb
=$
(git merge-base
"$onto" "$orig_head")
445 if test "$type" != interactive
&& test "$upstream" = "$onto" &&
446 test "$mb" = "$onto" &&
448 ! (git rev-list
--parents "$onto"..
"$orig_head" | sane_grep
" .* ") > /dev
/null
450 if test -z "$force_rebase"
452 # Lazily switch to the target branch if needed...
453 test -z "$switch_to" || git checkout
"$switch_to" --
454 say
"Current branch $branch_name is up to date."
457 say
"Current branch $branch_name is up to date, rebase forced."
461 # If a hook exists, give it a chance to interrupt
462 run_pre_rebase_hook
"$upstream_arg" "$@"
464 if test -n "$diffstat"
466 if test -n "$verbose"
468 echo "Changes from $mb to $onto:"
470 # We want color (if set), but no pager
471 GIT_PAGER
='' git
diff --stat --summary "$mb" "$onto"
474 test "$type" = interactive
&& run_specific_rebase
476 # Detach HEAD and reset the tree
477 say
"First, rewinding head to replay your work on top of it..."
478 git checkout
-q "$onto^0" || die
"could not detach HEAD"
479 git update-ref ORIG_HEAD
$orig_head
481 # If the $onto is a proper descendant of the tip of the branch, then
482 # we just fast-forwarded.
483 if test "$mb" = "$orig_head"
485 say
"Fast-forwarded $branch_name to $onto_name."
486 move_to_original_branch
490 if test -n "$rebase_root"
492 revisions
="$onto..$orig_head"
494 revisions
="$upstream..$orig_head"