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 dotest
="$GIT_DIR/rebase-merge"
17 # The file containing rebase commands, comments, and empty lines.
18 # This file is created by "git rebase -i" then edited by the user. As
19 # the lines are processed, they are removed from the front of this
20 # file and written to the tail of $done.
21 todo
="$dotest"/git-rebase-todo
23 # The rebase command lines that have already been processed. A line
24 # is moved here when it is first handled, before any associated user
28 # The commit message that is planned to be used for any changes that
29 # need to be committed following a user interaction.
32 # The file into which is accumulated the suggested commit message for
33 # squash/fixup commands. When the first of a series of squash/fixups
34 # is seen, the file is created and the commit message from the
35 # previous commit and from the first squash/fixup commit are written
36 # to it. The commit message for each subsequent squash/fixup commit
37 # is appended to the file as it is processed.
39 # The first line of the file is of the form
40 # # This is a combination of $count commits.
41 # where $count is the number of commits whose messages have been
42 # written to the file so far (including the initial "pick" commit).
43 # Each time that a commit message is processed, this line is read and
44 # updated. It is deleted just before the combined commit is made.
45 squash_msg
="$dotest"/message-squash
47 # If the current series of squash/fixups has not yet included a squash
48 # command, then this file exists and holds the commit message of the
49 # original "pick" commit. (If the series ends without a "squash"
50 # command, then this can be used as the commit message of the combined
51 # commit without opening the editor.)
52 fixup_msg
="$dotest"/message-fixup
54 # $rewritten is the name of a directory containing files for each
55 # commit that is reachable by at least one merge base of $head and
56 # $upstream. They are not necessarily rewritten, but their children
57 # might be. This ensures that commits on merged, but otherwise
58 # unrelated side branches are left alone. (Think "X" in the man page's
60 rewritten
="$dotest"/rewritten
62 dropped
="$dotest"/dropped
64 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
65 # GIT_AUTHOR_DATE that will be used for the commit that is currently
67 author_script
="$dotest"/author-script
69 # When an "edit" rebase command is being processed, the SHA1 of the
70 # commit to be edited is recorded in this file. When "git rebase
71 # --continue" is executed, if there are any staged changes then they
72 # will be amended to the HEAD commit, but only provided the HEAD
73 # commit is still the commit to be edited. When any other rebase
74 # command is processed, this file is deleted.
77 # For the post-rewrite hook, we make a list of rewritten commits and
78 # their new sha1s. The rewritten-pending list keeps the sha1s of
79 # commits that have been processed, but not committed yet,
80 # e.g. because they are waiting for a 'squash' command.
81 rewritten_list
="$dotest"/rewritten-list
82 rewritten_pending
="$dotest"/rewritten-pending
84 GIT_CHERRY_PICK_HELP
="\
85 hint: after resolving the conflicts, mark the corrected paths
86 hint: with 'git add <paths>' and run 'git rebase --continue'"
87 export GIT_CHERRY_PICK_HELP
90 printf '%s\n' "$*" >&2
98 test $status != 0 && printf "%s\n" "$output"
107 # Output the commit message for the specified commit.
109 git cat-file commit
"$1" |
sed "1,/^$/d"
112 run_pre_rebase_hook
() {
113 if test -z "$ok_to_skip_pre_rebase" &&
114 test -x "$GIT_DIR/hooks/pre-rebase"
116 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
{
117 echo >&2 "The pre-rebase hook refused to rebase."
124 orig_reflog_action
="$GIT_REFLOG_ACTION"
126 comment_for_reflog
() {
127 case "$orig_reflog_action" in
129 GIT_REFLOG_ACTION
="rebase -i ($1)"
130 export GIT_REFLOG_ACTION
136 mark_action_done
() {
137 sed -e 1q
< "$todo" >> "$done"
138 sed -e 1d
< "$todo" >> "$todo".new
139 mv -f "$todo".new
"$todo"
140 new_count
=$
(sane_grep
-c '^[^#]' < "$done")
141 total
=$
(($new_count+$
(sane_grep
-c '^[^#]' < "$todo")))
142 if test "$last_count" != "$new_count"
144 last_count
=$new_count
145 printf "Rebasing (%d/%d)\r" $new_count $total
146 test -z "$verbose" ||
echo
151 sha1_and_parents
="$(git rev-list --parents -1 "$1")"
152 case "$sha1_and_parents" in
154 git
diff --cc $sha1_and_parents
157 git diff-tree
-p "$1^!"
162 esac > "$dotest"/patch
164 commit_message
"$1" > "$msg"
165 test -f "$author_script" ||
166 get_author_ident_from_commit
"$1" > "$author_script"
170 echo "$1" > "$dotest"/stopped-sha
182 sane_grep
'^[^#]' "$1" >/dev
/null
185 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
186 # GIT_AUTHOR_DATE exported from the current environment.
189 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
196 case "$1" in -n) sha1
=$2; ff
= ;; *) sha1
=$1 ;; esac
197 case "$force_rebase" in '') ;; ?
*) ff
= ;; esac
198 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
199 test -d "$rewritten" &&
200 pick_one_preserving_merges
"$@" && return
201 if test -n "$rebase_root"
203 output git cherry-pick
"$@"
206 output git cherry-pick
$ff "$@"
209 pick_one_preserving_merges
() {
220 sha1
=$
(git rev-parse
$sha1)
222 if test -f "$dotest"/current-commit
224 if test "$fast_forward" = t
226 while read current_commit
228 git rev-parse HEAD
> "$rewritten"/$current_commit
229 done <"$dotest"/current-commit
230 rm "$dotest"/current-commit ||
231 die
"Cannot write current commit's replacement sha1"
235 echo $sha1 >> "$dotest"/current-commit
237 # rewrite parents; if none were rewritten, we can fast-forward.
239 pend
=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
240 if test "$pend" = " "
244 while [ "$pend" != "" ]
246 p
=$
(expr "$pend" : ' \([^ ]*\)')
249 if test -f "$rewritten"/$p
251 new_p
=$
(cat "$rewritten"/$p)
253 # If the todo reordered commits, and our parent is marked for
254 # rewriting, but hasn't been gotten to yet, assume the user meant to
255 # drop it on top of the current HEAD
258 new_p
=$
(git rev-parse HEAD
)
261 test $p != $new_p && fast_forward
=f
262 case "$new_parents" in
264 ;; # do nothing; that parent is already there
266 new_parents
="$new_parents $new_p"
270 if test -f "$dropped"/$p
273 replacement
="$(cat "$dropped"/$p)"
274 test -z "$replacement" && replacement
=root
275 pend
=" $replacement$pend"
277 new_parents
="$new_parents $p"
281 case $fast_forward in
283 output warn
"Fast-forward to $sha1"
284 output git
reset --hard $sha1 ||
285 die
"Cannot fast-forward to $sha1"
288 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
292 # detach HEAD to current parent
293 output git checkout
$first_parent 2> /dev
/null ||
294 die
"Cannot move HEAD to $first_parent"
297 case "$new_parents" in
299 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
302 author_script_content
=$
(get_author_ident_from_commit
$sha1)
303 eval "$author_script_content"
304 msg_content
="$(commit_message $sha1)"
305 # No point in merging the first parent, that's HEAD
306 new_parents
=${new_parents# $first_parent}
307 if ! do_with_author output \
308 git merge
${strategy:+-s $strategy} -m \
309 "$msg_content" $new_parents
311 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
312 die_with_patch
$sha1 "Error redoing merge $sha1"
314 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
317 output git cherry-pick
"$@" ||
318 die_with_patch
$sha1 "Could not pick $sha1"
327 *1[0-9]|
*[04-9]) echo "$1"th
;;
334 update_squash_messages
() {
335 if test -f "$squash_msg"; then
336 mv "$squash_msg" "$squash_msg".bak ||
exit
338 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
339 -e "q" < "$squash_msg".bak
)+1))
341 echo "# This is a combination of $count commits."
342 sed -e 1d
-e '2,/^./{
344 }' <"$squash_msg".bak
347 commit_message HEAD
> "$fixup_msg" || die
"Cannot write $fixup_msg"
350 echo "# This is a combination of 2 commits."
351 echo "# The first commit's message is:"
360 echo "# This is the $(nth_string $count) commit message:"
366 echo "# The $(nth_string $count) commit message will be skipped:"
368 commit_message
$2 |
sed -e 's/^/# /'
373 peek_next_command
() {
374 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$todo"
377 # A squash/fixup has failed. Prepare the long version of the squash
378 # commit message, then die_with_patch. This code path requires the
379 # user to edit the combined commit message for all commits that have
380 # been squashed/fixedup so far. So also erase the old squash
381 # messages, effectively causing the combined commit to be used as the
382 # new basis for any further squash/fixups. Args: sha1 rest
383 die_failed_squash
() {
384 mv "$squash_msg" "$msg" ||
exit
386 cp "$msg" "$GIT_DIR"/MERGE_MSG ||
exit
388 warn
"Could not apply $1... $2"
392 flush_rewritten_pending
() {
393 test -s "$rewritten_pending" ||
return
394 newsha1
="$(git rev-parse HEAD^0)"
395 sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
396 rm -f "$rewritten_pending"
399 record_in_rewritten
() {
400 oldsha1
="$(git rev-parse $1)"
401 echo "$oldsha1" >> "$rewritten_pending"
403 case "$(peek_next_command)" in
407 flush_rewritten_pending
413 rm -f "$msg" "$author_script" "$amend" ||
exit
414 read -r command sha1 rest
< "$todo"
420 comment_for_reflog pick
424 die_with_patch
$sha1 "Could not apply $sha1... $rest"
425 record_in_rewritten
$sha1
428 comment_for_reflog reword
432 die_with_patch
$sha1 "Could not apply $sha1... $rest"
433 git commit
--amend --no-post-rewrite
434 record_in_rewritten
$sha1
437 comment_for_reflog edit
441 die_with_patch
$sha1 "Could not apply $sha1... $rest"
442 echo "$sha1" > "$dotest"/stopped-sha
444 git rev-parse
--verify HEAD
> "$amend"
445 warn
"Stopped at $sha1... $rest"
446 warn
"You can amend the commit now, with"
448 warn
" git commit --amend"
450 warn
"Once you are satisfied with your changes, run"
452 warn
" git rebase --continue"
465 comment_for_reflog
$squash_style
467 test -f "$done" && has_action
"$done" ||
468 die
"Cannot '$squash_style' without a previous commit"
471 update_squash_messages
$squash_style $sha1
472 author_script_content
=$
(get_author_ident_from_commit HEAD
)
473 echo "$author_script_content" > "$author_script"
474 eval "$author_script_content"
475 output git
reset --soft HEAD^
476 pick_one
-n $sha1 || die_failed_squash
$sha1 "$rest"
477 case "$(peek_next_command)" in
479 # This is an intermediate commit; its message will only be
480 # used in case of trouble. So use the long version:
481 do_with_author output git commit
--no-verify -F "$squash_msg" ||
482 die_failed_squash
$sha1 "$rest"
485 # This is the final command of this squash/fixup group
486 if test -f "$fixup_msg"
488 do_with_author git commit
--no-verify -F "$fixup_msg" ||
489 die_failed_squash
$sha1 "$rest"
491 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG ||
exit
492 rm -f "$GIT_DIR"/MERGE_MSG
493 do_with_author git commit
--no-verify -e ||
494 die_failed_squash
$sha1 "$rest"
496 rm -f "$squash_msg" "$fixup_msg"
499 record_in_rewritten
$sha1
502 read -r command rest
< "$todo"
504 printf 'Executing: %s\n' "$rest"
505 # "exec" command doesn't take a sha1 in the todo-list.
506 # => can't just use $sha1 here.
507 git rev-parse
--verify HEAD
> "$dotest"/stopped-sha
508 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
510 if test "$status" -ne 0
512 warn
"Execution failed: $rest"
513 warn
"You can fix the problem, and then run"
515 warn
" git rebase --continue"
519 # Run in subshell because require_clean_work_tree can die.
520 if ! (require_clean_work_tree
"rebase")
522 warn
"Commit or stash your changes, and then run"
524 warn
" git rebase --continue"
530 warn
"Unknown command: $command $sha1 $rest"
531 if git rev-parse
--verify -q "$sha1" >/dev
/null
533 die_with_patch
$sha1 "Please fix this in the file $todo."
535 die
"Please fix this in the file $todo."
539 test -s "$todo" && return
541 comment_for_reflog finish
&&
542 headname
=$
(cat "$dotest"/head-name
) &&
543 oldhead
=$
(cat "$dotest"/head) &&
544 shortonto
=$
(git rev-parse
--short $
(cat "$dotest"/onto
)) &&
545 newhead
=$
(git rev-parse HEAD
) &&
548 message
="$GIT_REFLOG_ACTION: $headname onto $shortonto" &&
549 git update-ref
-m "$message" $headname $newhead $oldhead &&
550 git symbolic-ref HEAD
$headname
553 test ! -f "$dotest"/verbose ||
554 git diff-tree
--stat $
(cat "$dotest"/head)..HEAD
557 test -s "$rewritten_list" &&
558 git notes copy
--for-rewrite=rebase
< "$rewritten_list" ||
559 true
# we don't care if this copying failed
561 if test -x "$GIT_DIR"/hooks
/post-rewrite
&&
562 test -s "$rewritten_list"; then
563 "$GIT_DIR"/hooks
/post-rewrite rebase
< "$rewritten_list"
564 true
# we don't care if this hook failed
568 warn
"Successfully rebased and updated $headname."
580 # skip picking commits whose parents are unchanged
581 skip_unnecessary_picks
() {
583 while read -r command rest
585 # fd=3 means we skip the command
586 case "$fd,$command" in
588 # pick a commit whose parent is current $onto -> skip
590 case "$(git rev-parse --verify --quiet "$sha1"^)" in
606 printf '%s\n' "$command${rest:+ }$rest" >&$fd
607 done <"$todo" >"$todo.new" 3>>"$done" &&
608 mv -f "$todo".new
"$todo" &&
609 case "$(peek_next_command)" in
611 record_in_rewritten
"$onto"
614 die
"Could not skip unnecessary pick commands"
617 get_saved_options
() {
618 test -d "$rewritten" && preserve_merges
=t
619 test -f "$dotest"/strategy
&& strategy
="$(cat "$dotest"/strategy)"
620 test -f "$dotest"/verbose
&& verbose
=t
621 test -f "$dotest"/rebase-root
&& rebase_root
=t
624 # Rearrange the todo list that has both "pick sha1 msg" and
625 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
626 # comes immediately after the former, and change "pick" to
628 rearrange_squash
() {
629 # extract fixup!/squash! lines and resolve any referenced sha1's
630 while read -r pick sha1 message
633 "squash! "*|
"fixup! "*)
634 action
="${message%%!*}"
635 rest
="${message#*! }"
636 echo "$sha1 $action $rest"
637 # if it's a single word, try to resolve to a full sha1 and
638 # emit a second copy. This allows us to match on both message
640 if test "${rest#* }" = "$rest"; then
641 fullsha
="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
642 if test -n "$fullsha"; then
643 # prefix the action to uniquely identify this line as
644 # intended for full sha1 match
645 echo "$sha1 +$action $fullsha"
650 test -s "$1.sq" ||
return
653 while read -r pick sha1 message
656 *" $sha1 "*) continue ;;
658 printf '%s\n' "$pick $sha1 $message"
660 while read -r squash action msg_content
663 *" $squash "*) continue ;;
669 # full sha1 prefix test
670 case "$msg_content" in "$sha1"*) emit
=1;; esac ;;
672 # message prefix test
673 case "$message" in "$msg_content"*) emit
=1;; esac ;;
675 if test $emit = 1; then
676 printf '%s\n' "$action $squash $action! $msg_content"
680 done >"$1.rearranged" <"$1"
681 cat "$1.rearranged" >"$1"
682 rm -f "$1.sq" "$1.rearranged"
690 if left
=${1%...*} right
=${1#*...} &&
691 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
701 git rev-parse
--verify "$1^0"
707 comment_for_reflog
continue
710 git rev-parse
--verify HEAD
>/dev
/null ||
711 die
"Cannot read HEAD"
712 git update-index
--ignore-submodules --refresh &&
713 git diff-files
--quiet --ignore-submodules ||
714 die
"Working tree is dirty"
716 # do we have anything to commit?
717 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
719 : Nothing to commit
-- skip this
721 .
"$author_script" ||
722 die
"Cannot find the author identity"
726 current_head
=$
(git rev-parse
--verify HEAD
)
727 test "$current_head" = $
(cat "$amend") ||
729 You have uncommitted changes in your working tree. Please, commit them
730 first and then run 'git rebase --continue' again."
731 git
reset --soft HEAD^ ||
732 die
"Cannot rewind the HEAD"
734 do_with_author git commit
--no-verify -F "$msg" -e ||
{
735 test -n "$current_head" && git
reset --soft $current_head
736 die
"Could not commit staged changes."
740 record_in_rewritten
"$(cat "$dotest"/stopped-sha)"
742 require_clean_work_tree
"rebase"
747 comment_for_reflog abort
751 headname
=$
(cat "$dotest"/head-name
)
752 head=$
(cat "$dotest"/head)
755 git symbolic-ref HEAD
$headname
758 output git
reset --hard $head &&
764 comment_for_reflog skip
768 output git
reset --hard && do_rest
774 onto
=$
(parse_onto
"$onto") || die
"Does not point to a valid commit: $1"
777 test -z "$rebase_root" -a $# -ge 1 -a $# -le 2 ||
778 test ! -z "$rebase_root" -a $# -le 1 || usage
780 git var GIT_COMMITTER_IDENT
>/dev
/null ||
781 die
"You need to set your committer info first"
783 if test -z "$rebase_root"
786 upstream
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
787 test -z "$onto" && onto
=$upstream
793 die
"You must specify --onto when using --root"
795 require_clean_work_tree
"rebase" "Please commit or stash them."
797 run_pre_rebase_hook
"$upstream_arg" "$@"
799 comment_for_reflog start
803 output git checkout
"$1" -- ||
804 die
"Could not checkout $1"
807 head=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
808 mkdir
"$dotest" || die
"Could not create temporary $dotest"
810 : > "$dotest"/interactive || die
"Could not mark as interactive"
811 git symbolic-ref HEAD
> "$dotest"/head-name
2> /dev
/null ||
812 echo "detached HEAD" > "$dotest"/head-name
814 echo $head > "$dotest"/head
815 case "$rebase_root" in
817 rm -f "$dotest"/rebase-root
;;
819 : >"$dotest"/rebase-root
;;
821 echo $onto > "$dotest"/onto
822 test -z "$strategy" ||
echo "$strategy" > "$dotest"/strategy
823 test t
= "$verbose" && : > "$dotest"/verbose
824 if test t
= "$preserve_merges"
826 if test -z "$rebase_root"
828 mkdir
"$rewritten" &&
829 for c
in $
(git merge-base
--all $head $upstream)
831 echo $onto > "$rewritten"/$c ||
832 die
"Could not init rewritten commits"
835 mkdir
"$rewritten" &&
836 echo $onto > "$rewritten"/root ||
837 die
"Could not init rewritten commits"
839 # No cherry-pick because our first pass is to determine
840 # parents to rewrite and skipping dropped commits would
841 # prematurely end our probe
843 first_after_upstream
="$(git rev-list --reverse --first-parent $upstream..$head | head -n 1)"
845 merges_option
="--no-merges --cherry-pick"
848 shorthead
=$
(git rev-parse
--short $head)
849 shortonto
=$
(git rev-parse
--short $onto)
850 if test -z "$rebase_root"
851 # this is now equivalent to ! -z "$upstream"
853 shortupstream
=$
(git rev-parse
--short $upstream)
854 revisions
=$upstream...
$head
855 shortrevisions
=$shortupstream..
$shorthead
857 revisions
=$onto...
$head
858 shortrevisions
=$shorthead
860 git rev-list
$merges_option --pretty=oneline
--abbrev-commit \
861 --abbrev=7 --reverse --left-right --topo-order \
864 while read -r shortsha1 rest
866 if test t
!= "$preserve_merges"
868 printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
870 sha1
=$
(git rev-parse
$shortsha1)
871 if test -z "$rebase_root"
874 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -s -f2-)
876 if test -f "$rewritten"/$p -a \
( $p != $onto -o $sha1 = $first_after_upstream \
)
884 if test f
= "$preserve"
886 touch "$rewritten"/$sha1
887 printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
892 # Watch for commits that been dropped by --cherry-pick
893 if test t
= "$preserve_merges"
896 # Save all non-cherry-picked changes
897 git rev-list
$revisions --left-right --cherry-pick | \
898 sed -n "s/^>//p" > "$dotest"/not-cherry-picks
899 # Now all commits and note which ones are missing in
900 # not-cherry-picks and hence being dropped
901 git rev-list
$revisions |
904 if test -f "$rewritten"/$rev -a "$(sane_grep "$rev" "$dotest"/not-cherry-picks)" = ""
906 # Use -f2 because if rev-list is telling us this commit is
907 # not worthwhile, we don't want to track its multiple heads,
908 # just the history of its first-parent for others that will
909 # be rebasing on top of it
910 git rev-list
--parents -1 $rev | cut
-d' ' -s -f2 > "$dropped"/$rev
911 short
=$
(git rev-list
-1 --abbrev-commit --abbrev=7 $rev)
912 sane_grep
-v "^[a-z][a-z]* $short" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
918 test -s "$todo" ||
echo noop
>> "$todo"
919 test -n "$autosquash" && rearrange_squash
"$todo"
920 cat >> "$todo" << EOF
922 # Rebase $shortrevisions onto $shortonto
925 # p, pick = use commit
926 # r, reword = use commit, but edit the commit message
927 # e, edit = use commit, but stop for amending
928 # s, squash = use commit, but meld into previous commit
929 # f, fixup = like "squash", but discard this commit's log message
930 # x, exec = run command (the rest of the line) using shell
932 # If you remove a line here THAT COMMIT WILL BE LOST.
933 # However, if you remove everything, the rebase will be aborted.
937 has_action
"$todo" ||
938 die_abort
"Nothing to do"
940 cp "$todo" "$todo".backup
941 git_editor
"$todo" ||
942 die_abort
"Could not execute editor"
944 has_action
"$todo" ||
945 die_abort
"Nothing to do"
947 test -d "$rewritten" ||
test -n "$force_rebase" || skip_unnecessary_picks
949 output git checkout
$onto || die_abort
"could not detach HEAD"
950 git update-ref ORIG_HEAD
$head