3 # Copyright (c) 2006 Johannes E. Schindelin
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
15 git-rebase [-i] [options] [--] <upstream> [<branch>]
16 git-rebase [-i] (--continue | --abort | --skip)
19 v,verbose display a diffstat of what changed upstream
20 onto= rebase onto given branch instead of upstream
21 p,preserve-merges try to recreate merges instead of ignoring them
22 s,strategy= use the given merge strategy
23 no-ff cherry-pick all commits, even if unchanged
24 m,merge always used (no-op)
25 i,interactive always used (no-op)
27 continue continue rebasing process
28 abort abort rebasing process and restore original branch
29 skip skip current patch and continue rebasing process
30 no-verify override pre-rebase hook from stopping the operation
31 verify allow pre-rebase hook to run
32 root rebase all reachable commmits up to the root(s)
33 autosquash move commits that begin with squash!/fixup! under -i
39 DOTEST
="$GIT_DIR/rebase-merge"
41 # The file containing rebase commands, comments, and empty lines.
42 # This file is created by "git rebase -i" then edited by the user. As
43 # the lines are processed, they are removed from the front of this
44 # file and written to the tail of $DONE.
45 TODO
="$DOTEST"/git-rebase-todo
47 # The rebase command lines that have already been processed. A line
48 # is moved here when it is first handled, before any associated user
52 # The commit message that is planned to be used for any changes that
53 # need to be committed following a user interaction.
56 # The file into which is accumulated the suggested commit message for
57 # squash/fixup commands. When the first of a series of squash/fixups
58 # is seen, the file is created and the commit message from the
59 # previous commit and from the first squash/fixup commit are written
60 # to it. The commit message for each subsequent squash/fixup commit
61 # is appended to the file as it is processed.
63 # The first line of the file is of the form
64 # # This is a combination of $COUNT commits.
65 # where $COUNT is the number of commits whose messages have been
66 # written to the file so far (including the initial "pick" commit).
67 # Each time that a commit message is processed, this line is read and
68 # updated. It is deleted just before the combined commit is made.
69 SQUASH_MSG
="$DOTEST"/message-squash
71 # If the current series of squash/fixups has not yet included a squash
72 # command, then this file exists and holds the commit message of the
73 # original "pick" commit. (If the series ends without a "squash"
74 # command, then this can be used as the commit message of the combined
75 # commit without opening the editor.)
76 FIXUP_MSG
="$DOTEST"/message-fixup
78 # $REWRITTEN is the name of a directory containing files for each
79 # commit that is reachable by at least one merge base of $HEAD and
80 # $UPSTREAM. They are not necessarily rewritten, but their children
81 # might be. This ensures that commits on merged, but otherwise
82 # unrelated side branches are left alone. (Think "X" in the man page's
84 REWRITTEN
="$DOTEST"/rewritten
86 DROPPED
="$DOTEST"/dropped
88 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
89 # GIT_AUTHOR_DATE that will be used for the commit that is currently
91 AUTHOR_SCRIPT
="$DOTEST"/author-script
93 # When an "edit" rebase command is being processed, the SHA1 of the
94 # commit to be edited is recorded in this file. When "git rebase
95 # --continue" is executed, if there are any staged changes then they
96 # will be amended to the HEAD commit, but only provided the HEAD
97 # commit is still the commit to be edited. When any other rebase
98 # command is processed, this file is deleted.
101 # For the post-rewrite hook, we make a list of rewritten commits and
102 # their new sha1s. The rewritten-pending list keeps the sha1s of
103 # commits that have been processed, but not committed yet,
104 # e.g. because they are waiting for a 'squash' command.
105 REWRITTEN_LIST
="$DOTEST"/rewritten-list
106 REWRITTEN_PENDING
="$DOTEST"/rewritten-pending
112 OK_TO_SKIP_PRE_REBASE
=
115 test "$(git config --bool rebase.autosquash)" = "true" && AUTOSQUASH
=t
118 GIT_CHERRY_PICK_HELP
="\
119 hint: after resolving the conflicts, mark the corrected paths
120 hint: with 'git add <paths>' and run 'git rebase --continue'"
121 export GIT_CHERRY_PICK_HELP
124 printf '%s\n' "$*" >&2
132 test $status != 0 && printf "%s\n" "$output"
141 # Output the commit message for the specified commit.
143 git cat-file commit
"$1" |
sed "1,/^$/d"
146 run_pre_rebase_hook
() {
147 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
148 test -x "$GIT_DIR/hooks/pre-rebase"
150 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
{
151 echo >&2 "The pre-rebase hook refused to rebase."
158 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
160 comment_for_reflog
() {
161 case "$ORIG_REFLOG_ACTION" in
163 GIT_REFLOG_ACTION
="rebase -i ($1)"
164 export GIT_REFLOG_ACTION
170 mark_action_done
() {
171 sed -e 1q
< "$TODO" >> "$DONE"
172 sed -e 1d
< "$TODO" >> "$TODO".new
173 mv -f "$TODO".new
"$TODO"
174 count
=$
(sane_grep
-c '^[^#]' < "$DONE")
175 total
=$
(($count+$
(sane_grep
-c '^[^#]' < "$TODO")))
176 if test "$last_count" != "$count"
179 printf "Rebasing (%d/%d)\r" $count $total
180 test -z "$VERBOSE" ||
echo
185 sha1_and_parents
="$(git rev-list --parents -1 "$1")"
186 case "$sha1_and_parents" in
188 git
diff --cc $sha1_and_parents
191 git diff-tree
-p "$1^!"
196 esac > "$DOTEST"/patch
198 commit_message
"$1" > "$MSG"
199 test -f "$AUTHOR_SCRIPT" ||
200 get_author_ident_from_commit
"$1" > "$AUTHOR_SCRIPT"
204 echo "$1" > "$DOTEST"/stopped-sha
216 sane_grep
'^[^#]' "$1" >/dev
/null
219 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
220 # GIT_AUTHOR_DATE exported from the current environment.
223 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
230 case "$1" in -n) sha1
=$2; ff
= ;; *) sha1
=$1 ;; esac
231 case "$NEVER_FF" in '') ;; ?
*) ff
= ;; esac
232 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
233 test -d "$REWRITTEN" &&
234 pick_one_preserving_merges
"$@" && return
235 if test -n "$REBASE_ROOT"
237 output git cherry-pick
"$@"
240 output git cherry-pick
$ff "$@"
243 pick_one_preserving_merges
() {
254 sha1
=$
(git rev-parse
$sha1)
256 if test -f "$DOTEST"/current-commit
258 if test "$fast_forward" = t
260 while read current_commit
262 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
263 done <"$DOTEST"/current-commit
264 rm "$DOTEST"/current-commit ||
265 die
"Cannot write current commit's replacement sha1"
269 echo $sha1 >> "$DOTEST"/current-commit
271 # rewrite parents; if none were rewritten, we can fast-forward.
273 pend
=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
274 if test "$pend" = " "
278 while [ "$pend" != "" ]
280 p
=$
(expr "$pend" : ' \([^ ]*\)')
283 if test -f "$REWRITTEN"/$p
285 new_p
=$
(cat "$REWRITTEN"/$p)
287 # If the todo reordered commits, and our parent is marked for
288 # rewriting, but hasn't been gotten to yet, assume the user meant to
289 # drop it on top of the current HEAD
292 new_p
=$
(git rev-parse HEAD
)
295 test $p != $new_p && fast_forward
=f
296 case "$new_parents" in
298 ;; # do nothing; that parent is already there
300 new_parents
="$new_parents $new_p"
304 if test -f "$DROPPED"/$p
307 replacement
="$(cat "$DROPPED"/$p)"
308 test -z "$replacement" && replacement
=root
309 pend
=" $replacement$pend"
311 new_parents
="$new_parents $p"
315 case $fast_forward in
317 output warn
"Fast-forward to $sha1"
318 output git
reset --hard $sha1 ||
319 die
"Cannot fast-forward to $sha1"
322 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
326 # detach HEAD to current parent
327 output git checkout
$first_parent 2> /dev
/null ||
328 die
"Cannot move HEAD to $first_parent"
331 case "$new_parents" in
333 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
336 author_script
=$
(get_author_ident_from_commit
$sha1)
337 eval "$author_script"
338 msg
="$(commit_message $sha1)"
339 # No point in merging the first parent, that's HEAD
340 new_parents
=${new_parents# $first_parent}
341 if ! do_with_author output \
342 git merge
$STRATEGY -m "$msg" $new_parents
344 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
345 die_with_patch
$sha1 "Error redoing merge $sha1"
347 echo "$sha1 $(git rev-parse HEAD^0)" >> "$REWRITTEN_LIST"
350 output git cherry-pick
"$@" ||
351 die_with_patch
$sha1 "Could not pick $sha1"
360 *1[0-9]|
*[04-9]) echo "$1"th
;;
367 update_squash_messages
() {
368 if test -f "$SQUASH_MSG"; then
369 mv "$SQUASH_MSG" "$SQUASH_MSG".bak ||
exit
371 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
372 -e "q" < "$SQUASH_MSG".bak
)+1))
374 echo "# This is a combination of $COUNT commits."
375 sed -e 1d
-e '2,/^./{
377 }' <"$SQUASH_MSG".bak
380 commit_message HEAD
> "$FIXUP_MSG" || die
"Cannot write $FIXUP_MSG"
383 echo "# This is a combination of 2 commits."
384 echo "# The first commit's message is:"
393 echo "# This is the $(nth_string $COUNT) commit message:"
399 echo "# The $(nth_string $COUNT) commit message will be skipped:"
401 commit_message
$2 |
sed -e 's/^/# /'
406 peek_next_command
() {
407 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
410 # A squash/fixup has failed. Prepare the long version of the squash
411 # commit message, then die_with_patch. This code path requires the
412 # user to edit the combined commit message for all commits that have
413 # been squashed/fixedup so far. So also erase the old squash
414 # messages, effectively causing the combined commit to be used as the
415 # new basis for any further squash/fixups. Args: sha1 rest
416 die_failed_squash
() {
417 mv "$SQUASH_MSG" "$MSG" ||
exit
419 cp "$MSG" "$GIT_DIR"/MERGE_MSG ||
exit
421 warn
"Could not apply $1... $2"
425 flush_rewritten_pending
() {
426 test -s "$REWRITTEN_PENDING" ||
return
427 newsha1
="$(git rev-parse HEAD^0)"
428 sed "s/$/ $newsha1/" < "$REWRITTEN_PENDING" >> "$REWRITTEN_LIST"
429 rm -f "$REWRITTEN_PENDING"
432 record_in_rewritten
() {
433 oldsha1
="$(git rev-parse $1)"
434 echo "$oldsha1" >> "$REWRITTEN_PENDING"
436 case "$(peek_next_command)" in
440 flush_rewritten_pending
446 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" ||
exit
447 read -r command sha1 rest
< "$TODO"
453 comment_for_reflog pick
457 die_with_patch
$sha1 "Could not apply $sha1... $rest"
458 record_in_rewritten
$sha1
461 comment_for_reflog reword
465 die_with_patch
$sha1 "Could not apply $sha1... $rest"
466 git commit
--amend --no-post-rewrite
467 record_in_rewritten
$sha1
470 comment_for_reflog edit
474 die_with_patch
$sha1 "Could not apply $sha1... $rest"
475 echo "$sha1" > "$DOTEST"/stopped-sha
477 git rev-parse
--verify HEAD
> "$AMEND"
478 warn
"Stopped at $sha1... $rest"
479 warn
"You can amend the commit now, with"
481 warn
" git commit --amend"
483 warn
"Once you are satisfied with your changes, run"
485 warn
" git rebase --continue"
498 comment_for_reflog
$squash_style
500 test -f "$DONE" && has_action
"$DONE" ||
501 die
"Cannot '$squash_style' without a previous commit"
504 update_squash_messages
$squash_style $sha1
505 author_script
=$
(get_author_ident_from_commit HEAD
)
506 echo "$author_script" > "$AUTHOR_SCRIPT"
507 eval "$author_script"
508 output git
reset --soft HEAD^
509 pick_one
-n $sha1 || die_failed_squash
$sha1 "$rest"
510 case "$(peek_next_command)" in
512 # This is an intermediate commit; its message will only be
513 # used in case of trouble. So use the long version:
514 do_with_author output git commit
--no-verify -F "$SQUASH_MSG" ||
515 die_failed_squash
$sha1 "$rest"
518 # This is the final command of this squash/fixup group
519 if test -f "$FIXUP_MSG"
521 do_with_author git commit
--no-verify -F "$FIXUP_MSG" ||
522 die_failed_squash
$sha1 "$rest"
524 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG ||
exit
525 rm -f "$GIT_DIR"/MERGE_MSG
526 do_with_author git commit
--no-verify -e ||
527 die_failed_squash
$sha1 "$rest"
529 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
532 record_in_rewritten
$sha1
535 read -r command rest
< "$TODO"
537 printf 'Executing: %s\n' "$rest"
538 # "exec" command doesn't take a sha1 in the todo-list.
539 # => can't just use $sha1 here.
540 git rev-parse
--verify HEAD
> "$DOTEST"/stopped-sha
541 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
543 if test "$status" -ne 0
545 warn
"Execution failed: $rest"
546 warn
"You can fix the problem, and then run"
548 warn
" git rebase --continue"
552 # Run in subshell because require_clean_work_tree can die.
553 if ! (require_clean_work_tree
"rebase")
555 warn
"Commit or stash your changes, and then run"
557 warn
" git rebase --continue"
563 warn
"Unknown command: $command $sha1 $rest"
564 if git rev-parse
--verify -q "$sha1" >/dev
/null
566 die_with_patch
$sha1 "Please fix this in the file $TODO."
568 die
"Please fix this in the file $TODO."
572 test -s "$TODO" && return
574 comment_for_reflog finish
&&
575 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
576 OLDHEAD
=$
(cat "$DOTEST"/head) &&
577 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
578 NEWHEAD
=$
(git rev-parse HEAD
) &&
581 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
582 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
583 git symbolic-ref HEAD
$HEADNAME
586 test ! -f "$DOTEST"/verbose ||
587 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
590 test -s "$REWRITTEN_LIST" &&
591 git notes copy
--for-rewrite=rebase
< "$REWRITTEN_LIST" ||
592 true
# we don't care if this copying failed
594 if test -x "$GIT_DIR"/hooks
/post-rewrite
&&
595 test -s "$REWRITTEN_LIST"; then
596 "$GIT_DIR"/hooks
/post-rewrite rebase
< "$REWRITTEN_LIST"
597 true
# we don't care if this hook failed
601 warn
"Successfully rebased and updated $HEADNAME."
613 # skip picking commits whose parents are unchanged
614 skip_unnecessary_picks
() {
616 while read -r command rest
618 # fd=3 means we skip the command
619 case "$fd,$command" in
621 # pick a commit whose parent is current $ONTO -> skip
623 case "$(git rev-parse --verify --quiet "$sha1"^)" in
639 printf '%s\n' "$command${rest:+ }$rest" >&$fd
640 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
641 mv -f "$TODO".new
"$TODO" &&
642 case "$(peek_next_command)" in
644 record_in_rewritten
"$ONTO"
647 die
"Could not skip unnecessary pick commands"
650 # check if no other options are set
652 test $# -eq 2 -a "$2" = '--' &&
654 test -z "$PRESERVE_MERGES" &&
655 test -z "$STRATEGY" &&
659 get_saved_options
() {
660 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
661 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
662 test -f "$DOTEST"/verbose
&& VERBOSE
=t
663 test -f "$DOTEST"/rebase-root
&& REBASE_ROOT
=t
666 # Rearrange the todo list that has both "pick sha1 msg" and
667 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
668 # comes immediately after the former, and change "pick" to
670 rearrange_squash
() {
671 # extract fixup!/squash! lines and resolve any referenced sha1's
672 while read -r pick sha1 message
675 "squash! "*|
"fixup! "*)
676 action
="${message%%!*}"
677 rest
="${message#*! }"
678 echo "$sha1 $action $rest"
679 # if it's a single word, try to resolve to a full sha1 and
680 # emit a second copy. This allows us to match on both message
682 if test "${rest#* }" = "$rest"; then
683 fullsha
="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
684 if test -n "$fullsha"; then
685 # prefix the action to uniquely identify this line as
686 # intended for full sha1 match
687 echo "$sha1 +$action $fullsha"
692 test -s "$1.sq" ||
return
695 while read -r pick sha1 message
698 *" $sha1 "*) continue ;;
700 printf '%s\n' "$pick $sha1 $message"
702 while read -r squash action msg
705 *" $squash "*) continue ;;
711 # full sha1 prefix test
712 case "$msg" in "$sha1"*) emit
=1;; esac ;;
714 # message prefix test
715 case "$message" in "$msg"*) emit
=1;; esac ;;
717 if test $emit = 1; then
718 printf '%s\n' "$action $squash $action! $msg"
722 done >"$1.rearranged" <"$1"
723 cat "$1.rearranged" >"$1"
724 rm -f "$1.sq" "$1.rearranged"
732 if left
=${1%...*} right
=${1#*...} &&
733 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
743 git rev-parse
--verify "$1^0"
750 OK_TO_SKIP_PRE_REBASE
=yes
753 OK_TO_SKIP_PRE_REBASE
=
756 is_standalone
"$@" || usage
758 comment_for_reflog
continue
760 test -d "$DOTEST" || die
"No interactive rebase running"
763 git rev-parse
--verify HEAD
>/dev
/null ||
764 die
"Cannot read HEAD"
765 git update-index
--ignore-submodules --refresh &&
766 git diff-files
--quiet --ignore-submodules ||
767 die
"Working tree is dirty"
769 # do we have anything to commit?
770 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
772 : Nothing to commit
-- skip this
774 .
"$AUTHOR_SCRIPT" ||
775 die
"Cannot find the author identity"
779 amend
=$
(git rev-parse
--verify HEAD
)
780 test "$amend" = $
(cat "$AMEND") ||
782 You have uncommitted changes in your working tree. Please, commit them
783 first and then run 'git rebase --continue' again."
784 git
reset --soft HEAD^ ||
785 die
"Cannot rewind the HEAD"
787 do_with_author git commit
--no-verify -F "$MSG" -e ||
{
788 test -n "$amend" && git
reset --soft $amend
789 die
"Could not commit staged changes."
793 record_in_rewritten
"$(cat "$DOTEST"/stopped-sha)"
795 require_clean_work_tree
"rebase"
799 is_standalone
"$@" || usage
801 comment_for_reflog abort
804 test -d "$DOTEST" || die
"No interactive rebase running"
806 HEADNAME
=$
(cat "$DOTEST"/head-name
)
807 HEAD
=$
(cat "$DOTEST"/head)
810 git symbolic-ref HEAD
$HEADNAME
813 output git
reset --hard $HEAD &&
818 is_standalone
"$@" || usage
820 comment_for_reflog skip
823 test -d "$DOTEST" || die
"No interactive rebase running"
825 output git
reset --hard && do_rest
830 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
839 # we use merge anyway
864 ONTO
=$
(parse_onto
"$1") ||
865 die
"Does not point to a valid commit: $1"
869 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
870 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
872 die
"Interactive rebase already started"
874 git var GIT_COMMITTER_IDENT
>/dev
/null ||
875 die
"You need to set your committer info first"
877 if test -z "$REBASE_ROOT"
880 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
881 test -z "$ONTO" && ONTO
=$UPSTREAM
887 die
"You must specify --onto when using --root"
889 run_pre_rebase_hook
"$UPSTREAM_ARG" "$@"
891 comment_for_reflog start
893 require_clean_work_tree
"rebase" "Please commit or stash them."
897 output git checkout
"$1" ||
898 die
"Could not checkout $1"
901 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
902 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
904 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
905 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
906 echo "detached HEAD" > "$DOTEST"/head-name
908 echo $HEAD > "$DOTEST"/head
909 case "$REBASE_ROOT" in
911 rm -f "$DOTEST"/rebase-root
;;
913 : >"$DOTEST"/rebase-root
;;
915 echo $ONTO > "$DOTEST"/onto
916 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
917 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
918 if test t
= "$PRESERVE_MERGES"
920 if test -z "$REBASE_ROOT"
922 mkdir
"$REWRITTEN" &&
923 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
925 echo $ONTO > "$REWRITTEN"/$c ||
926 die
"Could not init rewritten commits"
929 mkdir
"$REWRITTEN" &&
930 echo $ONTO > "$REWRITTEN"/root ||
931 die
"Could not init rewritten commits"
933 # No cherry-pick because our first pass is to determine
934 # parents to rewrite and skipping dropped commits would
935 # prematurely end our probe
937 first_after_upstream
="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
939 MERGES_OPTION
="--no-merges --cherry-pick"
942 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
943 SHORTONTO
=$
(git rev-parse
--short $ONTO)
944 if test -z "$REBASE_ROOT"
945 # this is now equivalent to ! -z "$UPSTREAM"
947 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
948 REVISIONS
=$UPSTREAM...
$HEAD
949 SHORTREVISIONS
=$SHORTUPSTREAM..
$SHORTHEAD
951 REVISIONS
=$ONTO...
$HEAD
952 SHORTREVISIONS
=$SHORTHEAD
954 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
955 --abbrev=7 --reverse --left-right --topo-order \
958 while read -r shortsha1 rest
960 if test t
!= "$PRESERVE_MERGES"
962 printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
964 sha1
=$
(git rev-parse
$shortsha1)
965 if test -z "$REBASE_ROOT"
968 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -s -f2-)
970 if test -f "$REWRITTEN"/$p -a \
( $p != $ONTO -o $sha1 = $first_after_upstream \
)
978 if test f
= "$preserve"
980 touch "$REWRITTEN"/$sha1
981 printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
986 # Watch for commits that been dropped by --cherry-pick
987 if test t
= "$PRESERVE_MERGES"
990 # Save all non-cherry-picked changes
991 git rev-list
$REVISIONS --left-right --cherry-pick | \
992 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
993 # Now all commits and note which ones are missing in
994 # not-cherry-picks and hence being dropped
995 git rev-list
$REVISIONS |
998 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
1000 # Use -f2 because if rev-list is telling us this commit is
1001 # not worthwhile, we don't want to track its multiple heads,
1002 # just the history of its first-parent for others that will
1003 # be rebasing on top of it
1004 git rev-list
--parents -1 $rev | cut
-d' ' -s -f2 > "$DROPPED"/$rev
1005 short
=$
(git rev-list
-1 --abbrev-commit --abbrev=7 $rev)
1006 sane_grep
-v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
1007 rm "$REWRITTEN"/$rev
1012 test -s "$TODO" ||
echo noop
>> "$TODO"
1013 test -n "$AUTOSQUASH" && rearrange_squash
"$TODO"
1014 cat >> "$TODO" << EOF
1016 # Rebase $SHORTREVISIONS onto $SHORTONTO
1019 # p, pick = use commit
1020 # r, reword = use commit, but edit the commit message
1021 # e, edit = use commit, but stop for amending
1022 # s, squash = use commit, but meld into previous commit
1023 # f, fixup = like "squash", but discard this commit's log message
1024 # x <cmd>, exec <cmd> = Run a shell command <cmd>, and stop if it fails
1026 # If you remove a line here THAT COMMIT WILL BE LOST.
1027 # However, if you remove everything, the rebase will be aborted.
1031 has_action
"$TODO" ||
1032 die_abort
"Nothing to do"
1034 cp "$TODO" "$TODO".backup
1035 git_editor
"$TODO" ||
1036 die_abort
"Could not execute editor"
1038 has_action
"$TODO" ||
1039 die_abort
"Nothing to do"
1041 test -d "$REWRITTEN" ||
test -n "$NEVER_FF" || skip_unnecessary_picks
1043 output git checkout
$ONTO || die_abort
"could not detach HEAD"
1044 git update-ref ORIG_HEAD
$HEAD