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)
36 DOTEST="$GIT_DIR/rebase-merge"
38 # The file containing rebase commands, comments, and empty lines.
39 # This file is created by "git rebase -i" then edited by the user. As
40 # the lines are processed, they are removed from the front of this
41 # file and written to the tail of $DONE.
42 TODO="$DOTEST"/git-rebase-todo
44 # The rebase command lines that have already been processed. A line
45 # is moved here when it is first handled, before any associated user
49 # The commit message that is planned to be used for any changes that
50 # need to be committed following a user interaction.
53 # The file into which is accumulated the suggested commit message for
54 # squash/fixup commands. When the first of a series of squash/fixups
55 # is seen, the file is created and the commit message from the
56 # previous commit and from the first squash/fixup commit are written
57 # to it. The commit message for each subsequent squash/fixup commit
58 # is appended to the file as it is processed.
60 # The first line of the file is of the form
61 # # This is a combination of $COUNT commits.
62 # where $COUNT is the number of commits whose messages have been
63 # written to the file so far (including the initial "pick" commit).
64 # Each time that a commit message is processed, this line is read and
65 # updated. It is deleted just before the combined commit is made.
66 SQUASH_MSG="$DOTEST"/message-squash
68 # $REWRITTEN is the name of a directory containing files for each
69 # commit that is reachable by at least one merge base of $HEAD and
70 # $UPSTREAM. They are not necessarily rewritten, but their children
71 # might be. This ensures that commits on merged, but otherwise
72 # unrelated side branches are left alone. (Think "X" in the man page's
74 REWRITTEN="$DOTEST"/rewritten
76 DROPPED="$DOTEST"/dropped
81 OK_TO_SKIP_PRE_REBASE=
84 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
85 mark the corrected paths with 'git add <paths>', and
86 run 'git rebase --continue'"
87 export GIT_CHERRY_PICK_HELP
98 test $status != 0 && printf "%s\n" "$output"
107 run_pre_rebase_hook () {
108 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
109 test -x "$GIT_DIR/hooks/pre-rebase"
111 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
112 echo >&2 "The pre-rebase hook refused to rebase."
118 require_clean_work_tree () {
119 # test if working tree is dirty
120 git rev-parse --verify HEAD > /dev/null &&
121 git update-index --ignore-submodules --refresh &&
122 git diff-files --quiet --ignore-submodules &&
123 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
124 die "Working tree is dirty"
127 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
129 comment_for_reflog () {
130 case "$ORIG_REFLOG_ACTION" in
132 GIT_REFLOG_ACTION="rebase -i ($1)"
133 export GIT_REFLOG_ACTION
139 mark_action_done () {
140 sed -e 1q < "$TODO" >> "$DONE"
141 sed -e 1d < "$TODO" >> "$TODO".new
142 mv -f "$TODO".new "$TODO"
143 count=$(sane_grep -c '^[^#]' < "$DONE")
144 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
145 if test "$last_count" != "$count"
148 printf "Rebasing (%d/%d)\r" $count $total
149 test -z "$VERBOSE" || echo
154 sha1_and_parents="$(git rev-list --parents -1 "$1")"
155 case "$sha1_and_parents" in
157 git diff --cc $sha1_and_parents
160 git diff-tree -p "$1^!"
165 esac > "$DOTEST"/patch
167 git cat-file commit "$1" | sed "1,/^$/d" > "$MSG"
168 test -f "$DOTEST"/author-script ||
169 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
184 sane_grep '^[^#]' "$1" >/dev/null
189 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
190 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
191 test -d "$REWRITTEN" &&
192 pick_one_preserving_merges "$@" && return
193 if test -n "$REBASE_ROOT"
195 output git cherry-pick "$@"
198 parent_sha1=$(git rev-parse --verify $sha1^) ||
199 die "Could not get the parent of $sha1"
200 current_sha1=$(git rev-parse --verify HEAD)
201 if test -z "$no_ff" -a "$current_sha1" = "$parent_sha1"
203 output git reset --hard $sha1
204 output warn Fast-forward to $(git rev-parse --short $sha1)
206 output git cherry-pick "$@"
210 pick_one_preserving_merges () {
221 sha1=$(git rev-parse $sha1)
223 if test -f "$DOTEST"/current-commit
225 if test "$fast_forward" = t
227 cat "$DOTEST"/current-commit | while read current_commit
229 git rev-parse HEAD > "$REWRITTEN"/$current_commit
231 rm "$DOTEST"/current-commit ||
232 die "Cannot write current commit's replacement sha1"
236 echo $sha1 >> "$DOTEST"/current-commit
238 # rewrite parents; if none were rewritten, we can fast-forward.
240 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
241 if test "$pend" = " "
245 while [ "$pend" != "" ]
247 p=$(expr "$pend" : ' \([^ ]*\)')
250 if test -f "$REWRITTEN"/$p
252 new_p=$(cat "$REWRITTEN"/$p)
254 # If the todo reordered commits, and our parent is marked for
255 # rewriting, but hasn't been gotten to yet, assume the user meant to
256 # drop it on top of the current HEAD
259 new_p=$(git rev-parse HEAD)
262 test $p != $new_p && fast_forward=f
263 case "$new_parents" in
265 ;; # do nothing; that parent is already there
267 new_parents="$new_parents $new_p"
271 if test -f "$DROPPED"/$p
274 replacement="$(cat "$DROPPED"/$p)"
275 test -z "$replacement" && replacement=root
276 pend=" $replacement$pend"
278 new_parents="$new_parents $p"
282 case $fast_forward in
284 output warn "Fast-forward to $sha1"
285 output git reset --hard $sha1 ||
286 die "Cannot fast-forward to $sha1"
289 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
293 # detach HEAD to current parent
294 output git checkout $first_parent 2> /dev/null ||
295 die "Cannot move HEAD to $first_parent"
298 case "$new_parents" in
300 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
303 author_script=$(get_author_ident_from_commit $sha1)
304 eval "$author_script"
305 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
306 # No point in merging the first parent, that's HEAD
307 new_parents=${new_parents# $first_parent}
308 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
309 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
310 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
311 output git merge $STRATEGY -m "$msg" \
314 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
315 die_with_patch $sha1 "Error redoing merge $sha1"
319 output git cherry-pick "$@" ||
320 die_with_patch $sha1 "Could not pick $sha1"
329 *1[0-9]|*[04-9]) echo "$1"th;;
336 make_squash_message () {
337 if test -f "$SQUASH_MSG"; then
338 # We want to be careful about matching only the commit
339 # message comment lines generated by this function.
340 # "[snrt][tdh]" matches the nth_string endings.
341 COUNT=$(($(sed -n "s/^# Th[^0-9]*\([1-9][0-9]*\)[snrt][tdh] commit message.*:/\1/p" \
342 < "$SQUASH_MSG" | sed -ne '$p')+1))
343 echo "# This is a combination of $COUNT commits."
344 sed -e 1d -e '2,/^./{
349 echo "# This is a combination of two commits."
350 echo "# The first commit's message is:"
352 git cat-file commit HEAD | sed -e '1,/^$/d'
357 echo "# This is the $(nth_string $COUNT) commit message:"
359 git cat-file commit $2 | sed -e '1,/^$/d'
363 echo "# The $(nth_string $COUNT) commit message will be skipped:"
365 # Comment the lines of the commit message out using
366 # "# " rather than "# " to make them less likely to
367 # confuse the sed regexp above.
368 git cat-file commit $2 | sed -e '1,/^$/d' -e 's/^/# /'
373 peek_next_command () {
374 sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO"
378 rm -f "$MSG" "$DOTEST"/author-script \
379 "$DOTEST"/amend || exit
380 read command sha1 rest < "$TODO"
386 comment_for_reflog pick
390 die_with_patch $sha1 "Could not apply $sha1... $rest"
393 comment_for_reflog reword
397 die_with_patch $sha1 "Could not apply $sha1... $rest"
401 comment_for_reflog edit
405 die_with_patch $sha1 "Could not apply $sha1... $rest"
407 git rev-parse --verify HEAD > "$DOTEST"/amend
408 warn "Stopped at $sha1... $rest"
409 warn "You can amend the commit now, with"
411 warn " git commit --amend"
413 warn "Once you are satisfied with your changes, run"
415 warn " git rebase --continue"
428 comment_for_reflog $squash_style
430 test -f "$DONE" && has_action "$DONE" ||
431 die "Cannot '$squash_style' without a previous commit"
434 make_squash_message $squash_style $sha1 > "$MSG"
436 author_script=$(get_author_ident_from_commit HEAD)
437 output git reset --soft HEAD^
438 pick_one -n $sha1 || failed=t
439 case "$(peek_next_command)" in
444 cp "$MSG" "$SQUASH_MSG"
450 rm -f "$SQUASH_MSG" || exit
451 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
452 rm -f "$GIT_DIR"/MERGE_MSG || exit
455 echo "$author_script" > "$DOTEST"/author-script
458 # This is like --amend, but with a different message
459 eval "$author_script"
460 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
461 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
462 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
463 $USE_OUTPUT git commit --no-verify \
464 $MSG_OPT "$EDIT_OR_FILE" || failed=t
468 cp "$MSG" "$GIT_DIR"/MERGE_MSG
470 warn "Could not apply $sha1... $rest"
471 die_with_patch $sha1 ""
475 warn "Unknown command: $command $sha1 $rest"
476 if git rev-parse --verify -q "$sha1" >/dev/null
478 die_with_patch $sha1 "Please fix this in the file $TODO."
480 die "Please fix this in the file $TODO."
484 test -s "$TODO" && return
486 comment_for_reflog finish &&
487 HEADNAME=$(cat "$DOTEST"/head-name) &&
488 OLDHEAD=$(cat "$DOTEST"/head) &&
489 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
490 NEWHEAD=$(git rev-parse HEAD) &&
493 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
494 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
495 git symbolic-ref HEAD $HEADNAME
498 test ! -f "$DOTEST"/verbose ||
499 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
503 warn "Successfully rebased and updated $HEADNAME."
515 # skip picking commits whose parents are unchanged
516 skip_unnecessary_picks () {
518 while read command sha1 rest
520 # fd=3 means we skip the command
521 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
522 3,pick,"$ONTO"*|3,p,"$ONTO"*)
523 # pick a commit whose parent is current $ONTO -> skip
533 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
534 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
535 mv -f "$TODO".new "$TODO" ||
536 die "Could not skip unnecessary pick commands"
539 # check if no other options are set
541 test $# -eq 2 -a "$2" = '--' &&
543 test -z "$PRESERVE_MERGES" &&
544 test -z "$STRATEGY" &&
548 get_saved_options () {
549 test -d "$REWRITTEN" && PRESERVE_MERGES=t
550 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
551 test -f "$DOTEST"/verbose && VERBOSE=t
552 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
559 OK_TO_SKIP_PRE_REBASE=yes
564 is_standalone "$@" || usage
566 comment_for_reflog continue
568 test -d "$DOTEST" || die "No interactive rebase running"
571 git rev-parse --verify HEAD >/dev/null ||
572 die "Cannot read HEAD"
573 git update-index --ignore-submodules --refresh &&
574 git diff-files --quiet --ignore-submodules ||
575 die "Working tree is dirty"
577 # do we have anything to commit?
578 if git diff-index --cached --quiet --ignore-submodules HEAD --
580 : Nothing to commit -- skip this
582 . "$DOTEST"/author-script ||
583 die "Cannot find the author identity"
585 if test -f "$DOTEST"/amend
587 amend=$(git rev-parse --verify HEAD)
588 test "$amend" = $(cat "$DOTEST"/amend) ||
590 You have uncommitted changes in your working tree. Please, commit them
591 first and then run 'git rebase --continue' again."
592 git reset --soft HEAD^ ||
593 die "Cannot rewind the HEAD"
595 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
596 git commit --no-verify -F "$MSG" -e || {
597 test -n "$amend" && git reset --soft $amend
598 die "Could not commit staged changes."
602 require_clean_work_tree
606 is_standalone "$@" || usage
608 comment_for_reflog abort
611 test -d "$DOTEST" || die "No interactive rebase running"
613 HEADNAME=$(cat "$DOTEST"/head-name)
614 HEAD=$(cat "$DOTEST"/head)
617 git symbolic-ref HEAD $HEADNAME
620 output git reset --hard $HEAD &&
625 is_standalone "$@" || usage
627 comment_for_reflog skip
630 test -d "$DOTEST" || die "No interactive rebase running"
632 output git reset --hard && do_rest
637 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
646 # we use merge anyway
662 ONTO=$(git rev-parse --verify "$1") ||
663 die "Does not point to a valid commit: $1"
667 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
668 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
670 die "Interactive rebase already started"
672 git var GIT_COMMITTER_IDENT >/dev/null ||
673 die "You need to set your committer info first"
675 if test -z "$REBASE_ROOT"
678 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
679 test -z "$ONTO" && ONTO=$UPSTREAM
685 die "You must specify --onto when using --root"
687 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
689 comment_for_reflog start
691 require_clean_work_tree
695 output git show-ref --verify --quiet "refs/heads/$1" ||
696 die "Invalid branchname: $1"
697 output git checkout "$1" ||
698 die "Could not checkout $1"
701 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
702 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
704 : > "$DOTEST"/interactive || die "Could not mark as interactive"
705 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
706 echo "detached HEAD" > "$DOTEST"/head-name
708 echo $HEAD > "$DOTEST"/head
709 case "$REBASE_ROOT" in
711 rm -f "$DOTEST"/rebase-root ;;
713 : >"$DOTEST"/rebase-root ;;
715 echo $ONTO > "$DOTEST"/onto
716 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
717 test t = "$VERBOSE" && : > "$DOTEST"/verbose
718 if test t = "$PRESERVE_MERGES"
720 if test -z "$REBASE_ROOT"
722 mkdir "$REWRITTEN" &&
723 for c in $(git merge-base --all $HEAD $UPSTREAM)
725 echo $ONTO > "$REWRITTEN"/$c ||
726 die "Could not init rewritten commits"
729 mkdir "$REWRITTEN" &&
730 echo $ONTO > "$REWRITTEN"/root ||
731 die "Could not init rewritten commits"
733 # No cherry-pick because our first pass is to determine
734 # parents to rewrite and skipping dropped commits would
735 # prematurely end our probe
737 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
739 MERGES_OPTION="--no-merges --cherry-pick"
742 SHORTHEAD=$(git rev-parse --short $HEAD)
743 SHORTONTO=$(git rev-parse --short $ONTO)
744 if test -z "$REBASE_ROOT"
745 # this is now equivalent to ! -z "$UPSTREAM"
747 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
748 REVISIONS=$UPSTREAM...$HEAD
749 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
751 REVISIONS=$ONTO...$HEAD
752 SHORTREVISIONS=$SHORTHEAD
754 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
755 --abbrev=7 --reverse --left-right --topo-order \
757 sed -n "s/^>//p" | while read shortsha1 rest
759 if test t != "$PRESERVE_MERGES"
761 echo "pick $shortsha1 $rest" >> "$TODO"
763 sha1=$(git rev-parse $shortsha1)
764 if test -z "$REBASE_ROOT"
767 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
769 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
777 if test f = "$preserve"
779 touch "$REWRITTEN"/$sha1
780 echo "pick $shortsha1 $rest" >> "$TODO"
785 # Watch for commits that been dropped by --cherry-pick
786 if test t = "$PRESERVE_MERGES"
789 # Save all non-cherry-picked changes
790 git rev-list $REVISIONS --left-right --cherry-pick | \
791 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
792 # Now all commits and note which ones are missing in
793 # not-cherry-picks and hence being dropped
794 git rev-list $REVISIONS |
797 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
799 # Use -f2 because if rev-list is telling us this commit is
800 # not worthwhile, we don't want to track its multiple heads,
801 # just the history of its first-parent for others that will
802 # be rebasing on top of it
803 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
804 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
805 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
811 test -s "$TODO" || echo noop >> "$TODO"
812 cat >> "$TODO" << EOF
814 # Rebase $SHORTREVISIONS onto $SHORTONTO
817 # p, pick = use commit
818 # r, reword = use commit, but edit the commit message
819 # e, edit = use commit, but stop for amending
820 # s, squash = use commit, but meld into previous commit
821 # f, fixup = like "squash", but discard this commit's log message
823 # If you remove a line here THAT COMMIT WILL BE LOST.
824 # However, if you remove everything, the rebase will be aborted.
828 has_action "$TODO" ||
829 die_abort "Nothing to do"
831 cp "$TODO" "$TODO".backup
832 git_editor "$TODO" ||
833 die "Could not execute editor"
835 has_action "$TODO" ||
836 die_abort "Nothing to do"
838 test -d "$REWRITTEN" || skip_unnecessary_picks
840 git update-ref ORIG_HEAD $HEAD
841 output git checkout $ONTO && do_rest