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"
38 TODO="$DOTEST"/git-rebase-todo
41 SQUASH_MSG="$DOTEST"/message-squash
42 REWRITTEN="$DOTEST"/rewritten
43 DROPPED="$DOTEST"/dropped
48 OK_TO_SKIP_PRE_REBASE=
52 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
53 mark the corrected paths with 'git add <paths>', and
54 run 'git rebase --continue'"
55 export GIT_CHERRY_PICK_HELP
66 test $status != 0 && printf "%s\n" "$output"
75 run_pre_rebase_hook () {
76 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
77 test -x "$GIT_DIR/hooks/pre-rebase"
79 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
80 echo >&2 "The pre-rebase hook refused to rebase."
86 require_clean_work_tree () {
87 # test if working tree is dirty
88 git rev-parse --verify HEAD > /dev/null &&
89 git update-index --ignore-submodules --refresh &&
90 git diff-files --quiet --ignore-submodules &&
91 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
92 die "Working tree is dirty"
95 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
97 comment_for_reflog () {
98 case "$ORIG_REFLOG_ACTION" in
100 GIT_REFLOG_ACTION="rebase -i ($1)"
101 export GIT_REFLOG_ACTION
107 mark_action_done () {
108 sed -e 1q < "$TODO" >> "$DONE"
109 sed -e 1d < "$TODO" >> "$TODO".new
110 mv -f "$TODO".new "$TODO"
111 count=$(sane_grep -c '^[^#]' < "$DONE")
112 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
113 if test "$last_count" != "$count"
116 printf "Rebasing (%d/%d)\r" $count $total
117 test -z "$VERBOSE" || echo
122 sha1_and_parents="$(git rev-list --parents -1 "$1")"
123 case "$sha1_and_parents" in
125 git diff --cc $sha1_and_parents
128 git diff-tree -p "$1^!"
133 esac > "$DOTEST"/patch
134 test -f "$DOTEST"/message ||
135 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
136 test -f "$DOTEST"/author-script ||
137 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
152 sane_grep '^[^#]' "$1" >/dev/null
157 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
158 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
159 test -d "$REWRITTEN" &&
160 pick_one_preserving_merges "$@" && return
161 if test ! -z "$REBASE_ROOT"
163 output git cherry-pick "$@"
166 parent_sha1=$(git rev-parse --verify $sha1^) ||
167 die "Could not get the parent of $sha1"
168 current_sha1=$(git rev-parse --verify HEAD)
169 if test "$no_ff$current_sha1" = "$parent_sha1"; then
170 output git reset --hard $sha1
171 test "a$1" = a-n && output git reset --soft $current_sha1
172 sha1=$(git rev-parse --short $sha1)
173 output warn Fast-forward to $sha1
175 output git cherry-pick "$@"
179 pick_one_preserving_merges () {
190 sha1=$(git rev-parse $sha1)
192 if test -f "$DOTEST"/current-commit
194 if test "$fast_forward" = t
196 cat "$DOTEST"/current-commit | while read current_commit
198 git rev-parse HEAD > "$REWRITTEN"/$current_commit
200 rm "$DOTEST"/current-commit ||
201 die "Cannot write current commit's replacement sha1"
205 echo $sha1 >> "$DOTEST"/current-commit
207 # rewrite parents; if none were rewritten, we can fast-forward.
209 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
210 if test "$pend" = " "
214 while [ "$pend" != "" ]
216 p=$(expr "$pend" : ' \([^ ]*\)')
219 if test -f "$REWRITTEN"/$p
221 new_p=$(cat "$REWRITTEN"/$p)
223 # If the todo reordered commits, and our parent is marked for
224 # rewriting, but hasn't been gotten to yet, assume the user meant to
225 # drop it on top of the current HEAD
228 new_p=$(git rev-parse HEAD)
231 test $p != $new_p && fast_forward=f
232 case "$new_parents" in
234 ;; # do nothing; that parent is already there
236 new_parents="$new_parents $new_p"
240 if test -f "$DROPPED"/$p
243 replacement="$(cat "$DROPPED"/$p)"
244 test -z "$replacement" && replacement=root
245 pend=" $replacement$pend"
247 new_parents="$new_parents $p"
251 case $fast_forward in
253 output warn "Fast-forward to $sha1"
254 output git reset --hard $sha1 ||
255 die "Cannot fast-forward to $sha1"
258 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
262 # detach HEAD to current parent
263 output git checkout $first_parent 2> /dev/null ||
264 die "Cannot move HEAD to $first_parent"
267 case "$new_parents" in
269 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
272 author_script=$(get_author_ident_from_commit $sha1)
273 eval "$author_script"
274 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
275 # No point in merging the first parent, that's HEAD
276 new_parents=${new_parents# $first_parent}
277 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
278 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
279 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
280 output git merge $STRATEGY -m "$msg" \
283 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
284 die_with_patch $sha1 "Error redoing merge $sha1"
288 output git cherry-pick "$@" ||
289 die_with_patch $sha1 "Could not pick $sha1"
298 *1[0-9]|*[04-9]) echo "$1"th;;
305 make_squash_message () {
306 if test -f "$SQUASH_MSG"; then
307 # We want to be careful about matching only the commit
308 # message comment lines generated by this function.
309 # "[snrt][tdh]" matches the nth_string endings.
310 COUNT=$(($(sed -n "s/^# Th[^0-9]*\([1-9][0-9]*\)[snrt][tdh] commit message.*:/\1/p" \
311 < "$SQUASH_MSG" | sed -ne '$p')+1))
312 echo "# This is a combination of $COUNT commits."
313 sed -e 1d -e '2,/^./{
318 echo "# This is a combination of two commits."
319 echo "# The first commit's message is:"
321 git cat-file commit HEAD | sed -e '1,/^$/d'
326 echo "# This is the $(nth_string $COUNT) commit message:"
328 git cat-file commit $2 | sed -e '1,/^$/d'
332 echo "# The $(nth_string $COUNT) commit message will be skipped:"
334 # Comment the lines of the commit message out using
335 # "# " rather than "# " to make them less likely to
336 # confuse the sed regexp above.
337 git cat-file commit $2 | sed -e '1,/^$/d' -e 's/^/# /'
342 peek_next_command () {
343 sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO"
347 rm -f "$DOTEST"/message "$DOTEST"/author-script \
348 "$DOTEST"/amend || exit
349 read command sha1 rest < "$TODO"
355 comment_for_reflog pick
359 die_with_patch $sha1 "Could not apply $sha1... $rest"
362 comment_for_reflog reword
366 die_with_patch $sha1 "Could not apply $sha1... $rest"
370 comment_for_reflog edit
374 die_with_patch $sha1 "Could not apply $sha1... $rest"
376 git rev-parse --verify HEAD > "$DOTEST"/amend
377 warn "Stopped at $sha1... $rest"
378 warn "You can amend the commit now, with"
380 warn " git commit --amend"
382 warn "Once you are satisfied with your changes, run"
384 warn " git rebase --continue"
397 comment_for_reflog $squash_style
399 test -f "$DONE" && has_action "$DONE" ||
400 die "Cannot '$squash_style' without a previous commit"
403 make_squash_message $squash_style $sha1 > "$MSG"
405 author_script=$(get_author_ident_from_commit HEAD)
406 output git reset --soft HEAD^
407 pick_one -n $sha1 || failed=t
408 case "$(peek_next_command)" in
413 cp "$MSG" "$SQUASH_MSG"
419 rm -f "$SQUASH_MSG" || exit
420 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
421 rm -f "$GIT_DIR"/MERGE_MSG || exit
424 echo "$author_script" > "$DOTEST"/author-script
427 # This is like --amend, but with a different message
428 eval "$author_script"
429 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
430 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
431 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
432 $USE_OUTPUT git commit --no-verify \
433 $MSG_OPT "$EDIT_OR_FILE" || failed=t
437 cp "$MSG" "$GIT_DIR"/MERGE_MSG
439 warn "Could not apply $sha1... $rest"
440 die_with_patch $sha1 ""
444 warn "Unknown command: $command $sha1 $rest"
445 if git rev-parse --verify -q "$sha1" >/dev/null
447 die_with_patch $sha1 "Please fix this in the file $TODO."
449 die "Please fix this in the file $TODO."
453 test -s "$TODO" && return
455 comment_for_reflog finish &&
456 HEADNAME=$(cat "$DOTEST"/head-name) &&
457 OLDHEAD=$(cat "$DOTEST"/head) &&
458 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
459 NEWHEAD=$(git rev-parse HEAD) &&
462 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
463 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
464 git symbolic-ref HEAD $HEADNAME
467 test ! -f "$DOTEST"/verbose ||
468 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
472 warn "Successfully rebased and updated $HEADNAME."
484 # skip picking commits whose parents are unchanged
485 skip_unnecessary_picks () {
487 while read command sha1 rest
489 # fd=3 means we skip the command
490 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
491 3,pick,"$ONTO"*|3,p,"$ONTO"*)
492 # pick a commit whose parent is current $ONTO -> skip
502 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
503 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
504 mv -f "$TODO".new "$TODO" ||
505 die "Could not skip unnecessary pick commands"
508 # check if no other options are set
510 test $# -eq 2 -a "$2" = '--' &&
512 test -z "$PRESERVE_MERGES" &&
513 test -z "$STRATEGY" &&
517 get_saved_options () {
518 test -d "$REWRITTEN" && PRESERVE_MERGES=t
519 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
520 test -f "$DOTEST"/verbose && VERBOSE=t
521 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
524 # Rearrange the todo list that has both "pick sha1 msg" and
525 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
526 # comes immediately after the former, and change "pick" to
528 rearrange_squash () {
529 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
530 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
532 test -s "$1.sq" || return
535 while read pick sha1 message
538 *" $sha1 "*) continue ;;
540 echo "$pick $sha1 $message"
541 while read squash action msg
545 echo "$action $squash $action! $msg"
550 done >"$1.rearranged" <"$1"
551 cat "$1.rearranged" >"$1"
552 rm -f "$1.sq" "$1.rearranged"
560 if left=${1%...*} right=${1#*...} &&
561 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
571 git rev-parse --verify "$1^0"
578 OK_TO_SKIP_PRE_REBASE=yes
583 is_standalone "$@" || usage
585 comment_for_reflog continue
587 test -d "$DOTEST" || die "No interactive rebase running"
590 git rev-parse --verify HEAD >/dev/null ||
591 die "Cannot read HEAD"
592 git update-index --ignore-submodules --refresh &&
593 git diff-files --quiet --ignore-submodules ||
594 die "Working tree is dirty"
596 # do we have anything to commit?
597 if git diff-index --cached --quiet --ignore-submodules HEAD --
599 : Nothing to commit -- skip this
601 . "$DOTEST"/author-script ||
602 die "Cannot find the author identity"
604 if test -f "$DOTEST"/amend
606 amend=$(git rev-parse --verify HEAD)
607 test "$amend" = $(cat "$DOTEST"/amend) ||
609 You have uncommitted changes in your working tree. Please, commit them
610 first and then run 'git rebase --continue' again."
611 git reset --soft HEAD^ ||
612 die "Cannot rewind the HEAD"
614 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
615 git commit --no-verify -F "$DOTEST"/message -e || {
616 test -n "$amend" && git reset --soft $amend
617 die "Could not commit staged changes."
621 require_clean_work_tree
625 is_standalone "$@" || usage
627 comment_for_reflog abort
630 test -d "$DOTEST" || die "No interactive rebase running"
632 HEADNAME=$(cat "$DOTEST"/head-name)
633 HEAD=$(cat "$DOTEST"/head)
636 git symbolic-ref HEAD $HEADNAME
639 output git reset --hard $HEAD &&
644 is_standalone "$@" || usage
646 comment_for_reflog skip
649 test -d "$DOTEST" || die "No interactive rebase running"
651 output git reset --hard && do_rest
656 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
665 # we use merge anyway
684 ONTO=$(parse_onto "$1") ||
685 die "Does not point to a valid commit: $1"
689 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
690 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
692 die "Interactive rebase already started"
694 git var GIT_COMMITTER_IDENT >/dev/null ||
695 die "You need to set your committer info first"
697 if test -z "$REBASE_ROOT"
700 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
701 test -z "$ONTO" && ONTO=$UPSTREAM
707 die "You must specify --onto when using --root"
709 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
711 comment_for_reflog start
713 require_clean_work_tree
717 output git show-ref --verify --quiet "refs/heads/$1" ||
718 die "Invalid branchname: $1"
719 output git checkout "$1" ||
720 die "Could not checkout $1"
723 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
724 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
726 : > "$DOTEST"/interactive || die "Could not mark as interactive"
727 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
728 echo "detached HEAD" > "$DOTEST"/head-name
730 echo $HEAD > "$DOTEST"/head
731 case "$REBASE_ROOT" in
733 rm -f "$DOTEST"/rebase-root ;;
735 : >"$DOTEST"/rebase-root ;;
737 echo $ONTO > "$DOTEST"/onto
738 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
739 test t = "$VERBOSE" && : > "$DOTEST"/verbose
740 if test t = "$PRESERVE_MERGES"
742 # $REWRITTEN contains files for each commit that is
743 # reachable by at least one merge base of $HEAD and
744 # $UPSTREAM. They are not necessarily rewritten, but
745 # their children might be.
746 # This ensures that commits on merged, but otherwise
747 # unrelated side branches are left alone. (Think "X"
748 # in the man page's example.)
749 if test -z "$REBASE_ROOT"
751 mkdir "$REWRITTEN" &&
752 for c in $(git merge-base --all $HEAD $UPSTREAM)
754 echo $ONTO > "$REWRITTEN"/$c ||
755 die "Could not init rewritten commits"
758 mkdir "$REWRITTEN" &&
759 echo $ONTO > "$REWRITTEN"/root ||
760 die "Could not init rewritten commits"
762 # No cherry-pick because our first pass is to determine
763 # parents to rewrite and skipping dropped commits would
764 # prematurely end our probe
766 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
768 MERGES_OPTION="--no-merges --cherry-pick"
771 SHORTHEAD=$(git rev-parse --short $HEAD)
772 SHORTONTO=$(git rev-parse --short $ONTO)
773 if test -z "$REBASE_ROOT"
774 # this is now equivalent to ! -z "$UPSTREAM"
776 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
777 REVISIONS=$UPSTREAM...$HEAD
778 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
780 REVISIONS=$ONTO...$HEAD
781 SHORTREVISIONS=$SHORTHEAD
783 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
784 --abbrev=7 --reverse --left-right --topo-order \
786 sed -n "s/^>//p" | while read shortsha1 rest
788 if test t != "$PRESERVE_MERGES"
790 echo "pick $shortsha1 $rest" >> "$TODO"
792 sha1=$(git rev-parse $shortsha1)
793 if test -z "$REBASE_ROOT"
796 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
798 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
806 if test f = "$preserve"
808 touch "$REWRITTEN"/$sha1
809 echo "pick $shortsha1 $rest" >> "$TODO"
814 # Watch for commits that been dropped by --cherry-pick
815 if test t = "$PRESERVE_MERGES"
818 # Save all non-cherry-picked changes
819 git rev-list $REVISIONS --left-right --cherry-pick | \
820 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
821 # Now all commits and note which ones are missing in
822 # not-cherry-picks and hence being dropped
823 git rev-list $REVISIONS |
826 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
828 # Use -f2 because if rev-list is telling us this commit is
829 # not worthwhile, we don't want to track its multiple heads,
830 # just the history of its first-parent for others that will
831 # be rebasing on top of it
832 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
833 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
834 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
840 test -s "$TODO" || echo noop >> "$TODO"
841 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
842 cat >> "$TODO" << EOF
844 # Rebase $SHORTREVISIONS onto $SHORTONTO
847 # p, pick = use commit
848 # r, reword = use commit, but edit the commit message
849 # e, edit = use commit, but stop for amending
850 # s, squash = use commit, but meld into previous commit
851 # f, fixup = like "squash", but discard this commit's log message
853 # If you remove a line here THAT COMMIT WILL BE LOST.
854 # However, if you remove everything, the rebase will be aborted.
858 has_action "$TODO" ||
859 die_abort "Nothing to do"
861 cp "$TODO" "$TODO".backup
862 git_editor "$TODO" ||
863 die_abort "Could not execute editor"
865 has_action "$TODO" ||
866 die_abort "Nothing to do"
868 test -d "$REWRITTEN" || skip_unnecessary_picks
870 git update-ref ORIG_HEAD $HEAD
871 output git checkout $ONTO && do_rest