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 "1s/ .*$//p" < "$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"
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 "$DOTEST"/message -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
665 ONTO=$(git rev-parse --verify "$1") ||
666 die "Does not point to a valid commit: $1"
670 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
671 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
673 die "Interactive rebase already started"
675 git var GIT_COMMITTER_IDENT >/dev/null ||
676 die "You need to set your committer info first"
678 if test -z "$REBASE_ROOT"
681 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
682 test -z "$ONTO" && ONTO=$UPSTREAM
688 die "You must specify --onto when using --root"
690 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
692 comment_for_reflog start
694 require_clean_work_tree
698 output git show-ref --verify --quiet "refs/heads/$1" ||
699 die "Invalid branchname: $1"
700 output git checkout "$1" ||
701 die "Could not checkout $1"
704 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
705 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
707 : > "$DOTEST"/interactive || die "Could not mark as interactive"
708 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
709 echo "detached HEAD" > "$DOTEST"/head-name
711 echo $HEAD > "$DOTEST"/head
712 case "$REBASE_ROOT" in
714 rm -f "$DOTEST"/rebase-root ;;
716 : >"$DOTEST"/rebase-root ;;
718 echo $ONTO > "$DOTEST"/onto
719 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
720 test t = "$VERBOSE" && : > "$DOTEST"/verbose
721 if test t = "$PRESERVE_MERGES"
723 # $REWRITTEN contains files for each commit that is
724 # reachable by at least one merge base of $HEAD and
725 # $UPSTREAM. They are not necessarily rewritten, but
726 # their children might be.
727 # This ensures that commits on merged, but otherwise
728 # unrelated side branches are left alone. (Think "X"
729 # in the man page's example.)
730 if test -z "$REBASE_ROOT"
732 mkdir "$REWRITTEN" &&
733 for c in $(git merge-base --all $HEAD $UPSTREAM)
735 echo $ONTO > "$REWRITTEN"/$c ||
736 die "Could not init rewritten commits"
739 mkdir "$REWRITTEN" &&
740 echo $ONTO > "$REWRITTEN"/root ||
741 die "Could not init rewritten commits"
743 # No cherry-pick because our first pass is to determine
744 # parents to rewrite and skipping dropped commits would
745 # prematurely end our probe
747 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
749 MERGES_OPTION="--no-merges --cherry-pick"
752 SHORTHEAD=$(git rev-parse --short $HEAD)
753 SHORTONTO=$(git rev-parse --short $ONTO)
754 if test -z "$REBASE_ROOT"
755 # this is now equivalent to ! -z "$UPSTREAM"
757 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
758 REVISIONS=$UPSTREAM...$HEAD
759 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
761 REVISIONS=$ONTO...$HEAD
762 SHORTREVISIONS=$SHORTHEAD
764 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
765 --abbrev=7 --reverse --left-right --topo-order \
767 sed -n "s/^>//p" | while read shortsha1 rest
769 if test t != "$PRESERVE_MERGES"
771 echo "pick $shortsha1 $rest" >> "$TODO"
773 sha1=$(git rev-parse $shortsha1)
774 if test -z "$REBASE_ROOT"
777 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
779 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
787 if test f = "$preserve"
789 touch "$REWRITTEN"/$sha1
790 echo "pick $shortsha1 $rest" >> "$TODO"
795 # Watch for commits that been dropped by --cherry-pick
796 if test t = "$PRESERVE_MERGES"
799 # Save all non-cherry-picked changes
800 git rev-list $REVISIONS --left-right --cherry-pick | \
801 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
802 # Now all commits and note which ones are missing in
803 # not-cherry-picks and hence being dropped
804 git rev-list $REVISIONS |
807 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
809 # Use -f2 because if rev-list is telling us this commit is
810 # not worthwhile, we don't want to track its multiple heads,
811 # just the history of its first-parent for others that will
812 # be rebasing on top of it
813 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
814 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
815 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
821 test -s "$TODO" || echo noop >> "$TODO"
822 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
823 cat >> "$TODO" << EOF
825 # Rebase $SHORTREVISIONS onto $SHORTONTO
828 # p, pick = use commit
829 # r, reword = use commit, but edit the commit message
830 # e, edit = use commit, but stop for amending
831 # s, squash = use commit, but meld into previous commit
832 # f, fixup = like "squash", but discard this commit's log message
834 # If you remove a line here THAT COMMIT WILL BE LOST.
835 # However, if you remove everything, the rebase will be aborted.
839 has_action "$TODO" ||
840 die_abort "Nothing to do"
842 cp "$TODO" "$TODO".backup
843 git_editor "$TODO" ||
844 die "Could not execute editor"
846 has_action "$TODO" ||
847 die_abort "Nothing to do"
849 test -d "$REWRITTEN" || skip_unnecessary_picks
851 git update-ref ORIG_HEAD $HEAD
852 output git checkout $ONTO && do_rest