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
78 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
79 # GIT_AUTHOR_DATE that will be used for the commit that is currently
81 AUTHOR_SCRIPT="$DOTEST"/author-script
83 # When an "edit" rebase command is being processed, the SHA1 of the
84 # commit to be edited is recorded in this file. When "git rebase
85 # --continue" is executed, if there are any staged changes then they
86 # will be amended to the HEAD commit, but only provided the HEAD
87 # commit is still the commit to be edited. When any other rebase
88 # command is processed, this file is deleted.
95 OK_TO_SKIP_PRE_REBASE=
98 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
99 mark the corrected paths with 'git add <paths>', and
100 run 'git rebase --continue'"
101 export GIT_CHERRY_PICK_HELP
112 test $status != 0 && printf "%s\n" "$output"
121 run_pre_rebase_hook () {
122 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
123 test -x "$GIT_DIR/hooks/pre-rebase"
125 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
126 echo >&2 "The pre-rebase hook refused to rebase."
132 require_clean_work_tree () {
133 # test if working tree is dirty
134 git rev-parse --verify HEAD > /dev/null &&
135 git update-index --ignore-submodules --refresh &&
136 git diff-files --quiet --ignore-submodules &&
137 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
138 die "Working tree is dirty"
141 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
143 comment_for_reflog () {
144 case "$ORIG_REFLOG_ACTION" in
146 GIT_REFLOG_ACTION="rebase -i ($1)"
147 export GIT_REFLOG_ACTION
153 mark_action_done () {
154 sed -e 1q < "$TODO" >> "$DONE"
155 sed -e 1d < "$TODO" >> "$TODO".new
156 mv -f "$TODO".new "$TODO"
157 count=$(sane_grep -c '^[^#]' < "$DONE")
158 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
159 if test "$last_count" != "$count"
162 printf "Rebasing (%d/%d)\r" $count $total
163 test -z "$VERBOSE" || echo
168 sha1_and_parents="$(git rev-list --parents -1 "$1")"
169 case "$sha1_and_parents" in
171 git diff --cc $sha1_and_parents
174 git diff-tree -p "$1^!"
179 esac > "$DOTEST"/patch
181 git cat-file commit "$1" | sed "1,/^$/d" > "$MSG"
182 test -f "$AUTHOR_SCRIPT" ||
183 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
198 sane_grep '^[^#]' "$1" >/dev/null
203 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
204 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
205 test -d "$REWRITTEN" &&
206 pick_one_preserving_merges "$@" && return
207 if test -n "$REBASE_ROOT"
209 output git cherry-pick "$@"
212 parent_sha1=$(git rev-parse --verify $sha1^) ||
213 die "Could not get the parent of $sha1"
214 current_sha1=$(git rev-parse --verify HEAD)
215 if test -z "$no_ff" -a "$current_sha1" = "$parent_sha1"
217 output git reset --hard $sha1
218 output warn Fast-forward to $(git rev-parse --short $sha1)
220 output git cherry-pick "$@"
224 pick_one_preserving_merges () {
235 sha1=$(git rev-parse $sha1)
237 if test -f "$DOTEST"/current-commit
239 if test "$fast_forward" = t
241 cat "$DOTEST"/current-commit | while read current_commit
243 git rev-parse HEAD > "$REWRITTEN"/$current_commit
245 rm "$DOTEST"/current-commit ||
246 die "Cannot write current commit's replacement sha1"
250 echo $sha1 >> "$DOTEST"/current-commit
252 # rewrite parents; if none were rewritten, we can fast-forward.
254 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
255 if test "$pend" = " "
259 while [ "$pend" != "" ]
261 p=$(expr "$pend" : ' \([^ ]*\)')
264 if test -f "$REWRITTEN"/$p
266 new_p=$(cat "$REWRITTEN"/$p)
268 # If the todo reordered commits, and our parent is marked for
269 # rewriting, but hasn't been gotten to yet, assume the user meant to
270 # drop it on top of the current HEAD
273 new_p=$(git rev-parse HEAD)
276 test $p != $new_p && fast_forward=f
277 case "$new_parents" in
279 ;; # do nothing; that parent is already there
281 new_parents="$new_parents $new_p"
285 if test -f "$DROPPED"/$p
288 replacement="$(cat "$DROPPED"/$p)"
289 test -z "$replacement" && replacement=root
290 pend=" $replacement$pend"
292 new_parents="$new_parents $p"
296 case $fast_forward in
298 output warn "Fast-forward to $sha1"
299 output git reset --hard $sha1 ||
300 die "Cannot fast-forward to $sha1"
303 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
307 # detach HEAD to current parent
308 output git checkout $first_parent 2> /dev/null ||
309 die "Cannot move HEAD to $first_parent"
312 case "$new_parents" in
314 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
317 author_script=$(get_author_ident_from_commit $sha1)
318 eval "$author_script"
319 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
320 # No point in merging the first parent, that's HEAD
321 new_parents=${new_parents# $first_parent}
322 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
323 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
324 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
325 output git merge $STRATEGY -m "$msg" \
328 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
329 die_with_patch $sha1 "Error redoing merge $sha1"
333 output git cherry-pick "$@" ||
334 die_with_patch $sha1 "Could not pick $sha1"
343 *1[0-9]|*[04-9]) echo "$1"th;;
350 make_squash_message () {
351 if test -f "$SQUASH_MSG"; then
353 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
354 -e "q" < "$SQUASH_MSG")+1))
355 echo "# This is a combination of $COUNT commits."
356 sed -e 1d -e '2,/^./{
361 echo "# This is a combination of 2 commits."
362 echo "# The first commit's message is:"
364 git cat-file commit HEAD | sed -e '1,/^$/d'
369 echo "# This is the $(nth_string $COUNT) commit message:"
371 git cat-file commit $2 | sed -e '1,/^$/d'
375 echo "# The $(nth_string $COUNT) commit message will be skipped:"
377 git cat-file commit $2 | sed -e '1,/^$/d' -e 's/^/# /'
382 peek_next_command () {
383 sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO"
387 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
388 read command sha1 rest < "$TODO"
394 comment_for_reflog pick
398 die_with_patch $sha1 "Could not apply $sha1... $rest"
401 comment_for_reflog reword
405 die_with_patch $sha1 "Could not apply $sha1... $rest"
409 comment_for_reflog edit
413 die_with_patch $sha1 "Could not apply $sha1... $rest"
415 git rev-parse --verify HEAD > "$AMEND"
416 warn "Stopped at $sha1... $rest"
417 warn "You can amend the commit now, with"
419 warn " git commit --amend"
421 warn "Once you are satisfied with your changes, run"
423 warn " git rebase --continue"
436 comment_for_reflog $squash_style
438 test -f "$DONE" && has_action "$DONE" ||
439 die "Cannot '$squash_style' without a previous commit"
442 make_squash_message $squash_style $sha1 > "$MSG"
444 author_script=$(get_author_ident_from_commit HEAD)
445 output git reset --soft HEAD^
446 pick_one -n $sha1 || failed=t
447 case "$(peek_next_command)" in
452 cp "$MSG" "$SQUASH_MSG"
458 rm -f "$SQUASH_MSG" || exit
459 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
460 rm -f "$GIT_DIR"/MERGE_MSG || exit
463 echo "$author_script" > "$AUTHOR_SCRIPT"
466 # This is like --amend, but with a different message
467 eval "$author_script"
468 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
469 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
470 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
471 $USE_OUTPUT git commit --no-verify \
472 $MSG_OPT "$EDIT_OR_FILE" || failed=t
476 cp "$MSG" "$GIT_DIR"/MERGE_MSG
478 warn "Could not apply $sha1... $rest"
479 die_with_patch $sha1 ""
483 warn "Unknown command: $command $sha1 $rest"
484 if git rev-parse --verify -q "$sha1" >/dev/null
486 die_with_patch $sha1 "Please fix this in the file $TODO."
488 die "Please fix this in the file $TODO."
492 test -s "$TODO" && return
494 comment_for_reflog finish &&
495 HEADNAME=$(cat "$DOTEST"/head-name) &&
496 OLDHEAD=$(cat "$DOTEST"/head) &&
497 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
498 NEWHEAD=$(git rev-parse HEAD) &&
501 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
502 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
503 git symbolic-ref HEAD $HEADNAME
506 test ! -f "$DOTEST"/verbose ||
507 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
511 warn "Successfully rebased and updated $HEADNAME."
523 # skip picking commits whose parents are unchanged
524 skip_unnecessary_picks () {
526 while read command sha1 rest
528 # fd=3 means we skip the command
529 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
530 3,pick,"$ONTO"*|3,p,"$ONTO"*)
531 # pick a commit whose parent is current $ONTO -> skip
541 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
542 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
543 mv -f "$TODO".new "$TODO" ||
544 die "Could not skip unnecessary pick commands"
547 # check if no other options are set
549 test $# -eq 2 -a "$2" = '--' &&
551 test -z "$PRESERVE_MERGES" &&
552 test -z "$STRATEGY" &&
556 get_saved_options () {
557 test -d "$REWRITTEN" && PRESERVE_MERGES=t
558 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
559 test -f "$DOTEST"/verbose && VERBOSE=t
560 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
567 OK_TO_SKIP_PRE_REBASE=yes
572 is_standalone "$@" || usage
574 comment_for_reflog continue
576 test -d "$DOTEST" || die "No interactive rebase running"
579 git rev-parse --verify HEAD >/dev/null ||
580 die "Cannot read HEAD"
581 git update-index --ignore-submodules --refresh &&
582 git diff-files --quiet --ignore-submodules ||
583 die "Working tree is dirty"
585 # do we have anything to commit?
586 if git diff-index --cached --quiet --ignore-submodules HEAD --
588 : Nothing to commit -- skip this
590 . "$AUTHOR_SCRIPT" ||
591 die "Cannot find the author identity"
595 amend=$(git rev-parse --verify HEAD)
596 test "$amend" = $(cat "$AMEND") ||
598 You have uncommitted changes in your working tree. Please, commit them
599 first and then run 'git rebase --continue' again."
600 git reset --soft HEAD^ ||
601 die "Cannot rewind the HEAD"
603 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
604 git commit --no-verify -F "$MSG" -e || {
605 test -n "$amend" && git reset --soft $amend
606 die "Could not commit staged changes."
610 require_clean_work_tree
614 is_standalone "$@" || usage
616 comment_for_reflog abort
619 test -d "$DOTEST" || die "No interactive rebase running"
621 HEADNAME=$(cat "$DOTEST"/head-name)
622 HEAD=$(cat "$DOTEST"/head)
625 git symbolic-ref HEAD $HEADNAME
628 output git reset --hard $HEAD &&
633 is_standalone "$@" || usage
635 comment_for_reflog skip
638 test -d "$DOTEST" || die "No interactive rebase running"
640 output git reset --hard && do_rest
645 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
654 # we use merge anyway
670 ONTO=$(git rev-parse --verify "$1") ||
671 die "Does not point to a valid commit: $1"
675 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
676 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
678 die "Interactive rebase already started"
680 git var GIT_COMMITTER_IDENT >/dev/null ||
681 die "You need to set your committer info first"
683 if test -z "$REBASE_ROOT"
686 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
687 test -z "$ONTO" && ONTO=$UPSTREAM
693 die "You must specify --onto when using --root"
695 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
697 comment_for_reflog start
699 require_clean_work_tree
703 output git show-ref --verify --quiet "refs/heads/$1" ||
704 die "Invalid branchname: $1"
705 output git checkout "$1" ||
706 die "Could not checkout $1"
709 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
710 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
712 : > "$DOTEST"/interactive || die "Could not mark as interactive"
713 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
714 echo "detached HEAD" > "$DOTEST"/head-name
716 echo $HEAD > "$DOTEST"/head
717 case "$REBASE_ROOT" in
719 rm -f "$DOTEST"/rebase-root ;;
721 : >"$DOTEST"/rebase-root ;;
723 echo $ONTO > "$DOTEST"/onto
724 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
725 test t = "$VERBOSE" && : > "$DOTEST"/verbose
726 if test t = "$PRESERVE_MERGES"
728 if test -z "$REBASE_ROOT"
730 mkdir "$REWRITTEN" &&
731 for c in $(git merge-base --all $HEAD $UPSTREAM)
733 echo $ONTO > "$REWRITTEN"/$c ||
734 die "Could not init rewritten commits"
737 mkdir "$REWRITTEN" &&
738 echo $ONTO > "$REWRITTEN"/root ||
739 die "Could not init rewritten commits"
741 # No cherry-pick because our first pass is to determine
742 # parents to rewrite and skipping dropped commits would
743 # prematurely end our probe
745 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
747 MERGES_OPTION="--no-merges --cherry-pick"
750 SHORTHEAD=$(git rev-parse --short $HEAD)
751 SHORTONTO=$(git rev-parse --short $ONTO)
752 if test -z "$REBASE_ROOT"
753 # this is now equivalent to ! -z "$UPSTREAM"
755 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
756 REVISIONS=$UPSTREAM...$HEAD
757 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
759 REVISIONS=$ONTO...$HEAD
760 SHORTREVISIONS=$SHORTHEAD
762 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
763 --abbrev=7 --reverse --left-right --topo-order \
765 sed -n "s/^>//p" | while read shortsha1 rest
767 if test t != "$PRESERVE_MERGES"
769 echo "pick $shortsha1 $rest" >> "$TODO"
771 sha1=$(git rev-parse $shortsha1)
772 if test -z "$REBASE_ROOT"
775 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
777 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
785 if test f = "$preserve"
787 touch "$REWRITTEN"/$sha1
788 echo "pick $shortsha1 $rest" >> "$TODO"
793 # Watch for commits that been dropped by --cherry-pick
794 if test t = "$PRESERVE_MERGES"
797 # Save all non-cherry-picked changes
798 git rev-list $REVISIONS --left-right --cherry-pick | \
799 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
800 # Now all commits and note which ones are missing in
801 # not-cherry-picks and hence being dropped
802 git rev-list $REVISIONS |
805 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
807 # Use -f2 because if rev-list is telling us this commit is
808 # not worthwhile, we don't want to track its multiple heads,
809 # just the history of its first-parent for others that will
810 # be rebasing on top of it
811 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
812 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
813 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
819 test -s "$TODO" || echo noop >> "$TODO"
820 cat >> "$TODO" << EOF
822 # Rebase $SHORTREVISIONS onto $SHORTONTO
825 # p, pick = use commit
826 # r, reword = use commit, but edit the commit message
827 # e, edit = use commit, but stop for amending
828 # s, squash = use commit, but meld into previous commit
829 # f, fixup = like "squash", but discard this commit's log message
831 # If you remove a line here THAT COMMIT WILL BE LOST.
832 # However, if you remove everything, the rebase will be aborted.
836 has_action "$TODO" ||
837 die_abort "Nothing to do"
839 cp "$TODO" "$TODO".backup
840 git_editor "$TODO" ||
841 die "Could not execute editor"
843 has_action "$TODO" ||
844 die_abort "Nothing to do"
846 test -d "$REWRITTEN" || skip_unnecessary_picks
848 git update-ref ORIG_HEAD $HEAD
849 output git checkout $ONTO && do_rest