]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase.sh
glossary: define commit-ish (a.k.a. committish)
[thirdparty/git.git] / git-rebase.sh
CommitLineData
59e6b23a
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano.
4#
5
533b7039 6SUBDIRECTORY_OK=Yes
45e2acf3
MZ
7OPTIONS_KEEPDASHDASH=
8OPTIONS_SPEC="\
c2145384 9git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>]
0cd993a7 10git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
eb9a7cb4 11git-rebase --continue | --abort | --skip | --edit-todo
45e2acf3
MZ
12--
13 Available options are
14v,verbose! display a diffstat of what changed upstream
15q,quiet! be quiet. implies --no-stat
58794775 16autostash! automatically stash/stash pop before and after
45e2acf3
MZ
17onto=! rebase onto given branch instead of upstream
18p,preserve-merges! try to recreate merges instead of ignoring them
19s,strategy=! use the given merge strategy
20no-ff! cherry-pick all commits, even if unchanged
21m,merge! use merging strategies to rebase
22i,interactive! let the user edit the list of commits to rebase
c2145384 23x,exec=! add exec lines after each commit of the editable list
90e1818f 24k,keep-empty preserve empty commits during rebase
45e2acf3
MZ
25f,force-rebase! force rebase even if branch is up to date
26X,strategy-option=! pass the argument through to the merge strategy
27stat! display a diffstat of what changed upstream
28n,no-stat! do not show diffstat of what changed upstream
29verify allow pre-rebase hook to run
30rerere-autoupdate allow rerere to update index with resolved conflicts
31root! rebase all reachable commits up to the root(s)
32autosquash move commits that begin with squash!/fixup! under -i
33committer-date-is-author-date! passed to 'git am'
34ignore-date! passed to 'git am'
35whitespace=! passed to 'git apply'
36ignore-whitespace! passed to 'git apply'
37C=! passed to 'git apply'
38 Actions:
5960bc9d
MZ
39continue! continue
40abort! abort and check out the original branch
41skip! skip current patch and continue
eb9a7cb4 42edit-todo! edit the todo list during an interactive rebase
45e2acf3 43"
ae2b0f15 44. git-sh-setup
c7108bf9 45. git-sh-i18n
f9474132 46set_reflog_action rebase
035b5bf6 47require_work_tree_exists
533b7039 48cd_to_toplevel
4282c4fb 49
61dfa1bb
JH
50LF='
51'
6bb4e485
MZ
52ok_to_skip_pre_rebase=
53resolvemsg="
c7108bf9
JX
54$(gettext 'When you have resolved this problem, run "git rebase --continue".
55If you prefer to skip this patch, run "git rebase --skip" instead.
56To check out the original branch and stop rebasing, run "git rebase --abort".')
cc120056 57"
6bb4e485 58unset onto
c2145384 59cmd=
9765b6ab 60strategy=
93ce190c 61strategy_opts=
58634dbf 62do_merge=
69a636ad
MZ
63merge_dir="$GIT_DIR"/rebase-merge
64apply_dir="$GIT_DIR"/rebase-apply
b758789c 65verbose=
9474a029
MZ
66diffstat=
67test "$(git config --bool rebase.stat)" = true && diffstat=t
58794775 68autostash="$(git config --bool rebase.autostash || echo false)"
67dad687 69git_am_opt=
190f5323 70rebase_root=
b2f82e05 71force_rebase=
cb6020bb 72allow_rerere_autoupdate=
99de0640
MZ
73# Non-empty if a rebase was in progress when 'git rebase' was invoked
74in_progress=
75# One of {am, merge, interactive}
76type=
77# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
78state_dir=
34262322
MZ
79# One of {'', continue, skip, abort}, as parsed from command line
80action=
cf432ca0
MZ
81preserve_merges=
82autosquash=
90e1818f 83keep_empty=
cf432ca0 84test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
58634dbf 85
fa99c1e1 86read_basic_state () {
dc8ca912
RR
87 test -f "$state_dir/head-name" &&
88 test -f "$state_dir/onto" &&
02ac45fa
MZ
89 head_name=$(cat "$state_dir"/head-name) &&
90 onto=$(cat "$state_dir"/onto) &&
84df4560
MZ
91 # We always write to orig-head, but interactive rebase used to write to
92 # head. Fall back to reading from head to cover for the case that the
93 # user upgraded git with an ongoing interactive rebase.
94 if test -f "$state_dir"/orig-head
2959c283 95 then
2959c283 96 orig_head=$(cat "$state_dir"/orig-head)
84df4560
MZ
97 else
98 orig_head=$(cat "$state_dir"/head)
2959c283 99 fi &&
7b37a7c6
MZ
100 GIT_QUIET=$(cat "$state_dir"/quiet) &&
101 test -f "$state_dir"/verbose && verbose=t
80ff4795
MZ
102 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
103 test -f "$state_dir"/strategy_opts &&
104 strategy_opts="$(cat "$state_dir"/strategy_opts)"
b3e4847e
MZ
105 test -f "$state_dir"/allow_rerere_autoupdate &&
106 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
02ac45fa
MZ
107}
108
84df4560
MZ
109write_basic_state () {
110 echo "$head_name" > "$state_dir"/head-name &&
111 echo "$onto" > "$state_dir"/onto &&
112 echo "$orig_head" > "$state_dir"/orig-head &&
7b37a7c6
MZ
113 echo "$GIT_QUIET" > "$state_dir"/quiet &&
114 test t = "$verbose" && : > "$state_dir"/verbose
80ff4795
MZ
115 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
116 test -n "$strategy_opts" && echo "$strategy_opts" > \
117 "$state_dir"/strategy_opts
b3e4847e
MZ
118 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
119 "$state_dir"/allow_rerere_autoupdate
84df4560
MZ
120}
121
4974c2ca
MZ
122output () {
123 case "$verbose" in
124 '')
125 output=$("$@" 2>&1 )
126 status=$?
127 test $status != 0 && printf "%s\n" "$output"
128 return $status
129 ;;
130 *)
131 "$@"
132 ;;
133 esac
134}
135
6fd2f5e6 136move_to_original_branch () {
6fd2f5e6
JS
137 case "$head_name" in
138 refs/*)
139 message="rebase finished: $head_name onto $onto"
140 git update-ref -m "$message" \
141 $head_name $(git rev-parse HEAD) $orig_head &&
53f2ffa8
JK
142 git symbolic-ref \
143 -m "rebase finished: returning to $head_name" \
144 HEAD $head_name ||
c7108bf9 145 die "$(gettext "Could not move back to $head_name")"
6fd2f5e6
JS
146 ;;
147 esac
148}
149
58794775
RR
150finish_rebase () {
151 if test -f "$state_dir/autostash"
152 then
153 stash_sha1=$(cat "$state_dir/autostash")
154 if git stash apply $stash_sha1 2>&1 >/dev/null
155 then
156 echo "$(gettext 'Applied autostash.')"
157 else
20351bb0
RR
158 git stash store -m "autostash" -q $stash_sha1 ||
159 die "$(eval_gettext "Cannot store \$stash_sha1")"
58794775
RR
160 gettext 'Applying autostash resulted in conflicts.
161Your changes are safe in the stash.
ac1998de 162You can run "git stash pop" or "git stash drop" at any time.
58794775
RR
163'
164 fi
165 fi
166 git gc --auto &&
167 rm -rf "$state_dir"
168}
169
fa99c1e1 170run_specific_rebase () {
f8cca019
AE
171 if [ "$interactive_rebase" = implied ]; then
172 GIT_EDITOR=:
173 export GIT_EDITOR
8a6dae10 174 autosquash=
f8cca019 175 fi
46df82d5 176 . git-rebase--$type
f5f758a5
RR
177 ret=$?
178 if test $ret -eq 0
179 then
58794775 180 finish_rebase
f5f758a5
RR
181 fi
182 exit $ret
1b1dce4b
JS
183}
184
d70b4a8f 185run_pre_rebase_hook () {
6bb4e485 186 if test -z "$ok_to_skip_pre_rebase" &&
c4427656 187 test -x "$GIT_DIR/hooks/pre-rebase"
d70b4a8f 188 then
bc2bbc45 189 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
c7108bf9 190 die "$(gettext "The pre-rebase hook refused to rebase.")"
d70b4a8f
NS
191 fi
192}
193
69a636ad 194test -f "$apply_dir"/applying &&
c7108bf9 195 die "$(gettext "It looks like git-am is in progress. Cannot rebase.")"
9b752a6e 196
99de0640
MZ
197if test -d "$apply_dir"
198then
199 type=am
200 state_dir="$apply_dir"
201elif test -d "$merge_dir"
202then
203 if test -f "$merge_dir"/interactive
204 then
205 type=interactive
206 interactive_rebase=explicit
207 else
208 type=merge
209 fi
210 state_dir="$merge_dir"
211fi
212test -n "$type" && in_progress=t
213
95135b06 214total_argc=$#
822f7c73 215while test $# != 0
e646c9c8
JH
216do
217 case "$1" in
c4427656 218 --no-verify)
6bb4e485 219 ok_to_skip_pre_rebase=yes
c4427656 220 ;;
7baf9c4b 221 --verify)
6bb4e485 222 ok_to_skip_pre_rebase=
7baf9c4b 223 ;;
eb9a7cb4 224 --continue|--skip|--abort|--edit-todo)
45e2acf3 225 test $total_argc -eq 2 || usage
34262322 226 action=${1##--}
031321c6 227 ;;
e646c9c8
JH
228 --onto)
229 test 2 -le "$#" || usage
6bb4e485 230 onto="$2"
e646c9c8
JH
231 shift
232 ;;
c2145384
LK
233 -x)
234 test 2 -le "$#" || usage
235 cmd="${cmd}exec $2${LF}"
236 shift
237 ;;
45e2acf3 238 -i)
cf432ca0
MZ
239 interactive_rebase=explicit
240 ;;
90e1818f
NH
241 -k)
242 keep_empty=yes
243 ;;
45e2acf3 244 -p)
cf432ca0
MZ
245 preserve_merges=t
246 test -z "$interactive_rebase" && interactive_rebase=implied
247 ;;
248 --autosquash)
249 autosquash=t
250 ;;
251 --no-autosquash)
252 autosquash=
253 ;;
45e2acf3 254 -M|-m)
58634dbf
EW
255 do_merge=t
256 ;;
45e2acf3
MZ
257 -X)
258 shift
259 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$1")"
93ce190c 260 do_merge=t
9765b6ab 261 test -z "$strategy" && strategy=recursive
93ce190c 262 ;;
45e2acf3
MZ
263 -s)
264 shift
265 strategy="$1"
58634dbf
EW
266 do_merge=t
267 ;;
45e2acf3 268 -n)
a9c3821c
TAV
269 diffstat=
270 ;;
271 --stat)
272 diffstat=t
273 ;;
58794775
RR
274 --autostash)
275 autostash=true
276 ;;
45e2acf3 277 -v)
b758789c 278 verbose=t
a9c3821c 279 diffstat=t
0e987a12
SB
280 GIT_QUIET=
281 ;;
45e2acf3 282 -q)
0e987a12
SB
283 GIT_QUIET=t
284 git_am_opt="$git_am_opt -q"
285 verbose=
286 diffstat=
b758789c 287 ;;
45e2acf3
MZ
288 --whitespace)
289 shift
290 git_am_opt="$git_am_opt --whitespace=$1"
b2f82e05 291 case "$1" in
45e2acf3 292 fix|strip)
b2f82e05
SR
293 force_rebase=t
294 ;;
295 esac
059f446d 296 ;;
86c91f91
GB
297 --ignore-whitespace)
298 git_am_opt="$git_am_opt $1"
299 ;;
570ccad3
MB
300 --committer-date-is-author-date|--ignore-date)
301 git_am_opt="$git_am_opt $1"
302 force_rebase=t
303 ;;
45e2acf3
MZ
304 -C)
305 shift
306 git_am_opt="$git_am_opt -C$1"
67dad687 307 ;;
190f5323
TR
308 --root)
309 rebase_root=t
310 ;;
45e2acf3 311 -f|--no-ff)
b2f82e05
SR
312 force_rebase=t
313 ;;
cb6020bb
JH
314 --rerere-autoupdate|--no-rerere-autoupdate)
315 allow_rerere_autoupdate="$1"
316 ;;
45e2acf3
MZ
317 --)
318 shift
e646c9c8
JH
319 break
320 ;;
321 esac
322 shift
323done
51b2ead0 324test $# -gt 2 && usage
2db8aaec 325
c2145384
LK
326if test -n "$cmd" &&
327 test "$interactive_rebase" != explicit
328then
465d6a00 329 die "$(gettext "The --exec option must be used with the --interactive option")"
c2145384
LK
330fi
331
cf432ca0
MZ
332if test -n "$action"
333then
c7108bf9 334 test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
2959c283
MZ
335 # Only interactive rebase uses detailed reflog messages
336 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
337 then
338 GIT_REFLOG_ACTION="rebase -i ($action)"
339 export GIT_REFLOG_ACTION
340 fi
cf432ca0 341fi
34262322 342
eb9a7cb4
AW
343if test "$action" = "edit-todo" && test "$type" != "interactive"
344then
345 die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
346fi
347
34262322
MZ
348case "$action" in
349continue)
2959c283
MZ
350 # Sanity check
351 git rev-parse --verify HEAD >/dev/null ||
c7108bf9 352 die "$(gettext "Cannot read HEAD")"
34262322
MZ
353 git update-index --ignore-submodules --refresh &&
354 git diff-files --quiet --ignore-submodules || {
c7108bf9
JX
355 echo "$(gettext "You must edit all merge conflicts and then
356mark them as resolved using git add")"
34262322
MZ
357 exit 1
358 }
fa99c1e1
MZ
359 read_basic_state
360 run_specific_rebase
34262322
MZ
361 ;;
362skip)
4974c2ca 363 output git reset --hard HEAD || exit $?
fa99c1e1
MZ
364 read_basic_state
365 run_specific_rebase
34262322
MZ
366 ;;
367abort)
368 git rerere clear
fa99c1e1 369 read_basic_state
34262322
MZ
370 case "$head_name" in
371 refs/*)
ea69619c 372 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
c7108bf9 373 die "$(eval_gettext "Could not move back to \$head_name")"
34262322
MZ
374 ;;
375 esac
4974c2ca 376 output git reset --hard $orig_head
58794775 377 finish_rebase
34262322
MZ
378 exit
379 ;;
eb9a7cb4
AW
380edit-todo)
381 run_specific_rebase
382 ;;
34262322
MZ
383esac
384
99de0640
MZ
385# Make sure no rebase is in progress
386if test -n "$in_progress"
7f4bd5d8 387then
c7108bf9
JX
388 state_dir_base=${state_dir##*/}
389 cmd_live_rebase="git rebase (--continue | --abort | --skip)"
390 cmd_clear_stale_rebase="rm -fr \"$state_dir\""
391 die "
392$(eval_gettext 'It seems that there is already a $state_dir_base directory, and
e39beac6 393I wonder if you are in the middle of another rebase. If that is the
99de0640 394case, please try
c7108bf9 395 $cmd_live_rebase
99de0640 396If that is not the case, please
c7108bf9 397 $cmd_clear_stale_rebase
9b752a6e 398and run me again. I am stopping in case you still have something
c7108bf9 399valuable there.')"
7f4bd5d8
JH
400fi
401
df5df20c
CW
402if test -n "$rebase_root" && test -z "$onto"
403then
404 test -z "$interactive_rebase" && interactive_rebase=implied
405fi
406
cf432ca0
MZ
407if test -n "$interactive_rebase"
408then
409 type=interactive
410 state_dir="$merge_dir"
411elif test -n "$do_merge"
412then
413 type=merge
414 state_dir="$merge_dir"
415else
416 type=am
417 state_dir="$apply_dir"
418fi
419
190f5323
TR
420if test -z "$rebase_root"
421then
15a147e6
MZ
422 case "$#" in
423 0)
424 if ! upstream_name=$(git rev-parse --symbolic-full-name \
425 --verify -q @{upstream} 2>/dev/null)
426 then
427 . git-parse-remote
428 error_on_missing_default_upstream "rebase" "rebase" \
3c02396a 429 "against" "git rebase <branch>"
15a147e6
MZ
430 fi
431 ;;
432 *) upstream_name="$1"
433 shift
434 ;;
435 esac
2e6e276d 436 upstream=$(peel_committish "${upstream_name}") ||
c7108bf9 437 die "$(eval_gettext "invalid upstream \$upstream_name")"
190f5323
TR
438 upstream_arg="$upstream_name"
439else
df5df20c
CW
440 if test -z "$onto"
441 then
442 empty_tree=`git hash-object -t tree /dev/null`
443 onto=`git commit-tree $empty_tree </dev/null`
444 squash_onto="$onto"
445 fi
190f5323
TR
446 unset upstream_name
447 unset upstream
f2b6a199 448 test $# -gt 1 && usage
46df82d5 449 upstream_arg=--root
190f5323 450fi
32d99544 451
a1bf91e0 452# Make sure the branch to rebase onto is valid.
6bb4e485 453onto_name=${onto-"$upstream_name"}
9f21e97d
NS
454case "$onto_name" in
455*...*)
456 if left=${onto_name%...*} right=${onto_name#*...} &&
457 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
458 then
459 case "$onto" in
460 ?*"$LF"?*)
c7108bf9 461 die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
9f21e97d
NS
462 ;;
463 '')
c7108bf9 464 die "$(eval_gettext "\$onto_name: there is no merge base")"
9f21e97d
NS
465 ;;
466 esac
467 else
c7108bf9 468 die "$(eval_gettext "\$onto_name: there is no merge base")"
9f21e97d
NS
469 fi
470 ;;
471*)
2e6e276d 472 onto=$(peel_committish "$onto_name") ||
c7108bf9 473 die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
9f21e97d
NS
474 ;;
475esac
a1bf91e0 476
0cb06644
JH
477# If the branch to rebase is given, that is the branch we will rebase
478# $branch_name -- branch being rebased, or HEAD (already detached)
479# $orig_head -- commit object name of tip of the branch before rebasing
480# $head_name -- refs/heads/<that-branch> or "detached HEAD"
481switch_to=
59e6b23a 482case "$#" in
190f5323 4831)
0cb06644 484 # Is it "rebase other $branchname" or "rebase other $commit"?
190f5323
TR
485 branch_name="$1"
486 switch_to="$1"
0cb06644 487
190f5323 488 if git show-ref --verify --quiet -- "refs/heads/$1" &&
cb82a05d 489 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
0cb06644 490 then
190f5323 491 head_name="refs/heads/$1"
cb82a05d 492 elif orig_head=$(git rev-parse -q --verify "$1")
0cb06644
JH
493 then
494 head_name="detached HEAD"
495 else
c7108bf9 496 die "$(eval_gettext "fatal: no such branch: \$branch_name")"
0cb06644 497 fi
e646c9c8 498 ;;
f2b6a199 4990)
0cb06644 500 # Do not need to switch branches, we are already on it.
bcf31618
JH
501 if branch_name=`git symbolic-ref -q HEAD`
502 then
0cb06644 503 head_name=$branch_name
bcf31618
JH
504 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
505 else
0cb06644 506 head_name="detached HEAD"
bcf31618
JH
507 branch_name=HEAD ;# detached
508 fi
ea709800 509 orig_head=$(git rev-parse --verify HEAD) || exit
e646c9c8 510 ;;
f2b6a199
MZ
511*)
512 die "BUG: unexpected number of arguments left to parse"
513 ;;
59e6b23a
JH
514esac
515
58794775
RR
516if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
517then
518 stash_sha1=$(git stash create "autostash") ||
519 die "$(gettext 'Cannot autostash')"
520
521 mkdir -p "$state_dir" &&
522 echo $stash_sha1 >"$state_dir/autostash" &&
523 stash_abbrev=$(git rev-parse --short $stash_sha1) &&
524 echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
525 git reset --hard
526fi
527
c7108bf9 528require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
8f9bfb64 529
cb82a05d
MZ
530# Now we are rebasing commits $upstream..$orig_head (or with --root,
531# everything leading up to $orig_head) on top of $onto
e646c9c8 532
1308c17b 533# Check if we are already based on $onto with linear history,
cc1453e1
MZ
534# but this should be done only when upstream and onto are the same
535# and if this is not an interactive rebase.
cb82a05d 536mb=$(git merge-base "$onto" "$orig_head")
cc1453e1
MZ
537if test "$type" != interactive && test "$upstream" = "$onto" &&
538 test "$mb" = "$onto" &&
1308c17b 539 # linear history?
cb82a05d 540 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
7f4bd5d8 541then
b2f82e05
SR
542 if test -z "$force_rebase"
543 then
544 # Lazily switch to the target branch if needed...
4b03df21
RR
545 test -z "$switch_to" ||
546 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
547 git checkout "$switch_to" --
c7108bf9 548 say "$(eval_gettext "Current branch \$branch_name is up to date.")"
96e2b99e 549 finish_rebase
b2f82e05
SR
550 exit 0
551 else
c7108bf9 552 say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
b2f82e05 553 fi
7f4bd5d8
JH
554fi
555
8f9bfb64
MZ
556# If a hook exists, give it a chance to interrupt
557run_pre_rebase_hook "$upstream_arg" "$@"
558
a9c3821c
TAV
559if test -n "$diffstat"
560then
561 if test -n "$verbose"
562 then
c7108bf9 563 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
a9c3821c
TAV
564 fi
565 # We want color (if set), but no pager
566 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
567fi
568
fa99c1e1 569test "$type" = interactive && run_specific_rebase
f4107d9c
MZ
570
571# Detach HEAD and reset the tree
c7108bf9 572say "$(gettext "First, rewinding head to replay your work on top of it...")"
4b03df21
RR
573
574GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
575 git checkout -q "$onto^0" || die "could not detach HEAD"
cb82a05d 576git update-ref ORIG_HEAD $orig_head
f4107d9c 577
e646c9c8 578# If the $onto is a proper descendant of the tip of the branch, then
a75d7b54 579# we just fast-forwarded.
cb82a05d 580if test "$mb" = "$orig_head"
32d99544 581then
c7108bf9 582 say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
6fd2f5e6 583 move_to_original_branch
af2f0ebc 584 finish_rebase
32d99544
LS
585 exit 0
586fi
587
190f5323
TR
588if test -n "$rebase_root"
589then
590 revisions="$onto..$orig_head"
591else
592 revisions="$upstream..$orig_head"
593fi
594
fa99c1e1 595run_specific_rebase