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"
37 TODO="$DOTEST"/git-rebase-todo
40 SQUASH_MSG="$DOTEST"/message-squash
41 REWRITTEN="$DOTEST"/rewritten
42 DROPPED="$DOTEST"/dropped
47 OK_TO_SKIP_PRE_REBASE=
50 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
51 mark the corrected paths with 'git add <paths>', and
52 run 'git rebase --continue'"
53 export GIT_CHERRY_PICK_HELP
64 test $status != 0 && printf "%s\n" "$output"
73 run_pre_rebase_hook () {
74 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
75 test -x "$GIT_DIR/hooks/pre-rebase"
77 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
78 echo >&2 "The pre-rebase hook refused to rebase."
84 require_clean_work_tree () {
85 # test if working tree is dirty
86 git rev-parse --verify HEAD > /dev/null &&
87 git update-index --ignore-submodules --refresh &&
88 git diff-files --quiet --ignore-submodules &&
89 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
90 die "Working tree is dirty"
93 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
95 comment_for_reflog () {
96 case "$ORIG_REFLOG_ACTION" in
98 GIT_REFLOG_ACTION="rebase -i ($1)"
99 export GIT_REFLOG_ACTION
105 mark_action_done () {
106 sed -e 1q < "$TODO" >> "$DONE"
107 sed -e 1d < "$TODO" >> "$TODO".new
108 mv -f "$TODO".new "$TODO"
109 count=$(sane_grep -c '^[^#]' < "$DONE")
110 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
111 if test "$last_count" != "$count"
114 printf "Rebasing (%d/%d)\r" $count $total
115 test -z "$VERBOSE" || echo
120 sha1_and_parents="$(git rev-list --parents -1 "$1")"
121 case "$sha1_and_parents" in
123 git diff --cc $sha1_and_parents
126 git diff-tree -p "$1^!"
131 esac > "$DOTEST"/patch
132 test -f "$DOTEST"/message ||
133 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
134 test -f "$DOTEST"/author-script ||
135 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
150 sane_grep '^[^#]' "$1" >/dev/null
155 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
156 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
157 test -d "$REWRITTEN" &&
158 pick_one_preserving_merges "$@" && return
159 if test ! -z "$REBASE_ROOT"
161 output git cherry-pick "$@"
164 parent_sha1=$(git rev-parse --verify $sha1^) ||
165 die "Could not get the parent of $sha1"
166 current_sha1=$(git rev-parse --verify HEAD)
167 if test -z "$no_ff" -a "$current_sha1" = "$parent_sha1"
169 output git reset --hard $sha1
170 test "a$1" = a-n && output git reset --soft $current_sha1
171 sha1=$(git rev-parse --short $sha1)
172 output warn Fast-forward to $sha1
174 output git cherry-pick "$@"
178 pick_one_preserving_merges () {
189 sha1=$(git rev-parse $sha1)
191 if test -f "$DOTEST"/current-commit
193 if test "$fast_forward" = t
195 cat "$DOTEST"/current-commit | while read current_commit
197 git rev-parse HEAD > "$REWRITTEN"/$current_commit
199 rm "$DOTEST"/current-commit ||
200 die "Cannot write current commit's replacement sha1"
204 echo $sha1 >> "$DOTEST"/current-commit
206 # rewrite parents; if none were rewritten, we can fast-forward.
208 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
209 if test "$pend" = " "
213 while [ "$pend" != "" ]
215 p=$(expr "$pend" : ' \([^ ]*\)')
218 if test -f "$REWRITTEN"/$p
220 new_p=$(cat "$REWRITTEN"/$p)
222 # If the todo reordered commits, and our parent is marked for
223 # rewriting, but hasn't been gotten to yet, assume the user meant to
224 # drop it on top of the current HEAD
227 new_p=$(git rev-parse HEAD)
230 test $p != $new_p && fast_forward=f
231 case "$new_parents" in
233 ;; # do nothing; that parent is already there
235 new_parents="$new_parents $new_p"
239 if test -f "$DROPPED"/$p
242 replacement="$(cat "$DROPPED"/$p)"
243 test -z "$replacement" && replacement=root
244 pend=" $replacement$pend"
246 new_parents="$new_parents $p"
250 case $fast_forward in
252 output warn "Fast-forward to $sha1"
253 output git reset --hard $sha1 ||
254 die "Cannot fast-forward to $sha1"
257 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
261 # detach HEAD to current parent
262 output git checkout $first_parent 2> /dev/null ||
263 die "Cannot move HEAD to $first_parent"
266 case "$new_parents" in
268 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
271 author_script=$(get_author_ident_from_commit $sha1)
272 eval "$author_script"
273 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
274 # No point in merging the first parent, that's HEAD
275 new_parents=${new_parents# $first_parent}
276 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
277 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
278 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
279 output git merge $STRATEGY -m "$msg" \
282 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
283 die_with_patch $sha1 "Error redoing merge $sha1"
287 output git cherry-pick "$@" ||
288 die_with_patch $sha1 "Could not pick $sha1"
297 *1[0-9]|*[04-9]) echo "$1"th;;
304 make_squash_message () {
305 if test -f "$SQUASH_MSG"; then
306 # We want to be careful about matching only the commit
307 # message comment lines generated by this function.
308 # "[snrt][tdh]" matches the nth_string endings.
309 COUNT=$(($(sed -n "s/^# Th[^0-9]*\([1-9][0-9]*\)[snrt][tdh] commit message.*:/\1/p" \
310 < "$SQUASH_MSG" | sed -ne '$p')+1))
311 echo "# This is a combination of $COUNT commits."
312 sed -e 1d -e '2,/^./{
317 echo "# This is a combination of two commits."
318 echo "# The first commit's message is:"
320 git cat-file commit HEAD | sed -e '1,/^$/d'
325 echo "# This is the $(nth_string $COUNT) commit message:"
327 git cat-file commit $2 | sed -e '1,/^$/d'
331 echo "# The $(nth_string $COUNT) commit message will be skipped:"
333 # Comment the lines of the commit message out using
334 # "# " rather than "# " to make them less likely to
335 # confuse the sed regexp above.
336 git cat-file commit $2 | sed -e '1,/^$/d' -e 's/^/# /'
341 peek_next_command () {
342 sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO"
346 rm -f "$DOTEST"/message "$DOTEST"/author-script \
347 "$DOTEST"/amend || exit
348 read command sha1 rest < "$TODO"
354 comment_for_reflog pick
358 die_with_patch $sha1 "Could not apply $sha1... $rest"
361 comment_for_reflog reword
365 die_with_patch $sha1 "Could not apply $sha1... $rest"
369 comment_for_reflog edit
373 die_with_patch $sha1 "Could not apply $sha1... $rest"
375 git rev-parse --verify HEAD > "$DOTEST"/amend
376 warn "Stopped at $sha1... $rest"
377 warn "You can amend the commit now, with"
379 warn " git commit --amend"
381 warn "Once you are satisfied with your changes, run"
383 warn " git rebase --continue"
396 comment_for_reflog $squash_style
398 test -f "$DONE" && has_action "$DONE" ||
399 die "Cannot '$squash_style' without a previous commit"
402 make_squash_message $squash_style $sha1 > "$MSG"
404 author_script=$(get_author_ident_from_commit HEAD)
405 output git reset --soft HEAD^
406 pick_one -n $sha1 || failed=t
407 case "$(peek_next_command)" in
412 cp "$MSG" "$SQUASH_MSG"
418 rm -f "$SQUASH_MSG" || exit
419 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
420 rm -f "$GIT_DIR"/MERGE_MSG || exit
423 echo "$author_script" > "$DOTEST"/author-script
426 # This is like --amend, but with a different message
427 eval "$author_script"
428 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
429 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
430 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
431 $USE_OUTPUT git commit --no-verify \
432 $MSG_OPT "$EDIT_OR_FILE" || failed=t
436 cp "$MSG" "$GIT_DIR"/MERGE_MSG
438 warn "Could not apply $sha1... $rest"
439 die_with_patch $sha1 ""
443 warn "Unknown command: $command $sha1 $rest"
444 if git rev-parse --verify -q "$sha1" >/dev/null
446 die_with_patch $sha1 "Please fix this in the file $TODO."
448 die "Please fix this in the file $TODO."
452 test -s "$TODO" && return
454 comment_for_reflog finish &&
455 HEADNAME=$(cat "$DOTEST"/head-name) &&
456 OLDHEAD=$(cat "$DOTEST"/head) &&
457 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
458 NEWHEAD=$(git rev-parse HEAD) &&
461 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
462 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
463 git symbolic-ref HEAD $HEADNAME
466 test ! -f "$DOTEST"/verbose ||
467 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
471 warn "Successfully rebased and updated $HEADNAME."
483 # skip picking commits whose parents are unchanged
484 skip_unnecessary_picks () {
486 while read command sha1 rest
488 # fd=3 means we skip the command
489 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
490 3,pick,"$ONTO"*|3,p,"$ONTO"*)
491 # pick a commit whose parent is current $ONTO -> skip
501 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
502 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
503 mv -f "$TODO".new "$TODO" ||
504 die "Could not skip unnecessary pick commands"
507 # check if no other options are set
509 test $# -eq 2 -a "$2" = '--' &&
511 test -z "$PRESERVE_MERGES" &&
512 test -z "$STRATEGY" &&
516 get_saved_options () {
517 test -d "$REWRITTEN" && PRESERVE_MERGES=t
518 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
519 test -f "$DOTEST"/verbose && VERBOSE=t
520 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
527 OK_TO_SKIP_PRE_REBASE=yes
532 is_standalone "$@" || usage
534 comment_for_reflog continue
536 test -d "$DOTEST" || die "No interactive rebase running"
539 git rev-parse --verify HEAD >/dev/null ||
540 die "Cannot read HEAD"
541 git update-index --ignore-submodules --refresh &&
542 git diff-files --quiet --ignore-submodules ||
543 die "Working tree is dirty"
545 # do we have anything to commit?
546 if git diff-index --cached --quiet --ignore-submodules HEAD --
548 : Nothing to commit -- skip this
550 . "$DOTEST"/author-script ||
551 die "Cannot find the author identity"
553 if test -f "$DOTEST"/amend
555 amend=$(git rev-parse --verify HEAD)
556 test "$amend" = $(cat "$DOTEST"/amend) ||
558 You have uncommitted changes in your working tree. Please, commit them
559 first and then run 'git rebase --continue' again."
560 git reset --soft HEAD^ ||
561 die "Cannot rewind the HEAD"
563 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
564 git commit --no-verify -F "$DOTEST"/message -e || {
565 test -n "$amend" && git reset --soft $amend
566 die "Could not commit staged changes."
570 require_clean_work_tree
574 is_standalone "$@" || usage
576 comment_for_reflog abort
579 test -d "$DOTEST" || die "No interactive rebase running"
581 HEADNAME=$(cat "$DOTEST"/head-name)
582 HEAD=$(cat "$DOTEST"/head)
585 git symbolic-ref HEAD $HEADNAME
588 output git reset --hard $HEAD &&
593 is_standalone "$@" || usage
595 comment_for_reflog skip
598 test -d "$DOTEST" || die "No interactive rebase running"
600 output git reset --hard && do_rest
605 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
614 # we use merge anyway
630 ONTO=$(git rev-parse --verify "$1") ||
631 die "Does not point to a valid commit: $1"
635 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
636 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
638 die "Interactive rebase already started"
640 git var GIT_COMMITTER_IDENT >/dev/null ||
641 die "You need to set your committer info first"
643 if test -z "$REBASE_ROOT"
646 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
647 test -z "$ONTO" && ONTO=$UPSTREAM
653 die "You must specify --onto when using --root"
655 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
657 comment_for_reflog start
659 require_clean_work_tree
663 output git show-ref --verify --quiet "refs/heads/$1" ||
664 die "Invalid branchname: $1"
665 output git checkout "$1" ||
666 die "Could not checkout $1"
669 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
670 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
672 : > "$DOTEST"/interactive || die "Could not mark as interactive"
673 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
674 echo "detached HEAD" > "$DOTEST"/head-name
676 echo $HEAD > "$DOTEST"/head
677 case "$REBASE_ROOT" in
679 rm -f "$DOTEST"/rebase-root ;;
681 : >"$DOTEST"/rebase-root ;;
683 echo $ONTO > "$DOTEST"/onto
684 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
685 test t = "$VERBOSE" && : > "$DOTEST"/verbose
686 if test t = "$PRESERVE_MERGES"
688 # $REWRITTEN contains files for each commit that is
689 # reachable by at least one merge base of $HEAD and
690 # $UPSTREAM. They are not necessarily rewritten, but
691 # their children might be.
692 # This ensures that commits on merged, but otherwise
693 # unrelated side branches are left alone. (Think "X"
694 # in the man page's example.)
695 if test -z "$REBASE_ROOT"
697 mkdir "$REWRITTEN" &&
698 for c in $(git merge-base --all $HEAD $UPSTREAM)
700 echo $ONTO > "$REWRITTEN"/$c ||
701 die "Could not init rewritten commits"
704 mkdir "$REWRITTEN" &&
705 echo $ONTO > "$REWRITTEN"/root ||
706 die "Could not init rewritten commits"
708 # No cherry-pick because our first pass is to determine
709 # parents to rewrite and skipping dropped commits would
710 # prematurely end our probe
712 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
714 MERGES_OPTION="--no-merges --cherry-pick"
717 SHORTHEAD=$(git rev-parse --short $HEAD)
718 SHORTONTO=$(git rev-parse --short $ONTO)
719 if test -z "$REBASE_ROOT"
720 # this is now equivalent to ! -z "$UPSTREAM"
722 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
723 REVISIONS=$UPSTREAM...$HEAD
724 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
726 REVISIONS=$ONTO...$HEAD
727 SHORTREVISIONS=$SHORTHEAD
729 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
730 --abbrev=7 --reverse --left-right --topo-order \
732 sed -n "s/^>//p" | while read shortsha1 rest
734 if test t != "$PRESERVE_MERGES"
736 echo "pick $shortsha1 $rest" >> "$TODO"
738 sha1=$(git rev-parse $shortsha1)
739 if test -z "$REBASE_ROOT"
742 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
744 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
752 if test f = "$preserve"
754 touch "$REWRITTEN"/$sha1
755 echo "pick $shortsha1 $rest" >> "$TODO"
760 # Watch for commits that been dropped by --cherry-pick
761 if test t = "$PRESERVE_MERGES"
764 # Save all non-cherry-picked changes
765 git rev-list $REVISIONS --left-right --cherry-pick | \
766 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
767 # Now all commits and note which ones are missing in
768 # not-cherry-picks and hence being dropped
769 git rev-list $REVISIONS |
772 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
774 # Use -f2 because if rev-list is telling us this commit is
775 # not worthwhile, we don't want to track its multiple heads,
776 # just the history of its first-parent for others that will
777 # be rebasing on top of it
778 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
779 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
780 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
786 test -s "$TODO" || echo noop >> "$TODO"
787 cat >> "$TODO" << EOF
789 # Rebase $SHORTREVISIONS onto $SHORTONTO
792 # p, pick = use commit
793 # r, reword = use commit, but edit the commit message
794 # e, edit = use commit, but stop for amending
795 # s, squash = use commit, but meld into previous commit
796 # f, fixup = like "squash", but discard this commit's log message
798 # If you remove a line here THAT COMMIT WILL BE LOST.
799 # However, if you remove everything, the rebase will be aborted.
803 has_action "$TODO" ||
804 die_abort "Nothing to do"
806 cp "$TODO" "$TODO".backup
807 git_editor "$TODO" ||
808 die "Could not execute editor"
810 has_action "$TODO" ||
811 die_abort "Nothing to do"
813 test -d "$REWRITTEN" || skip_unnecessary_picks
815 git update-ref ORIG_HEAD $HEAD
816 output git checkout $ONTO && do_rest