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 root rebase all reachable commmits up to the root(s)
32 autosquash move commits that begin with squash!/fixup! under -i
38 DOTEST
="$GIT_DIR/rebase-merge"
40 # The file containing rebase commands, comments, and empty lines.
41 # This file is created by "git rebase -i" then edited by the user. As
42 # the lines are processed, they are removed from the front of this
43 # file and written to the tail of $DONE.
44 TODO
="$DOTEST"/git-rebase-todo
46 # The rebase command lines that have already been processed. A line
47 # is moved here when it is first handled, before any associated user
51 # The commit message that is planned to be used for any changes that
52 # need to be committed following a user interaction.
55 # The file into which is accumulated the suggested commit message for
56 # squash/fixup commands. When the first of a series of squash/fixups
57 # is seen, the file is created and the commit message from the
58 # previous commit and from the first squash/fixup commit are written
59 # to it. The commit message for each subsequent squash/fixup commit
60 # is appended to the file as it is processed.
62 # The first line of the file is of the form
63 # # This is a combination of $COUNT commits.
64 # where $COUNT is the number of commits whose messages have been
65 # written to the file so far (including the initial "pick" commit).
66 # Each time that a commit message is processed, this line is read and
67 # updated. It is deleted just before the combined commit is made.
68 SQUASH_MSG
="$DOTEST"/message-squash
70 # If the current series of squash/fixups has not yet included a squash
71 # command, then this file exists and holds the commit message of the
72 # original "pick" commit. (If the series ends without a "squash"
73 # command, then this can be used as the commit message of the combined
74 # commit without opening the editor.)
75 FIXUP_MSG
="$DOTEST"/message-fixup
77 # $REWRITTEN is the name of a directory containing files for each
78 # commit that is reachable by at least one merge base of $HEAD and
79 # $UPSTREAM. They are not necessarily rewritten, but their children
80 # might be. This ensures that commits on merged, but otherwise
81 # unrelated side branches are left alone. (Think "X" in the man page's
83 REWRITTEN
="$DOTEST"/rewritten
85 DROPPED
="$DOTEST"/dropped
87 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
88 # GIT_AUTHOR_DATE that will be used for the commit that is currently
90 AUTHOR_SCRIPT
="$DOTEST"/author-script
92 # When an "edit" rebase command is being processed, the SHA1 of the
93 # commit to be edited is recorded in this file. When "git rebase
94 # --continue" is executed, if there are any staged changes then they
95 # will be amended to the HEAD commit, but only provided the HEAD
96 # commit is still the commit to be edited. When any other rebase
97 # command is processed, this file is deleted.
100 # For the post-rewrite hook, we make a list of rewritten commits and
101 # their new sha1s. The rewritten-pending list keeps the sha1s of
102 # commits that have been processed, but not committed yet,
103 # e.g. because they are waiting for a 'squash' command.
104 REWRITTEN_LIST
="$DOTEST"/rewritten-list
105 REWRITTEN_PENDING
="$DOTEST"/rewritten-pending
111 OK_TO_SKIP_PRE_REBASE
=
116 GIT_CHERRY_PICK_HELP
=" After resolving the conflicts,
117 mark the corrected paths with 'git add <paths>', and
118 run 'git rebase --continue'"
119 export GIT_CHERRY_PICK_HELP
122 printf '%s\n' "$*" >&2
130 test $status != 0 && printf "%s\n" "$output"
139 # Output the commit message for the specified commit.
141 git cat-file commit
"$1" |
sed "1,/^$/d"
144 run_pre_rebase_hook
() {
145 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
146 test -x "$GIT_DIR/hooks/pre-rebase"
148 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
{
149 echo >&2 "The pre-rebase hook refused to rebase."
155 require_clean_work_tree
() {
156 # test if working tree is dirty
157 git rev-parse
--verify HEAD
> /dev
/null
&&
158 git update-index
--ignore-submodules --refresh &&
159 git diff-files
--quiet --ignore-submodules &&
160 git diff-index
--cached --quiet HEAD
--ignore-submodules -- ||
161 die
"Working tree is dirty"
164 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
166 comment_for_reflog
() {
167 case "$ORIG_REFLOG_ACTION" in
169 GIT_REFLOG_ACTION
="rebase -i ($1)"
170 export GIT_REFLOG_ACTION
176 mark_action_done
() {
177 sed -e 1q
< "$TODO" >> "$DONE"
178 sed -e 1d
< "$TODO" >> "$TODO".new
179 mv -f "$TODO".new
"$TODO"
180 count
=$
(sane_grep
-c '^[^#]' < "$DONE")
181 total
=$
(($count+$
(sane_grep
-c '^[^#]' < "$TODO")))
182 if test "$last_count" != "$count"
185 printf "Rebasing (%d/%d)\r" $count $total
186 test -z "$VERBOSE" ||
echo
191 sha1_and_parents
="$(git rev-list --parents -1 "$1")"
192 case "$sha1_and_parents" in
194 git
diff --cc $sha1_and_parents
197 git diff-tree
-p "$1^!"
202 esac > "$DOTEST"/patch
204 commit_message
"$1" > "$MSG"
205 test -f "$AUTHOR_SCRIPT" ||
206 get_author_ident_from_commit
"$1" > "$AUTHOR_SCRIPT"
210 echo "$1" > "$DOTEST"/stopped-sha
222 sane_grep
'^[^#]' "$1" >/dev
/null
225 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
226 # GIT_AUTHOR_DATE exported from the current environment.
229 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
236 case "$1" in -n) sha1
=$2; ff
= ;; *) sha1
=$1 ;; esac
237 case "$NEVER_FF" in '') ;; ?
*) ff
= ;; esac
238 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
239 test -d "$REWRITTEN" &&
240 pick_one_preserving_merges
"$@" && return
241 if test -n "$REBASE_ROOT"
243 output git cherry-pick
"$@"
246 output git cherry-pick
$ff "$@"
249 pick_one_preserving_merges
() {
260 sha1
=$
(git rev-parse
$sha1)
262 if test -f "$DOTEST"/current-commit
264 if test "$fast_forward" = t
266 while read current_commit
268 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
269 done <"$DOTEST"/current-commit
270 rm "$DOTEST"/current-commit ||
271 die
"Cannot write current commit's replacement sha1"
275 echo $sha1 >> "$DOTEST"/current-commit
277 # rewrite parents; if none were rewritten, we can fast-forward.
279 pend
=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
280 if test "$pend" = " "
284 while [ "$pend" != "" ]
286 p
=$
(expr "$pend" : ' \([^ ]*\)')
289 if test -f "$REWRITTEN"/$p
291 new_p
=$
(cat "$REWRITTEN"/$p)
293 # If the todo reordered commits, and our parent is marked for
294 # rewriting, but hasn't been gotten to yet, assume the user meant to
295 # drop it on top of the current HEAD
298 new_p
=$
(git rev-parse HEAD
)
301 test $p != $new_p && fast_forward
=f
302 case "$new_parents" in
304 ;; # do nothing; that parent is already there
306 new_parents
="$new_parents $new_p"
310 if test -f "$DROPPED"/$p
313 replacement
="$(cat "$DROPPED"/$p)"
314 test -z "$replacement" && replacement
=root
315 pend
=" $replacement$pend"
317 new_parents
="$new_parents $p"
321 case $fast_forward in
323 output warn
"Fast-forward to $sha1"
324 output git
reset --hard $sha1 ||
325 die
"Cannot fast-forward to $sha1"
328 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
332 # detach HEAD to current parent
333 output git checkout
$first_parent 2> /dev
/null ||
334 die
"Cannot move HEAD to $first_parent"
337 case "$new_parents" in
339 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
342 author_script
=$
(get_author_ident_from_commit
$sha1)
343 eval "$author_script"
344 msg
="$(commit_message $sha1)"
345 # No point in merging the first parent, that's HEAD
346 new_parents
=${new_parents# $first_parent}
347 if ! do_with_author output \
348 git merge
$STRATEGY -m "$msg" $new_parents
350 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
351 die_with_patch
$sha1 "Error redoing merge $sha1"
353 echo "$sha1 $(git rev-parse HEAD^0)" >> "$REWRITTEN_LIST"
356 output git cherry-pick
"$@" ||
357 die_with_patch
$sha1 "Could not pick $sha1"
366 *1[0-9]|
*[04-9]) echo "$1"th
;;
373 update_squash_messages
() {
374 if test -f "$SQUASH_MSG"; then
375 mv "$SQUASH_MSG" "$SQUASH_MSG".bak ||
exit
377 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
378 -e "q" < "$SQUASH_MSG".bak
)+1))
380 echo "# This is a combination of $COUNT commits."
381 sed -e 1d
-e '2,/^./{
383 }' <"$SQUASH_MSG".bak
386 commit_message HEAD
> "$FIXUP_MSG" || die
"Cannot write $FIXUP_MSG"
389 echo "# This is a combination of 2 commits."
390 echo "# The first commit's message is:"
399 echo "# This is the $(nth_string $COUNT) commit message:"
405 echo "# The $(nth_string $COUNT) commit message will be skipped:"
407 commit_message
$2 |
sed -e 's/^/# /'
412 peek_next_command
() {
413 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
416 # A squash/fixup has failed. Prepare the long version of the squash
417 # commit message, then die_with_patch. This code path requires the
418 # user to edit the combined commit message for all commits that have
419 # been squashed/fixedup so far. So also erase the old squash
420 # messages, effectively causing the combined commit to be used as the
421 # new basis for any further squash/fixups. Args: sha1 rest
422 die_failed_squash
() {
423 mv "$SQUASH_MSG" "$MSG" ||
exit
425 cp "$MSG" "$GIT_DIR"/MERGE_MSG ||
exit
427 warn
"Could not apply $1... $2"
431 flush_rewritten_pending
() {
432 test -s "$REWRITTEN_PENDING" ||
return
433 newsha1
="$(git rev-parse HEAD^0)"
434 sed "s/$/ $newsha1/" < "$REWRITTEN_PENDING" >> "$REWRITTEN_LIST"
435 rm -f "$REWRITTEN_PENDING"
438 record_in_rewritten
() {
439 oldsha1
="$(git rev-parse $1)"
440 echo "$oldsha1" >> "$REWRITTEN_PENDING"
442 case "$(peek_next_command)" in
446 flush_rewritten_pending
452 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" ||
exit
453 read -r command sha1 rest
< "$TODO"
459 comment_for_reflog pick
463 die_with_patch
$sha1 "Could not apply $sha1... $rest"
464 record_in_rewritten
$sha1
467 comment_for_reflog reword
471 die_with_patch
$sha1 "Could not apply $sha1... $rest"
472 git commit
--amend --no-post-rewrite
473 record_in_rewritten
$sha1
476 comment_for_reflog edit
480 die_with_patch
$sha1 "Could not apply $sha1... $rest"
481 echo "$sha1" > "$DOTEST"/stopped-sha
483 git rev-parse
--verify HEAD
> "$AMEND"
484 warn
"Stopped at $sha1... $rest"
485 warn
"You can amend the commit now, with"
487 warn
" git commit --amend"
489 warn
"Once you are satisfied with your changes, run"
491 warn
" git rebase --continue"
504 comment_for_reflog
$squash_style
506 test -f "$DONE" && has_action
"$DONE" ||
507 die
"Cannot '$squash_style' without a previous commit"
510 update_squash_messages
$squash_style $sha1
511 author_script
=$
(get_author_ident_from_commit HEAD
)
512 echo "$author_script" > "$AUTHOR_SCRIPT"
513 eval "$author_script"
514 output git
reset --soft HEAD^
515 pick_one
-n $sha1 || die_failed_squash
$sha1 "$rest"
516 case "$(peek_next_command)" in
518 # This is an intermediate commit; its message will only be
519 # used in case of trouble. So use the long version:
520 do_with_author output git commit
--no-verify -F "$SQUASH_MSG" ||
521 die_failed_squash
$sha1 "$rest"
524 # This is the final command of this squash/fixup group
525 if test -f "$FIXUP_MSG"
527 do_with_author git commit
--no-verify -F "$FIXUP_MSG" ||
528 die_failed_squash
$sha1 "$rest"
530 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG ||
exit
531 rm -f "$GIT_DIR"/MERGE_MSG
532 do_with_author git commit
--no-verify -e ||
533 die_failed_squash
$sha1 "$rest"
535 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
538 record_in_rewritten
$sha1
541 read -r command rest
< "$TODO"
543 printf 'Executing: %s\n' "$rest"
544 # "exec" command doesn't take a sha1 in the todo-list.
545 # => can't just use $sha1 here.
546 git rev-parse
--verify HEAD
> "$DOTEST"/stopped-sha
547 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
549 if test "$status" -ne 0
551 warn
"Execution failed: $rest"
552 warn
"You can fix the problem, and then run"
554 warn
" git rebase --continue"
558 # Run in subshell because require_clean_work_tree can die.
559 if ! (require_clean_work_tree
)
561 warn
"Commit or stash your changes, and then run"
563 warn
" git rebase --continue"
569 warn
"Unknown command: $command $sha1 $rest"
570 if git rev-parse
--verify -q "$sha1" >/dev
/null
572 die_with_patch
$sha1 "Please fix this in the file $TODO."
574 die
"Please fix this in the file $TODO."
578 test -s "$TODO" && return
580 comment_for_reflog finish
&&
581 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
582 OLDHEAD
=$
(cat "$DOTEST"/head) &&
583 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
584 NEWHEAD
=$
(git rev-parse HEAD
) &&
587 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
588 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
589 git symbolic-ref HEAD
$HEADNAME
592 test ! -f "$DOTEST"/verbose ||
593 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
596 test -s "$REWRITTEN_LIST" &&
597 git notes copy
--for-rewrite=rebase
< "$REWRITTEN_LIST" ||
598 true
# we don't care if this copying failed
600 if test -x "$GIT_DIR"/hooks
/post-rewrite
&&
601 test -s "$REWRITTEN_LIST"; then
602 "$GIT_DIR"/hooks
/post-rewrite rebase
< "$REWRITTEN_LIST"
603 true
# we don't care if this hook failed
607 warn
"Successfully rebased and updated $HEADNAME."
619 # skip picking commits whose parents are unchanged
620 skip_unnecessary_picks
() {
622 while read -r command rest
624 # fd=3 means we skip the command
625 case "$fd,$command" in
627 # pick a commit whose parent is current $ONTO -> skip
628 sha1
=$
(printf '%s' "$rest" | cut
-d ' ' -f 1)
629 case "$(git rev-parse --verify --quiet "$sha1"^)" in
645 printf '%s\n' "$command${rest:+ }$rest" >&$fd
646 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
647 mv -f "$TODO".new
"$TODO" &&
648 case "$(peek_next_command)" in
650 record_in_rewritten
"$ONTO"
653 die
"Could not skip unnecessary pick commands"
656 # check if no other options are set
658 test $# -eq 2 -a "$2" = '--' &&
660 test -z "$PRESERVE_MERGES" &&
661 test -z "$STRATEGY" &&
665 get_saved_options
() {
666 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
667 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
668 test -f "$DOTEST"/verbose
&& VERBOSE
=t
669 test -f "$DOTEST"/rebase-root
&& REBASE_ROOT
=t
672 # Rearrange the todo list that has both "pick sha1 msg" and
673 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
674 # comes immediately after the former, and change "pick" to
676 rearrange_squash
() {
677 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
678 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
680 test -s "$1.sq" ||
return
683 while read -r pick sha1 message
686 *" $sha1 "*) continue ;;
688 printf '%s\n' "$pick $sha1 $message"
689 while read -r squash action msg
693 printf '%s\n' "$action $squash $action! $msg"
698 done >"$1.rearranged" <"$1"
699 cat "$1.rearranged" >"$1"
700 rm -f "$1.sq" "$1.rearranged"
708 if left
=${1%...*} right
=${1#*...} &&
709 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
719 git rev-parse
--verify "$1^0"
726 OK_TO_SKIP_PRE_REBASE
=yes
731 is_standalone
"$@" || usage
733 comment_for_reflog
continue
735 test -d "$DOTEST" || die
"No interactive rebase running"
738 git rev-parse
--verify HEAD
>/dev
/null ||
739 die
"Cannot read HEAD"
740 git update-index
--ignore-submodules --refresh &&
741 git diff-files
--quiet --ignore-submodules ||
742 die
"Working tree is dirty"
744 # do we have anything to commit?
745 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
747 : Nothing to commit
-- skip this
749 .
"$AUTHOR_SCRIPT" ||
750 die
"Cannot find the author identity"
754 amend
=$
(git rev-parse
--verify HEAD
)
755 test "$amend" = $
(cat "$AMEND") ||
757 You have uncommitted changes in your working tree. Please, commit them
758 first and then run 'git rebase --continue' again."
759 git
reset --soft HEAD^ ||
760 die
"Cannot rewind the HEAD"
762 do_with_author git commit
--no-verify -F "$MSG" -e ||
{
763 test -n "$amend" && git
reset --soft $amend
764 die
"Could not commit staged changes."
768 record_in_rewritten
"$(cat "$DOTEST"/stopped-sha)"
770 require_clean_work_tree
774 is_standalone
"$@" || usage
776 comment_for_reflog abort
779 test -d "$DOTEST" || die
"No interactive rebase running"
781 HEADNAME
=$
(cat "$DOTEST"/head-name
)
782 HEAD
=$
(cat "$DOTEST"/head)
785 git symbolic-ref HEAD
$HEADNAME
788 output git
reset --hard $HEAD &&
793 is_standalone
"$@" || usage
795 comment_for_reflog skip
798 test -d "$DOTEST" || die
"No interactive rebase running"
800 output git
reset --hard && do_rest
805 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
814 # we use merge anyway
836 ONTO
=$
(parse_onto
"$1") ||
837 die
"Does not point to a valid commit: $1"
841 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
842 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
844 die
"Interactive rebase already started"
846 git var GIT_COMMITTER_IDENT
>/dev
/null ||
847 die
"You need to set your committer info first"
849 if test -z "$REBASE_ROOT"
852 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
853 test -z "$ONTO" && ONTO
=$UPSTREAM
859 die
"You must specify --onto when using --root"
861 run_pre_rebase_hook
"$UPSTREAM_ARG" "$@"
863 comment_for_reflog start
865 require_clean_work_tree
869 output git checkout
"$1" ||
870 die
"Could not checkout $1"
873 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
874 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
876 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
877 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
878 echo "detached HEAD" > "$DOTEST"/head-name
880 echo $HEAD > "$DOTEST"/head
881 case "$REBASE_ROOT" in
883 rm -f "$DOTEST"/rebase-root
;;
885 : >"$DOTEST"/rebase-root
;;
887 echo $ONTO > "$DOTEST"/onto
888 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
889 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
890 if test t
= "$PRESERVE_MERGES"
892 if test -z "$REBASE_ROOT"
894 mkdir
"$REWRITTEN" &&
895 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
897 echo $ONTO > "$REWRITTEN"/$c ||
898 die
"Could not init rewritten commits"
901 mkdir
"$REWRITTEN" &&
902 echo $ONTO > "$REWRITTEN"/root ||
903 die
"Could not init rewritten commits"
905 # No cherry-pick because our first pass is to determine
906 # parents to rewrite and skipping dropped commits would
907 # prematurely end our probe
909 first_after_upstream
="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
911 MERGES_OPTION
="--no-merges --cherry-pick"
914 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
915 SHORTONTO
=$
(git rev-parse
--short $ONTO)
916 if test -z "$REBASE_ROOT"
917 # this is now equivalent to ! -z "$UPSTREAM"
919 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
920 REVISIONS
=$UPSTREAM...
$HEAD
921 SHORTREVISIONS
=$SHORTUPSTREAM..
$SHORTHEAD
923 REVISIONS
=$ONTO...
$HEAD
924 SHORTREVISIONS
=$SHORTHEAD
926 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
927 --abbrev=7 --reverse --left-right --topo-order \
930 while read -r shortsha1 rest
932 if test t
!= "$PRESERVE_MERGES"
934 printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
936 sha1
=$
(git rev-parse
$shortsha1)
937 if test -z "$REBASE_ROOT"
940 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -s -f2-)
942 if test -f "$REWRITTEN"/$p -a \
( $p != $ONTO -o $sha1 = $first_after_upstream \
)
950 if test f
= "$preserve"
952 touch "$REWRITTEN"/$sha1
953 printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
958 # Watch for commits that been dropped by --cherry-pick
959 if test t
= "$PRESERVE_MERGES"
962 # Save all non-cherry-picked changes
963 git rev-list
$REVISIONS --left-right --cherry-pick | \
964 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
965 # Now all commits and note which ones are missing in
966 # not-cherry-picks and hence being dropped
967 git rev-list
$REVISIONS |
970 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
972 # Use -f2 because if rev-list is telling us this commit is
973 # not worthwhile, we don't want to track its multiple heads,
974 # just the history of its first-parent for others that will
975 # be rebasing on top of it
976 git rev-list
--parents -1 $rev | cut
-d' ' -s -f2 > "$DROPPED"/$rev
977 short
=$
(git rev-list
-1 --abbrev-commit --abbrev=7 $rev)
978 sane_grep
-v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
984 test -s "$TODO" ||
echo noop
>> "$TODO"
985 test -n "$AUTOSQUASH" && rearrange_squash
"$TODO"
986 cat >> "$TODO" << EOF
988 # Rebase $SHORTREVISIONS onto $SHORTONTO
991 # p, pick = use commit
992 # r, reword = use commit, but edit the commit message
993 # e, edit = use commit, but stop for amending
994 # s, squash = use commit, but meld into previous commit
995 # f, fixup = like "squash", but discard this commit's log message
996 # x <cmd>, exec <cmd> = Run a shell command <cmd>, and stop if it fails
998 # If you remove a line here THAT COMMIT WILL BE LOST.
999 # However, if you remove everything, the rebase will be aborted.
1003 has_action
"$TODO" ||
1004 die_abort
"Nothing to do"
1006 cp "$TODO" "$TODO".backup
1007 git_editor
"$TODO" ||
1008 die_abort
"Could not execute editor"
1010 has_action
"$TODO" ||
1011 die_abort
"Nothing to do"
1013 test -d "$REWRITTEN" ||
test -n "$NEVER_FF" || skip_unnecessary_picks
1015 output git checkout
$ONTO || die_abort
"could not detach HEAD"
1016 git update-ref ORIG_HEAD
$HEAD