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 m,merge always used (no-op)
24 i,interactive always used (no-op)
26 continue continue rebasing process
27 abort abort rebasing process and restore original branch
28 skip skip current patch and continue rebasing process
29 no-verify override pre-rebase hook from stopping the operation
30 root rebase all reachable commmits up to the root(s)
31 autosquash move commits that begin with squash!/fixup! under -i
37 DOTEST="$GIT_DIR/rebase-merge"
39 # The file containing rebase commands, comments, and empty lines.
40 # This file is created by "git rebase -i" then edited by the user. As
41 # the lines are processed, they are removed from the front of this
42 # file and written to the tail of $DONE.
43 TODO="$DOTEST"/git-rebase-todo
45 # The rebase command lines that have already been processed. A line
46 # is moved here when it is first handled, before any associated user
50 # The commit message that is planned to be used for any changes that
51 # need to be committed following a user interaction.
54 # The file into which is accumulated the suggested commit message for
55 # squash/fixup commands. When the first of a series of squash/fixups
56 # is seen, the file is created and the commit message from the
57 # previous commit and from the first squash/fixup commit are written
58 # to it. The commit message for each subsequent squash/fixup commit
59 # is appended to the file as it is processed.
61 # The first line of the file is of the form
62 # # This is a combination of $COUNT commits.
63 # where $COUNT is the number of commits whose messages have been
64 # written to the file so far (including the initial "pick" commit).
65 # Each time that a commit message is processed, this line is read and
66 # updated. It is deleted just before the combined commit is made.
67 SQUASH_MSG="$DOTEST"/message-squash
69 # If the current series of squash/fixups has not yet included a squash
70 # command, then this file exists and holds the commit message of the
71 # original "pick" commit. (If the series ends without a "squash"
72 # command, then this can be used as the commit message of the combined
73 # commit without opening the editor.)
74 FIXUP_MSG="$DOTEST"/message-fixup
76 # $REWRITTEN is the name of a directory containing files for each
77 # commit that is reachable by at least one merge base of $HEAD and
78 # $UPSTREAM. They are not necessarily rewritten, but their children
79 # might be. This ensures that commits on merged, but otherwise
80 # unrelated side branches are left alone. (Think "X" in the man page's
82 REWRITTEN="$DOTEST"/rewritten
84 DROPPED="$DOTEST"/dropped
86 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
87 # GIT_AUTHOR_DATE that will be used for the commit that is currently
89 AUTHOR_SCRIPT="$DOTEST"/author-script
91 # When an "edit" rebase command is being processed, the SHA1 of the
92 # commit to be edited is recorded in this file. When "git rebase
93 # --continue" is executed, if there are any staged changes then they
94 # will be amended to the HEAD commit, but only provided the HEAD
95 # commit is still the commit to be edited. When any other rebase
96 # command is processed, this file is deleted.
103 OK_TO_SKIP_PRE_REBASE=
107 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
108 mark the corrected paths with 'git add <paths>', and
109 run 'git rebase --continue'"
110 export GIT_CHERRY_PICK_HELP
121 test $status != 0 && printf "%s\n" "$output"
130 # Output the commit message for the specified commit.
132 git cat-file commit "$1" | sed "1,/^$/d"
135 run_pre_rebase_hook () {
136 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
137 test -x "$GIT_DIR/hooks/pre-rebase"
139 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
140 echo >&2 "The pre-rebase hook refused to rebase."
146 require_clean_work_tree () {
147 # test if working tree is dirty
148 git rev-parse --verify HEAD > /dev/null &&
149 git update-index --ignore-submodules --refresh &&
150 git diff-files --quiet --ignore-submodules &&
151 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
152 die "Working tree is dirty"
155 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
157 comment_for_reflog () {
158 case "$ORIG_REFLOG_ACTION" in
160 GIT_REFLOG_ACTION="rebase -i ($1)"
161 export GIT_REFLOG_ACTION
167 mark_action_done () {
168 sed -e 1q < "$TODO" >> "$DONE"
169 sed -e 1d < "$TODO" >> "$TODO".new
170 mv -f "$TODO".new "$TODO"
171 count=$(sane_grep -c '^[^#]' < "$DONE")
172 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
173 if test "$last_count" != "$count"
176 printf "Rebasing (%d/%d)\r" $count $total
177 test -z "$VERBOSE" || echo
182 sha1_and_parents="$(git rev-list --parents -1 "$1")"
183 case "$sha1_and_parents" in
185 git diff --cc $sha1_and_parents
188 git diff-tree -p "$1^!"
193 esac > "$DOTEST"/patch
195 commit_message "$1" > "$MSG"
196 test -f "$AUTHOR_SCRIPT" ||
197 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
212 sane_grep '^[^#]' "$1" >/dev/null
215 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
216 # GIT_AUTHOR_DATE exported from the current environment.
219 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
226 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
227 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
228 test -d "$REWRITTEN" &&
229 pick_one_preserving_merges "$@" && return
230 if test -n "$REBASE_ROOT"
232 output git cherry-pick "$@"
235 parent_sha1=$(git rev-parse --verify $sha1^) ||
236 die "Could not get the parent of $sha1"
237 current_sha1=$(git rev-parse --verify HEAD)
238 if test -z "$no_ff" && test "$current_sha1" = "$parent_sha1"
240 output git reset --hard $sha1
241 output warn Fast-forward to $(git rev-parse --short $sha1)
243 output git cherry-pick "$@"
247 pick_one_preserving_merges () {
258 sha1=$(git rev-parse $sha1)
260 if test -f "$DOTEST"/current-commit
262 if test "$fast_forward" = t
264 cat "$DOTEST"/current-commit | while read current_commit
266 git rev-parse HEAD > "$REWRITTEN"/$current_commit
268 rm "$DOTEST"/current-commit ||
269 die "Cannot write current commit's replacement sha1"
273 echo $sha1 >> "$DOTEST"/current-commit
275 # rewrite parents; if none were rewritten, we can fast-forward.
277 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
278 if test "$pend" = " "
282 while [ "$pend" != "" ]
284 p=$(expr "$pend" : ' \([^ ]*\)')
287 if test -f "$REWRITTEN"/$p
289 new_p=$(cat "$REWRITTEN"/$p)
291 # If the todo reordered commits, and our parent is marked for
292 # rewriting, but hasn't been gotten to yet, assume the user meant to
293 # drop it on top of the current HEAD
296 new_p=$(git rev-parse HEAD)
299 test $p != $new_p && fast_forward=f
300 case "$new_parents" in
302 ;; # do nothing; that parent is already there
304 new_parents="$new_parents $new_p"
308 if test -f "$DROPPED"/$p
311 replacement="$(cat "$DROPPED"/$p)"
312 test -z "$replacement" && replacement=root
313 pend=" $replacement$pend"
315 new_parents="$new_parents $p"
319 case $fast_forward in
321 output warn "Fast-forward to $sha1"
322 output git reset --hard $sha1 ||
323 die "Cannot fast-forward to $sha1"
326 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
330 # detach HEAD to current parent
331 output git checkout $first_parent 2> /dev/null ||
332 die "Cannot move HEAD to $first_parent"
335 case "$new_parents" in
337 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
340 author_script=$(get_author_ident_from_commit $sha1)
341 eval "$author_script"
342 msg="$(commit_message $sha1)"
343 # No point in merging the first parent, that's HEAD
344 new_parents=${new_parents# $first_parent}
345 if ! do_with_author output \
346 git merge $STRATEGY -m "$msg" $new_parents
348 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
349 die_with_patch $sha1 "Error redoing merge $sha1"
353 output git cherry-pick "$@" ||
354 die_with_patch $sha1 "Could not pick $sha1"
363 *1[0-9]|*[04-9]) echo "$1"th;;
370 update_squash_messages () {
371 if test -f "$SQUASH_MSG"; then
372 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
374 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
375 -e "q" < "$SQUASH_MSG".bak)+1))
377 echo "# This is a combination of $COUNT commits."
378 sed -e 1d -e '2,/^./{
380 }' <"$SQUASH_MSG".bak
383 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
386 echo "# This is a combination of 2 commits."
387 echo "# The first commit's message is:"
396 echo "# This is the $(nth_string $COUNT) commit message:"
402 echo "# The $(nth_string $COUNT) commit message will be skipped:"
404 commit_message $2 | sed -e 's/^/# /'
409 peek_next_command () {
410 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
413 # A squash/fixup has failed. Prepare the long version of the squash
414 # commit message, then die_with_patch. This code path requires the
415 # user to edit the combined commit message for all commits that have
416 # been squashed/fixedup so far. So also erase the old squash
417 # messages, effectively causing the combined commit to be used as the
418 # new basis for any further squash/fixups. Args: sha1 rest
419 die_failed_squash() {
420 mv "$SQUASH_MSG" "$MSG" || exit
422 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
424 warn "Could not apply $1... $2"
429 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
430 read command sha1 rest < "$TODO"
436 comment_for_reflog pick
440 die_with_patch $sha1 "Could not apply $sha1... $rest"
443 comment_for_reflog reword
447 die_with_patch $sha1 "Could not apply $sha1... $rest"
451 comment_for_reflog edit
455 die_with_patch $sha1 "Could not apply $sha1... $rest"
457 git rev-parse --verify HEAD > "$AMEND"
458 warn "Stopped at $sha1... $rest"
459 warn "You can amend the commit now, with"
461 warn " git commit --amend"
463 warn "Once you are satisfied with your changes, run"
465 warn " git rebase --continue"
478 comment_for_reflog $squash_style
480 test -f "$DONE" && has_action "$DONE" ||
481 die "Cannot '$squash_style' without a previous commit"
484 update_squash_messages $squash_style $sha1
485 author_script=$(get_author_ident_from_commit HEAD)
486 echo "$author_script" > "$AUTHOR_SCRIPT"
487 eval "$author_script"
488 output git reset --soft HEAD^
489 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
490 case "$(peek_next_command)" in
492 # This is an intermediate commit; its message will only be
493 # used in case of trouble. So use the long version:
494 do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
495 die_failed_squash $sha1 "$rest"
498 # This is the final command of this squash/fixup group
499 if test -f "$FIXUP_MSG"
501 do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
502 die_failed_squash $sha1 "$rest"
504 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
505 rm -f "$GIT_DIR"/MERGE_MSG
506 do_with_author git commit --no-verify -e ||
507 die_failed_squash $sha1 "$rest"
509 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
514 warn "Unknown command: $command $sha1 $rest"
515 if git rev-parse --verify -q "$sha1" >/dev/null
517 die_with_patch $sha1 "Please fix this in the file $TODO."
519 die "Please fix this in the file $TODO."
523 test -s "$TODO" && return
525 comment_for_reflog finish &&
526 HEADNAME=$(cat "$DOTEST"/head-name) &&
527 OLDHEAD=$(cat "$DOTEST"/head) &&
528 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
529 NEWHEAD=$(git rev-parse HEAD) &&
532 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
533 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
534 git symbolic-ref HEAD $HEADNAME
537 test ! -f "$DOTEST"/verbose ||
538 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
542 warn "Successfully rebased and updated $HEADNAME."
554 # skip picking commits whose parents are unchanged
555 skip_unnecessary_picks () {
557 while read command sha1 rest
559 # fd=3 means we skip the command
560 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
561 3,pick,"$ONTO"*|3,p,"$ONTO"*)
562 # pick a commit whose parent is current $ONTO -> skip
572 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
573 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
574 mv -f "$TODO".new "$TODO" ||
575 die "Could not skip unnecessary pick commands"
578 # check if no other options are set
580 test $# -eq 2 -a "$2" = '--' &&
582 test -z "$PRESERVE_MERGES" &&
583 test -z "$STRATEGY" &&
587 get_saved_options () {
588 test -d "$REWRITTEN" && PRESERVE_MERGES=t
589 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
590 test -f "$DOTEST"/verbose && VERBOSE=t
591 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
594 # Rearrange the todo list that has both "pick sha1 msg" and
595 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
596 # comes immediately after the former, and change "pick" to
598 rearrange_squash () {
599 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
600 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
602 test -s "$1.sq" || return
605 while read pick sha1 message
608 *" $sha1 "*) continue ;;
610 echo "$pick $sha1 $message"
611 while read squash action msg
615 echo "$action $squash $action! $msg"
620 done >"$1.rearranged" <"$1"
621 cat "$1.rearranged" >"$1"
622 rm -f "$1.sq" "$1.rearranged"
630 if left=${1%...*} right=${1#*...} &&
631 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
641 git rev-parse --verify "$1^0"
648 OK_TO_SKIP_PRE_REBASE=yes
653 is_standalone "$@" || usage
655 comment_for_reflog continue
657 test -d "$DOTEST" || die "No interactive rebase running"
660 git rev-parse --verify HEAD >/dev/null ||
661 die "Cannot read HEAD"
662 git update-index --ignore-submodules --refresh &&
663 git diff-files --quiet --ignore-submodules ||
664 die "Working tree is dirty"
666 # do we have anything to commit?
667 if git diff-index --cached --quiet --ignore-submodules HEAD --
669 : Nothing to commit -- skip this
671 . "$AUTHOR_SCRIPT" ||
672 die "Cannot find the author identity"
676 amend=$(git rev-parse --verify HEAD)
677 test "$amend" = $(cat "$AMEND") ||
679 You have uncommitted changes in your working tree. Please, commit them
680 first and then run 'git rebase --continue' again."
681 git reset --soft HEAD^ ||
682 die "Cannot rewind the HEAD"
684 do_with_author git commit --no-verify -F "$MSG" -e || {
685 test -n "$amend" && git reset --soft $amend
686 die "Could not commit staged changes."
690 require_clean_work_tree
694 is_standalone "$@" || usage
696 comment_for_reflog abort
699 test -d "$DOTEST" || die "No interactive rebase running"
701 HEADNAME=$(cat "$DOTEST"/head-name)
702 HEAD=$(cat "$DOTEST"/head)
705 git symbolic-ref HEAD $HEADNAME
708 output git reset --hard $HEAD &&
713 is_standalone "$@" || usage
715 comment_for_reflog skip
718 test -d "$DOTEST" || die "No interactive rebase running"
720 output git reset --hard && do_rest
725 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
734 # we use merge anyway
753 ONTO=$(parse_onto "$1") ||
754 die "Does not point to a valid commit: $1"
758 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
759 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
761 die "Interactive rebase already started"
763 git var GIT_COMMITTER_IDENT >/dev/null ||
764 die "You need to set your committer info first"
766 if test -z "$REBASE_ROOT"
769 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
770 test -z "$ONTO" && ONTO=$UPSTREAM
776 die "You must specify --onto when using --root"
778 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
780 comment_for_reflog start
782 require_clean_work_tree
786 output git show-ref --verify --quiet "refs/heads/$1" ||
787 die "Invalid branchname: $1"
788 output git checkout "$1" ||
789 die "Could not checkout $1"
792 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
793 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
795 : > "$DOTEST"/interactive || die "Could not mark as interactive"
796 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
797 echo "detached HEAD" > "$DOTEST"/head-name
799 echo $HEAD > "$DOTEST"/head
800 case "$REBASE_ROOT" in
802 rm -f "$DOTEST"/rebase-root ;;
804 : >"$DOTEST"/rebase-root ;;
806 echo $ONTO > "$DOTEST"/onto
807 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
808 test t = "$VERBOSE" && : > "$DOTEST"/verbose
809 if test t = "$PRESERVE_MERGES"
811 if test -z "$REBASE_ROOT"
813 mkdir "$REWRITTEN" &&
814 for c in $(git merge-base --all $HEAD $UPSTREAM)
816 echo $ONTO > "$REWRITTEN"/$c ||
817 die "Could not init rewritten commits"
820 mkdir "$REWRITTEN" &&
821 echo $ONTO > "$REWRITTEN"/root ||
822 die "Could not init rewritten commits"
824 # No cherry-pick because our first pass is to determine
825 # parents to rewrite and skipping dropped commits would
826 # prematurely end our probe
828 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
830 MERGES_OPTION="--no-merges --cherry-pick"
833 SHORTHEAD=$(git rev-parse --short $HEAD)
834 SHORTONTO=$(git rev-parse --short $ONTO)
835 if test -z "$REBASE_ROOT"
836 # this is now equivalent to ! -z "$UPSTREAM"
838 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
839 REVISIONS=$UPSTREAM...$HEAD
840 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
842 REVISIONS=$ONTO...$HEAD
843 SHORTREVISIONS=$SHORTHEAD
845 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
846 --abbrev=7 --reverse --left-right --topo-order \
848 sed -n "s/^>//p" | while read shortsha1 rest
850 if test t != "$PRESERVE_MERGES"
852 echo "pick $shortsha1 $rest" >> "$TODO"
854 sha1=$(git rev-parse $shortsha1)
855 if test -z "$REBASE_ROOT"
858 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
860 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
868 if test f = "$preserve"
870 touch "$REWRITTEN"/$sha1
871 echo "pick $shortsha1 $rest" >> "$TODO"
876 # Watch for commits that been dropped by --cherry-pick
877 if test t = "$PRESERVE_MERGES"
880 # Save all non-cherry-picked changes
881 git rev-list $REVISIONS --left-right --cherry-pick | \
882 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
883 # Now all commits and note which ones are missing in
884 # not-cherry-picks and hence being dropped
885 git rev-list $REVISIONS |
888 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
890 # Use -f2 because if rev-list is telling us this commit is
891 # not worthwhile, we don't want to track its multiple heads,
892 # just the history of its first-parent for others that will
893 # be rebasing on top of it
894 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
895 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
896 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
902 test -s "$TODO" || echo noop >> "$TODO"
903 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
904 cat >> "$TODO" << EOF
906 # Rebase $SHORTREVISIONS onto $SHORTONTO
909 # p, pick = use commit
910 # r, reword = use commit, but edit the commit message
911 # e, edit = use commit, but stop for amending
912 # s, squash = use commit, but meld into previous commit
913 # f, fixup = like "squash", but discard this commit's log message
915 # If you remove a line here THAT COMMIT WILL BE LOST.
916 # However, if you remove everything, the rebase will be aborted.
920 has_action "$TODO" ||
921 die_abort "Nothing to do"
923 cp "$TODO" "$TODO".backup
924 git_editor "$TODO" ||
925 die_abort "Could not execute editor"
927 has_action "$TODO" ||
928 die_abort "Nothing to do"
930 test -d "$REWRITTEN" || skip_unnecessary_picks
932 git update-ref ORIG_HEAD $HEAD
933 output git checkout $ONTO && do_rest