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 # If the current series of squash/fixups has not yet included a squash
69 # command, then this file exists and holds the commit message of the
70 # original "pick" commit. (If the series ends without a "squash"
71 # command, then this can be used as the commit message of the combined
72 # commit without opening the editor.)
73 FIXUP_MSG
="$DOTEST"/message-fixup
75 # $REWRITTEN is the name of a directory containing files for each
76 # commit that is reachable by at least one merge base of $HEAD and
77 # $UPSTREAM. They are not necessarily rewritten, but their children
78 # might be. This ensures that commits on merged, but otherwise
79 # unrelated side branches are left alone. (Think "X" in the man page's
81 REWRITTEN
="$DOTEST"/rewritten
83 DROPPED
="$DOTEST"/dropped
85 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
86 # GIT_AUTHOR_DATE that will be used for the commit that is currently
88 AUTHOR_SCRIPT
="$DOTEST"/author-script
90 # When an "edit" rebase command is being processed, the SHA1 of the
91 # commit to be edited is recorded in this file. When "git rebase
92 # --continue" is executed, if there are any staged changes then they
93 # will be amended to the HEAD commit, but only provided the HEAD
94 # commit is still the commit to be edited. When any other rebase
95 # command is processed, this file is deleted.
102 OK_TO_SKIP_PRE_REBASE
=
105 GIT_CHERRY_PICK_HELP
=" After resolving the conflicts,
106 mark the corrected paths with 'git add <paths>', and
107 run 'git rebase --continue'"
108 export GIT_CHERRY_PICK_HELP
119 test $status != 0 && printf "%s\n" "$output"
128 # Output the commit message for the specified commit.
130 git cat-file commit
"$1" |
sed "1,/^$/d"
133 run_pre_rebase_hook
() {
134 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
135 test -x "$GIT_DIR/hooks/pre-rebase"
137 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
{
138 echo >&2 "The pre-rebase hook refused to rebase."
144 require_clean_work_tree
() {
145 # test if working tree is dirty
146 git rev-parse
--verify HEAD
> /dev
/null
&&
147 git update-index
--ignore-submodules --refresh &&
148 git diff-files
--quiet --ignore-submodules &&
149 git diff-index
--cached --quiet HEAD
--ignore-submodules -- ||
150 die
"Working tree is dirty"
153 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
155 comment_for_reflog
() {
156 case "$ORIG_REFLOG_ACTION" in
158 GIT_REFLOG_ACTION
="rebase -i ($1)"
159 export GIT_REFLOG_ACTION
165 mark_action_done
() {
166 sed -e 1q
< "$TODO" >> "$DONE"
167 sed -e 1d
< "$TODO" >> "$TODO".new
168 mv -f "$TODO".new
"$TODO"
169 count
=$
(sane_grep
-c '^[^#]' < "$DONE")
170 total
=$
(($count+$
(sane_grep
-c '^[^#]' < "$TODO")))
171 if test "$last_count" != "$count"
174 printf "Rebasing (%d/%d)\r" $count $total
175 test -z "$VERBOSE" ||
echo
180 sha1_and_parents
="$(git rev-list --parents -1 "$1")"
181 case "$sha1_and_parents" in
183 git
diff --cc $sha1_and_parents
186 git diff-tree
-p "$1^!"
191 esac > "$DOTEST"/patch
193 commit_message
"$1" > "$MSG"
194 test -f "$AUTHOR_SCRIPT" ||
195 get_author_ident_from_commit
"$1" > "$AUTHOR_SCRIPT"
210 sane_grep
'^[^#]' "$1" >/dev
/null
213 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
214 # GIT_AUTHOR_DATE exported from the current environment.
216 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
217 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
218 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
224 case "$1" in -n) sha1
=$2; no_ff
=t
;; *) sha1
=$1 ;; esac
225 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
226 test -d "$REWRITTEN" &&
227 pick_one_preserving_merges
"$@" && return
228 if test -n "$REBASE_ROOT"
230 output git cherry-pick
"$@"
233 parent_sha1
=$
(git rev-parse
--verify $sha1^
) ||
234 die
"Could not get the parent of $sha1"
235 current_sha1
=$
(git rev-parse
--verify HEAD
)
236 if test -z "$no_ff" -a "$current_sha1" = "$parent_sha1"
238 output git
reset --hard $sha1
239 output warn Fast-forward to $
(git rev-parse
--short $sha1)
241 output git cherry-pick
"$@"
245 pick_one_preserving_merges
() {
256 sha1
=$
(git rev-parse
$sha1)
258 if test -f "$DOTEST"/current-commit
260 if test "$fast_forward" = t
262 cat "$DOTEST"/current-commit |
while read current_commit
264 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
266 rm "$DOTEST"/current-commit ||
267 die
"Cannot write current commit's replacement sha1"
271 echo $sha1 >> "$DOTEST"/current-commit
273 # rewrite parents; if none were rewritten, we can fast-forward.
275 pend
=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
276 if test "$pend" = " "
280 while [ "$pend" != "" ]
282 p
=$
(expr "$pend" : ' \([^ ]*\)')
285 if test -f "$REWRITTEN"/$p
287 new_p
=$
(cat "$REWRITTEN"/$p)
289 # If the todo reordered commits, and our parent is marked for
290 # rewriting, but hasn't been gotten to yet, assume the user meant to
291 # drop it on top of the current HEAD
294 new_p
=$
(git rev-parse HEAD
)
297 test $p != $new_p && fast_forward
=f
298 case "$new_parents" in
300 ;; # do nothing; that parent is already there
302 new_parents
="$new_parents $new_p"
306 if test -f "$DROPPED"/$p
309 replacement
="$(cat "$DROPPED"/$p)"
310 test -z "$replacement" && replacement
=root
311 pend
=" $replacement$pend"
313 new_parents
="$new_parents $p"
317 case $fast_forward in
319 output warn
"Fast-forward to $sha1"
320 output git
reset --hard $sha1 ||
321 die
"Cannot fast-forward to $sha1"
324 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
328 # detach HEAD to current parent
329 output git checkout
$first_parent 2> /dev
/null ||
330 die
"Cannot move HEAD to $first_parent"
333 case "$new_parents" in
335 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
338 author_script
=$
(get_author_ident_from_commit
$sha1)
339 eval "$author_script"
340 msg
="$(commit_message $sha1)"
341 # No point in merging the first parent, that's HEAD
342 new_parents
=${new_parents# $first_parent}
343 if ! do_with_author output \
344 git merge
$STRATEGY -m "$msg" $new_parents
346 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
347 die_with_patch
$sha1 "Error redoing merge $sha1"
351 output git cherry-pick
"$@" ||
352 die_with_patch
$sha1 "Could not pick $sha1"
361 *1[0-9]|
*[04-9]) echo "$1"th
;;
368 update_squash_messages
() {
369 if test -f "$SQUASH_MSG"; then
370 mv "$SQUASH_MSG" "$SQUASH_MSG".bak ||
exit
372 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
373 -e "q" < "$SQUASH_MSG".bak
)+1))
375 echo "# This is a combination of $COUNT commits."
376 sed -e 1d
-e '2,/^./{
378 }' <"$SQUASH_MSG".bak
381 commit_message HEAD
> "$FIXUP_MSG" || die
"Cannot write $FIXUP_MSG"
384 echo "# This is a combination of 2 commits."
385 echo "# The first commit's message is:"
394 echo "# This is the $(nth_string $COUNT) commit message:"
400 echo "# The $(nth_string $COUNT) commit message will be skipped:"
402 commit_message
$2 |
sed -e 's/^/# /'
407 peek_next_command
() {
408 sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO"
411 # A squash/fixup has failed. Prepare the long version of the squash
412 # commit message, then die_with_patch. This code path requires the
413 # user to edit the combined commit message for all commits that have
414 # been squashed/fixedup so far. So also erase the old squash
415 # messages, effectively causing the combined commit to be used as the
416 # new basis for any further squash/fixups. Args: sha1 rest
417 die_failed_squash
() {
418 mv "$SQUASH_MSG" "$MSG" ||
exit
420 cp "$MSG" "$GIT_DIR"/MERGE_MSG ||
exit
422 warn
"Could not apply $1... $2"
427 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" ||
exit
428 read command sha1 rest
< "$TODO"
434 comment_for_reflog pick
438 die_with_patch
$sha1 "Could not apply $sha1... $rest"
441 comment_for_reflog reword
445 die_with_patch
$sha1 "Could not apply $sha1... $rest"
449 comment_for_reflog edit
453 die_with_patch
$sha1 "Could not apply $sha1... $rest"
455 git rev-parse
--verify HEAD
> "$AMEND"
456 warn
"Stopped at $sha1... $rest"
457 warn
"You can amend the commit now, with"
459 warn
" git commit --amend"
461 warn
"Once you are satisfied with your changes, run"
463 warn
" git rebase --continue"
476 comment_for_reflog
$squash_style
478 test -f "$DONE" && has_action
"$DONE" ||
479 die
"Cannot '$squash_style' without a previous commit"
482 update_squash_messages
$squash_style $sha1
483 author_script
=$
(get_author_ident_from_commit HEAD
)
484 echo "$author_script" > "$AUTHOR_SCRIPT"
485 eval "$author_script"
486 output git
reset --soft HEAD^
487 pick_one
-n $sha1 || die_failed_squash
$sha1 "$rest"
488 case "$(peek_next_command)" in
490 # This is an intermediate commit; its message will only be
491 # used in case of trouble. So use the long version:
492 do_with_author output git commit
--no-verify -F "$SQUASH_MSG" ||
493 die_failed_squash
$sha1 "$rest"
496 # This is the final command of this squash/fixup group
497 if test -f "$FIXUP_MSG"
499 do_with_author git commit
--no-verify -F "$FIXUP_MSG" ||
500 die_failed_squash
$sha1 "$rest"
502 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG ||
exit
503 rm -f "$GIT_DIR"/MERGE_MSG
504 do_with_author git commit
--no-verify -e ||
505 die_failed_squash
$sha1 "$rest"
507 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
512 warn
"Unknown command: $command $sha1 $rest"
513 if git rev-parse
--verify -q "$sha1" >/dev
/null
515 die_with_patch
$sha1 "Please fix this in the file $TODO."
517 die
"Please fix this in the file $TODO."
521 test -s "$TODO" && return
523 comment_for_reflog finish
&&
524 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
525 OLDHEAD
=$
(cat "$DOTEST"/head) &&
526 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
527 NEWHEAD
=$
(git rev-parse HEAD
) &&
530 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
531 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
532 git symbolic-ref HEAD
$HEADNAME
535 test ! -f "$DOTEST"/verbose ||
536 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
540 warn
"Successfully rebased and updated $HEADNAME."
552 # skip picking commits whose parents are unchanged
553 skip_unnecessary_picks
() {
555 while read command sha1 rest
557 # fd=3 means we skip the command
558 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
559 3,pick
,"$ONTO"*|
3,p
,"$ONTO"*)
560 # pick a commit whose parent is current $ONTO -> skip
570 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
571 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
572 mv -f "$TODO".new
"$TODO" ||
573 die
"Could not skip unnecessary pick commands"
576 # check if no other options are set
578 test $# -eq 2 -a "$2" = '--' &&
580 test -z "$PRESERVE_MERGES" &&
581 test -z "$STRATEGY" &&
585 get_saved_options
() {
586 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
587 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
588 test -f "$DOTEST"/verbose
&& VERBOSE
=t
589 test -f "$DOTEST"/rebase-root
&& REBASE_ROOT
=t
596 OK_TO_SKIP_PRE_REBASE
=yes
601 is_standalone
"$@" || usage
603 comment_for_reflog
continue
605 test -d "$DOTEST" || die
"No interactive rebase running"
608 git rev-parse
--verify HEAD
>/dev
/null ||
609 die
"Cannot read HEAD"
610 git update-index
--ignore-submodules --refresh &&
611 git diff-files
--quiet --ignore-submodules ||
612 die
"Working tree is dirty"
614 # do we have anything to commit?
615 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
617 : Nothing to commit
-- skip this
619 .
"$AUTHOR_SCRIPT" ||
620 die
"Cannot find the author identity"
624 amend
=$
(git rev-parse
--verify HEAD
)
625 test "$amend" = $
(cat "$AMEND") ||
627 You have uncommitted changes in your working tree. Please, commit them
628 first and then run 'git rebase --continue' again."
629 git
reset --soft HEAD^ ||
630 die
"Cannot rewind the HEAD"
632 do_with_author git commit
--no-verify -F "$MSG" -e ||
{
633 test -n "$amend" && git
reset --soft $amend
634 die
"Could not commit staged changes."
638 require_clean_work_tree
642 is_standalone
"$@" || usage
644 comment_for_reflog abort
647 test -d "$DOTEST" || die
"No interactive rebase running"
649 HEADNAME
=$
(cat "$DOTEST"/head-name
)
650 HEAD
=$
(cat "$DOTEST"/head)
653 git symbolic-ref HEAD
$HEADNAME
656 output git
reset --hard $HEAD &&
661 is_standalone
"$@" || usage
663 comment_for_reflog skip
666 test -d "$DOTEST" || die
"No interactive rebase running"
668 output git
reset --hard && do_rest
673 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
682 # we use merge anyway
698 ONTO
=$
(git rev-parse
--verify "$1") ||
699 die
"Does not point to a valid commit: $1"
703 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
704 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
706 die
"Interactive rebase already started"
708 git var GIT_COMMITTER_IDENT
>/dev
/null ||
709 die
"You need to set your committer info first"
711 if test -z "$REBASE_ROOT"
714 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
715 test -z "$ONTO" && ONTO
=$UPSTREAM
721 die
"You must specify --onto when using --root"
723 run_pre_rebase_hook
"$UPSTREAM_ARG" "$@"
725 comment_for_reflog start
727 require_clean_work_tree
731 output git show-ref
--verify --quiet "refs/heads/$1" ||
732 die
"Invalid branchname: $1"
733 output git checkout
"$1" ||
734 die
"Could not checkout $1"
737 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
738 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
740 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
741 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
742 echo "detached HEAD" > "$DOTEST"/head-name
744 echo $HEAD > "$DOTEST"/head
745 case "$REBASE_ROOT" in
747 rm -f "$DOTEST"/rebase-root
;;
749 : >"$DOTEST"/rebase-root
;;
751 echo $ONTO > "$DOTEST"/onto
752 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
753 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
754 if test t
= "$PRESERVE_MERGES"
756 if test -z "$REBASE_ROOT"
758 mkdir
"$REWRITTEN" &&
759 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
761 echo $ONTO > "$REWRITTEN"/$c ||
762 die
"Could not init rewritten commits"
765 mkdir
"$REWRITTEN" &&
766 echo $ONTO > "$REWRITTEN"/root ||
767 die
"Could not init rewritten commits"
769 # No cherry-pick because our first pass is to determine
770 # parents to rewrite and skipping dropped commits would
771 # prematurely end our probe
773 first_after_upstream
="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
775 MERGES_OPTION
="--no-merges --cherry-pick"
778 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
779 SHORTONTO
=$
(git rev-parse
--short $ONTO)
780 if test -z "$REBASE_ROOT"
781 # this is now equivalent to ! -z "$UPSTREAM"
783 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
784 REVISIONS
=$UPSTREAM...
$HEAD
785 SHORTREVISIONS
=$SHORTUPSTREAM..
$SHORTHEAD
787 REVISIONS
=$ONTO...
$HEAD
788 SHORTREVISIONS
=$SHORTHEAD
790 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
791 --abbrev=7 --reverse --left-right --topo-order \
793 sed -n "s/^>//p" |
while read shortsha1 rest
795 if test t
!= "$PRESERVE_MERGES"
797 echo "pick $shortsha1 $rest" >> "$TODO"
799 sha1
=$
(git rev-parse
$shortsha1)
800 if test -z "$REBASE_ROOT"
803 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -s -f2-)
805 if test -f "$REWRITTEN"/$p -a \
( $p != $ONTO -o $sha1 = $first_after_upstream \
)
813 if test f
= "$preserve"
815 touch "$REWRITTEN"/$sha1
816 echo "pick $shortsha1 $rest" >> "$TODO"
821 # Watch for commits that been dropped by --cherry-pick
822 if test t
= "$PRESERVE_MERGES"
825 # Save all non-cherry-picked changes
826 git rev-list
$REVISIONS --left-right --cherry-pick | \
827 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
828 # Now all commits and note which ones are missing in
829 # not-cherry-picks and hence being dropped
830 git rev-list
$REVISIONS |
833 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
835 # Use -f2 because if rev-list is telling us this commit is
836 # not worthwhile, we don't want to track its multiple heads,
837 # just the history of its first-parent for others that will
838 # be rebasing on top of it
839 git rev-list
--parents -1 $rev | cut
-d' ' -s -f2 > "$DROPPED"/$rev
840 short
=$
(git rev-list
-1 --abbrev-commit --abbrev=7 $rev)
841 sane_grep
-v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
847 test -s "$TODO" ||
echo noop
>> "$TODO"
848 cat >> "$TODO" << EOF
850 # Rebase $SHORTREVISIONS onto $SHORTONTO
853 # p, pick = use commit
854 # r, reword = use commit, but edit the commit message
855 # e, edit = use commit, but stop for amending
856 # s, squash = use commit, but meld into previous commit
857 # f, fixup = like "squash", but discard this commit's log message
859 # If you remove a line here THAT COMMIT WILL BE LOST.
860 # However, if you remove everything, the rebase will be aborted.
864 has_action
"$TODO" ||
865 die_abort
"Nothing to do"
867 cp "$TODO" "$TODO".backup
868 git_editor
"$TODO" ||
869 die
"Could not execute editor"
871 has_action
"$TODO" ||
872 die_abort
"Nothing to do"
874 test -d "$REWRITTEN" || skip_unnecessary_picks
876 git update-ref ORIG_HEAD
$HEAD
877 output git checkout
$ONTO && do_rest