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"
39 # The file containing rebase commands, comments, and empty lines.
40 # This file is created by "git rebase -i" then edited by the user. As
41 # the lines are processed, they are removed from the front of this
42 # file and written to the tail of $DONE.
43 TODO
="$DOTEST"/git-rebase-todo
45 # The rebase command lines that have already been processed. A line
46 # is moved here when it is first handled, before any associated user
50 # The commit message that is planned to be used for any changes that
51 # need to be committed following a user interaction.
54 # The file into which is accumulated the suggested commit message for
55 # squash/fixup commands. When the first of a series of squash/fixups
56 # is seen, the file is created and the commit message from the
57 # previous commit and from the first squash/fixup commit are written
58 # to it. The commit message for each subsequent squash/fixup commit
59 # is appended to the file as it is processed.
61 # The first line of the file is of the form
62 # # This is a combination of $COUNT commits.
63 # where $COUNT is the number of commits whose messages have been
64 # written to the file so far (including the initial "pick" commit).
65 # Each time that a commit message is processed, this line is read and
66 # updated. It is deleted just before the combined commit is made.
67 SQUASH_MSG
="$DOTEST"/message-squash
69 # If the current series of squash/fixups has not yet included a squash
70 # command, then this file exists and holds the commit message of the
71 # original "pick" commit. (If the series ends without a "squash"
72 # command, then this can be used as the commit message of the combined
73 # commit without opening the editor.)
74 FIXUP_MSG
="$DOTEST"/message-fixup
76 # $REWRITTEN is the name of a directory containing files for each
77 # commit that is reachable by at least one merge base of $HEAD and
78 # $UPSTREAM. They are not necessarily rewritten, but their children
79 # might be. This ensures that commits on merged, but otherwise
80 # unrelated side branches are left alone. (Think "X" in the man page's
82 REWRITTEN
="$DOTEST"/rewritten
84 DROPPED
="$DOTEST"/dropped
86 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
87 # GIT_AUTHOR_DATE that will be used for the commit that is currently
89 AUTHOR_SCRIPT
="$DOTEST"/author-script
91 # When an "edit" rebase command is being processed, the SHA1 of the
92 # commit to be edited is recorded in this file. When "git rebase
93 # --continue" is executed, if there are any staged changes then they
94 # will be amended to the HEAD commit, but only provided the HEAD
95 # commit is still the commit to be edited. When any other rebase
96 # command is processed, this file is deleted.
103 OK_TO_SKIP_PRE_REBASE
=
107 GIT_CHERRY_PICK_HELP
=" After resolving the conflicts,
108 mark the corrected paths with 'git add <paths>', and
109 run 'git rebase --continue'"
110 export GIT_CHERRY_PICK_HELP
121 test $status != 0 && printf "%s\n" "$output"
130 # Output the commit message for the specified commit.
132 git cat-file commit
"$1" |
sed "1,/^$/d"
135 run_pre_rebase_hook
() {
136 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
137 test -x "$GIT_DIR/hooks/pre-rebase"
139 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
{
140 echo >&2 "The pre-rebase hook refused to rebase."
146 require_clean_work_tree
() {
147 # test if working tree is dirty
148 git rev-parse
--verify HEAD
> /dev
/null
&&
149 git update-index
--ignore-submodules --refresh &&
150 git diff-files
--quiet --ignore-submodules &&
151 git diff-index
--cached --quiet HEAD
--ignore-submodules -- ||
152 die
"Working tree is dirty"
155 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
157 comment_for_reflog
() {
158 case "$ORIG_REFLOG_ACTION" in
160 GIT_REFLOG_ACTION
="rebase -i ($1)"
161 export GIT_REFLOG_ACTION
167 mark_action_done
() {
168 sed -e 1q
< "$TODO" >> "$DONE"
169 sed -e 1d
< "$TODO" >> "$TODO".new
170 mv -f "$TODO".new
"$TODO"
171 count
=$
(sane_grep
-c '^[^#]' < "$DONE")
172 total
=$
(($count+$
(sane_grep
-c '^[^#]' < "$TODO")))
173 if test "$last_count" != "$count"
176 printf "Rebasing (%d/%d)\r" $count $total
177 test -z "$VERBOSE" ||
echo
182 sha1_and_parents
="$(git rev-list --parents -1 "$1")"
183 case "$sha1_and_parents" in
185 git
diff --cc $sha1_and_parents
188 git diff-tree
-p "$1^!"
193 esac > "$DOTEST"/patch
195 commit_message
"$1" > "$MSG"
196 test -f "$AUTHOR_SCRIPT" ||
197 get_author_ident_from_commit
"$1" > "$AUTHOR_SCRIPT"
212 sane_grep
'^[^#]' "$1" >/dev
/null
215 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
216 # GIT_AUTHOR_DATE exported from the current environment.
219 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
226 case "$1" in -n) sha1
=$2; ff
= ;; *) sha1
=$1 ;; esac
227 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
228 test -d "$REWRITTEN" &&
229 pick_one_preserving_merges
"$@" && return
230 if test -n "$REBASE_ROOT"
232 output git cherry-pick
"$@"
235 output git cherry-pick
$ff "$@"
238 pick_one_preserving_merges
() {
249 sha1
=$
(git rev-parse
$sha1)
251 if test -f "$DOTEST"/current-commit
253 if test "$fast_forward" = t
255 cat "$DOTEST"/current-commit |
while read current_commit
257 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
259 rm "$DOTEST"/current-commit ||
260 die
"Cannot write current commit's replacement sha1"
264 echo $sha1 >> "$DOTEST"/current-commit
266 # rewrite parents; if none were rewritten, we can fast-forward.
268 pend
=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
269 if test "$pend" = " "
273 while [ "$pend" != "" ]
275 p
=$
(expr "$pend" : ' \([^ ]*\)')
278 if test -f "$REWRITTEN"/$p
280 new_p
=$
(cat "$REWRITTEN"/$p)
282 # If the todo reordered commits, and our parent is marked for
283 # rewriting, but hasn't been gotten to yet, assume the user meant to
284 # drop it on top of the current HEAD
287 new_p
=$
(git rev-parse HEAD
)
290 test $p != $new_p && fast_forward
=f
291 case "$new_parents" in
293 ;; # do nothing; that parent is already there
295 new_parents
="$new_parents $new_p"
299 if test -f "$DROPPED"/$p
302 replacement
="$(cat "$DROPPED"/$p)"
303 test -z "$replacement" && replacement
=root
304 pend
=" $replacement$pend"
306 new_parents
="$new_parents $p"
310 case $fast_forward in
312 output warn
"Fast-forward to $sha1"
313 output git
reset --hard $sha1 ||
314 die
"Cannot fast-forward to $sha1"
317 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
321 # detach HEAD to current parent
322 output git checkout
$first_parent 2> /dev
/null ||
323 die
"Cannot move HEAD to $first_parent"
326 case "$new_parents" in
328 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
331 author_script
=$
(get_author_ident_from_commit
$sha1)
332 eval "$author_script"
333 msg
="$(commit_message $sha1)"
334 # No point in merging the first parent, that's HEAD
335 new_parents
=${new_parents# $first_parent}
336 if ! do_with_author output \
337 git merge
$STRATEGY -m "$msg" $new_parents
339 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
340 die_with_patch
$sha1 "Error redoing merge $sha1"
344 output git cherry-pick
"$@" ||
345 die_with_patch
$sha1 "Could not pick $sha1"
354 *1[0-9]|
*[04-9]) echo "$1"th
;;
361 update_squash_messages
() {
362 if test -f "$SQUASH_MSG"; then
363 mv "$SQUASH_MSG" "$SQUASH_MSG".bak ||
exit
365 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
366 -e "q" < "$SQUASH_MSG".bak
)+1))
368 echo "# This is a combination of $COUNT commits."
369 sed -e 1d
-e '2,/^./{
371 }' <"$SQUASH_MSG".bak
374 commit_message HEAD
> "$FIXUP_MSG" || die
"Cannot write $FIXUP_MSG"
377 echo "# This is a combination of 2 commits."
378 echo "# The first commit's message is:"
387 echo "# This is the $(nth_string $COUNT) commit message:"
393 echo "# The $(nth_string $COUNT) commit message will be skipped:"
395 commit_message
$2 |
sed -e 's/^/# /'
400 peek_next_command
() {
401 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
404 # A squash/fixup has failed. Prepare the long version of the squash
405 # commit message, then die_with_patch. This code path requires the
406 # user to edit the combined commit message for all commits that have
407 # been squashed/fixedup so far. So also erase the old squash
408 # messages, effectively causing the combined commit to be used as the
409 # new basis for any further squash/fixups. Args: sha1 rest
410 die_failed_squash
() {
411 mv "$SQUASH_MSG" "$MSG" ||
exit
413 cp "$MSG" "$GIT_DIR"/MERGE_MSG ||
exit
415 warn
"Could not apply $1... $2"
420 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" ||
exit
421 read command sha1 rest
< "$TODO"
427 comment_for_reflog pick
431 die_with_patch
$sha1 "Could not apply $sha1... $rest"
434 comment_for_reflog reword
438 die_with_patch
$sha1 "Could not apply $sha1... $rest"
442 comment_for_reflog edit
446 die_with_patch
$sha1 "Could not apply $sha1... $rest"
448 git rev-parse
--verify HEAD
> "$AMEND"
449 warn
"Stopped at $sha1... $rest"
450 warn
"You can amend the commit now, with"
452 warn
" git commit --amend"
454 warn
"Once you are satisfied with your changes, run"
456 warn
" git rebase --continue"
469 comment_for_reflog
$squash_style
471 test -f "$DONE" && has_action
"$DONE" ||
472 die
"Cannot '$squash_style' without a previous commit"
475 update_squash_messages
$squash_style $sha1
476 author_script
=$
(get_author_ident_from_commit HEAD
)
477 echo "$author_script" > "$AUTHOR_SCRIPT"
478 eval "$author_script"
479 output git
reset --soft HEAD^
480 pick_one
-n $sha1 || die_failed_squash
$sha1 "$rest"
481 case "$(peek_next_command)" in
483 # This is an intermediate commit; its message will only be
484 # used in case of trouble. So use the long version:
485 do_with_author output git commit
--no-verify -F "$SQUASH_MSG" ||
486 die_failed_squash
$sha1 "$rest"
489 # This is the final command of this squash/fixup group
490 if test -f "$FIXUP_MSG"
492 do_with_author git commit
--no-verify -F "$FIXUP_MSG" ||
493 die_failed_squash
$sha1 "$rest"
495 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG ||
exit
496 rm -f "$GIT_DIR"/MERGE_MSG
497 do_with_author git commit
--no-verify -e ||
498 die_failed_squash
$sha1 "$rest"
500 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
505 warn
"Unknown command: $command $sha1 $rest"
506 if git rev-parse
--verify -q "$sha1" >/dev
/null
508 die_with_patch
$sha1 "Please fix this in the file $TODO."
510 die
"Please fix this in the file $TODO."
514 test -s "$TODO" && return
516 comment_for_reflog finish
&&
517 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
518 OLDHEAD
=$
(cat "$DOTEST"/head) &&
519 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
520 NEWHEAD
=$
(git rev-parse HEAD
) &&
523 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
524 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
525 git symbolic-ref HEAD
$HEADNAME
528 test ! -f "$DOTEST"/verbose ||
529 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
533 warn
"Successfully rebased and updated $HEADNAME."
545 # skip picking commits whose parents are unchanged
546 skip_unnecessary_picks
() {
548 while read command sha1 rest
550 # fd=3 means we skip the command
551 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
552 3,pick
,"$ONTO"*|
3,p
,"$ONTO"*)
553 # pick a commit whose parent is current $ONTO -> skip
563 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
564 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
565 mv -f "$TODO".new
"$TODO" ||
566 die
"Could not skip unnecessary pick commands"
569 # check if no other options are set
571 test $# -eq 2 -a "$2" = '--' &&
573 test -z "$PRESERVE_MERGES" &&
574 test -z "$STRATEGY" &&
578 get_saved_options
() {
579 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
580 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
581 test -f "$DOTEST"/verbose
&& VERBOSE
=t
582 test -f "$DOTEST"/rebase-root
&& REBASE_ROOT
=t
585 # Rearrange the todo list that has both "pick sha1 msg" and
586 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
587 # comes immediately after the former, and change "pick" to
589 rearrange_squash
() {
590 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
591 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
593 test -s "$1.sq" ||
return
596 while read pick sha1 message
599 *" $sha1 "*) continue ;;
601 echo "$pick $sha1 $message"
602 while read squash action msg
606 echo "$action $squash $action! $msg"
611 done >"$1.rearranged" <"$1"
612 cat "$1.rearranged" >"$1"
613 rm -f "$1.sq" "$1.rearranged"
621 if left
=${1%...*} right
=${1#*...} &&
622 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
632 git rev-parse
--verify "$1^0"
639 OK_TO_SKIP_PRE_REBASE
=yes
644 is_standalone
"$@" || usage
646 comment_for_reflog
continue
648 test -d "$DOTEST" || die
"No interactive rebase running"
651 git rev-parse
--verify HEAD
>/dev
/null ||
652 die
"Cannot read HEAD"
653 git update-index
--ignore-submodules --refresh &&
654 git diff-files
--quiet --ignore-submodules ||
655 die
"Working tree is dirty"
657 # do we have anything to commit?
658 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
660 : Nothing to commit
-- skip this
662 .
"$AUTHOR_SCRIPT" ||
663 die
"Cannot find the author identity"
667 amend
=$
(git rev-parse
--verify HEAD
)
668 test "$amend" = $
(cat "$AMEND") ||
670 You have uncommitted changes in your working tree. Please, commit them
671 first and then run 'git rebase --continue' again."
672 git
reset --soft HEAD^ ||
673 die
"Cannot rewind the HEAD"
675 do_with_author git commit
--no-verify -F "$MSG" -e ||
{
676 test -n "$amend" && git
reset --soft $amend
677 die
"Could not commit staged changes."
681 require_clean_work_tree
685 is_standalone
"$@" || usage
687 comment_for_reflog abort
690 test -d "$DOTEST" || die
"No interactive rebase running"
692 HEADNAME
=$
(cat "$DOTEST"/head-name
)
693 HEAD
=$
(cat "$DOTEST"/head)
696 git symbolic-ref HEAD
$HEADNAME
699 output git
reset --hard $HEAD &&
704 is_standalone
"$@" || usage
706 comment_for_reflog skip
709 test -d "$DOTEST" || die
"No interactive rebase running"
711 output git
reset --hard && do_rest
716 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
725 # we use merge anyway
744 ONTO
=$
(parse_onto
"$1") ||
745 die
"Does not point to a valid commit: $1"
749 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
750 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
752 die
"Interactive rebase already started"
754 git var GIT_COMMITTER_IDENT
>/dev
/null ||
755 die
"You need to set your committer info first"
757 if test -z "$REBASE_ROOT"
760 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
761 test -z "$ONTO" && ONTO
=$UPSTREAM
767 die
"You must specify --onto when using --root"
769 run_pre_rebase_hook
"$UPSTREAM_ARG" "$@"
771 comment_for_reflog start
773 require_clean_work_tree
777 output git show-ref
--verify --quiet "refs/heads/$1" ||
778 die
"Invalid branchname: $1"
779 output git checkout
"$1" ||
780 die
"Could not checkout $1"
783 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
784 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
786 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
787 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
788 echo "detached HEAD" > "$DOTEST"/head-name
790 echo $HEAD > "$DOTEST"/head
791 case "$REBASE_ROOT" in
793 rm -f "$DOTEST"/rebase-root
;;
795 : >"$DOTEST"/rebase-root
;;
797 echo $ONTO > "$DOTEST"/onto
798 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
799 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
800 if test t
= "$PRESERVE_MERGES"
802 if test -z "$REBASE_ROOT"
804 mkdir
"$REWRITTEN" &&
805 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
807 echo $ONTO > "$REWRITTEN"/$c ||
808 die
"Could not init rewritten commits"
811 mkdir
"$REWRITTEN" &&
812 echo $ONTO > "$REWRITTEN"/root ||
813 die
"Could not init rewritten commits"
815 # No cherry-pick because our first pass is to determine
816 # parents to rewrite and skipping dropped commits would
817 # prematurely end our probe
819 first_after_upstream
="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
821 MERGES_OPTION
="--no-merges --cherry-pick"
824 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
825 SHORTONTO
=$
(git rev-parse
--short $ONTO)
826 if test -z "$REBASE_ROOT"
827 # this is now equivalent to ! -z "$UPSTREAM"
829 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
830 REVISIONS
=$UPSTREAM...
$HEAD
831 SHORTREVISIONS
=$SHORTUPSTREAM..
$SHORTHEAD
833 REVISIONS
=$ONTO...
$HEAD
834 SHORTREVISIONS
=$SHORTHEAD
836 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
837 --abbrev=7 --reverse --left-right --topo-order \
839 sed -n "s/^>//p" |
while read shortsha1 rest
841 if test t
!= "$PRESERVE_MERGES"
843 echo "pick $shortsha1 $rest" >> "$TODO"
845 sha1
=$
(git rev-parse
$shortsha1)
846 if test -z "$REBASE_ROOT"
849 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -s -f2-)
851 if test -f "$REWRITTEN"/$p -a \
( $p != $ONTO -o $sha1 = $first_after_upstream \
)
859 if test f
= "$preserve"
861 touch "$REWRITTEN"/$sha1
862 echo "pick $shortsha1 $rest" >> "$TODO"
867 # Watch for commits that been dropped by --cherry-pick
868 if test t
= "$PRESERVE_MERGES"
871 # Save all non-cherry-picked changes
872 git rev-list
$REVISIONS --left-right --cherry-pick | \
873 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
874 # Now all commits and note which ones are missing in
875 # not-cherry-picks and hence being dropped
876 git rev-list
$REVISIONS |
879 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
881 # Use -f2 because if rev-list is telling us this commit is
882 # not worthwhile, we don't want to track its multiple heads,
883 # just the history of its first-parent for others that will
884 # be rebasing on top of it
885 git rev-list
--parents -1 $rev | cut
-d' ' -s -f2 > "$DROPPED"/$rev
886 short
=$
(git rev-list
-1 --abbrev-commit --abbrev=7 $rev)
887 sane_grep
-v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
893 test -s "$TODO" ||
echo noop
>> "$TODO"
894 test -n "$AUTOSQUASH" && rearrange_squash
"$TODO"
895 cat >> "$TODO" << EOF
897 # Rebase $SHORTREVISIONS onto $SHORTONTO
900 # p, pick = use commit
901 # r, reword = use commit, but edit the commit message
902 # e, edit = use commit, but stop for amending
903 # s, squash = use commit, but meld into previous commit
904 # f, fixup = like "squash", but discard this commit's log message
906 # If you remove a line here THAT COMMIT WILL BE LOST.
907 # However, if you remove everything, the rebase will be aborted.
911 has_action
"$TODO" ||
912 die_abort
"Nothing to do"
914 cp "$TODO" "$TODO".backup
915 git_editor
"$TODO" ||
916 die_abort
"Could not execute editor"
918 has_action
"$TODO" ||
919 die_abort
"Nothing to do"
921 test -d "$REWRITTEN" || skip_unnecessary_picks
923 git update-ref ORIG_HEAD
$HEAD
924 output git checkout
$ONTO && do_rest