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 "$no_ff$current_sha1" = "$parent_sha1"; then
168 output git
reset --hard $sha1
169 test "a$1" = a-n
&& output git
reset --soft $current_sha1
170 sha1
=$
(git rev-parse
--short $sha1)
171 output warn Fast-forward to
$sha1
173 output git cherry-pick
"$@"
177 pick_one_preserving_merges
() {
188 sha1
=$
(git rev-parse
$sha1)
190 if test -f "$DOTEST"/current-commit
192 if test "$fast_forward" = t
194 cat "$DOTEST"/current-commit |
while read current_commit
196 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
198 rm "$DOTEST"/current-commit ||
199 die
"Cannot write current commit's replacement sha1"
203 echo $sha1 >> "$DOTEST"/current-commit
205 # rewrite parents; if none were rewritten, we can fast-forward.
207 pend
=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
208 if test "$pend" = " "
212 while [ "$pend" != "" ]
214 p
=$
(expr "$pend" : ' \([^ ]*\)')
217 if test -f "$REWRITTEN"/$p
219 new_p
=$
(cat "$REWRITTEN"/$p)
221 # If the todo reordered commits, and our parent is marked for
222 # rewriting, but hasn't been gotten to yet, assume the user meant to
223 # drop it on top of the current HEAD
226 new_p
=$
(git rev-parse HEAD
)
229 test $p != $new_p && fast_forward
=f
230 case "$new_parents" in
232 ;; # do nothing; that parent is already there
234 new_parents
="$new_parents $new_p"
238 if test -f "$DROPPED"/$p
241 replacement
="$(cat "$DROPPED"/$p)"
242 test -z "$replacement" && replacement
=root
243 pend
=" $replacement$pend"
245 new_parents
="$new_parents $p"
249 case $fast_forward in
251 output warn
"Fast-forward to $sha1"
252 output git
reset --hard $sha1 ||
253 die
"Cannot fast-forward to $sha1"
256 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
260 # detach HEAD to current parent
261 output git checkout
$first_parent 2> /dev
/null ||
262 die
"Cannot move HEAD to $first_parent"
265 case "$new_parents" in
267 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
270 author_script
=$
(get_author_ident_from_commit
$sha1)
271 eval "$author_script"
272 msg
="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
273 # No point in merging the first parent, that's HEAD
274 new_parents
=${new_parents# $first_parent}
275 if ! GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
276 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
277 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
278 output git merge
$STRATEGY -m "$msg" \
281 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
282 die_with_patch
$sha1 "Error redoing merge $sha1"
286 output git cherry-pick
"$@" ||
287 die_with_patch
$sha1 "Could not pick $sha1"
296 *1[0-9]|
*[04-9]) echo "$1"th
;;
303 make_squash_message
() {
304 if test -f "$SQUASH_MSG"; then
305 # We want to be careful about matching only the commit
306 # message comment lines generated by this function.
307 # "[snrt][tdh]" matches the nth_string endings.
308 COUNT
=$
(($
(sed -n "s/^# Th[^0-9]*\([1-9][0-9]*\)[snrt][tdh] commit message.*:/\1/p" \
309 < "$SQUASH_MSG" |
sed -ne '$p')+1))
310 echo "# This is a combination of $COUNT commits."
311 sed -e 1d
-e '2,/^./{
316 echo "# This is a combination of two commits."
317 echo "# The first commit's message is:"
319 git cat-file commit HEAD |
sed -e '1,/^$/d'
324 echo "# This is the $(nth_string $COUNT) commit message:"
326 git cat-file commit
$2 |
sed -e '1,/^$/d'
330 echo "# The $(nth_string $COUNT) commit message will be skipped:"
332 # Comment the lines of the commit message out using
333 # "# " rather than "# " to make them less likely to
334 # confuse the sed regexp above.
335 git cat-file commit
$2 |
sed -e '1,/^$/d' -e 's/^/# /'
340 peek_next_command
() {
341 sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO"
345 rm -f "$DOTEST"/message
"$DOTEST"/author-script \
346 "$DOTEST"/amend ||
exit
347 read command sha1 rest
< "$TODO"
353 comment_for_reflog pick
357 die_with_patch
$sha1 "Could not apply $sha1... $rest"
360 comment_for_reflog reword
364 die_with_patch
$sha1 "Could not apply $sha1... $rest"
368 comment_for_reflog edit
372 die_with_patch
$sha1 "Could not apply $sha1... $rest"
374 git rev-parse
--verify HEAD
> "$DOTEST"/amend
375 warn
"Stopped at $sha1... $rest"
376 warn
"You can amend the commit now, with"
378 warn
" git commit --amend"
380 warn
"Once you are satisfied with your changes, run"
382 warn
" git rebase --continue"
395 comment_for_reflog
$squash_style
397 test -f "$DONE" && has_action
"$DONE" ||
398 die
"Cannot '$squash_style' without a previous commit"
401 make_squash_message
$squash_style $sha1 > "$MSG"
403 author_script
=$
(get_author_ident_from_commit HEAD
)
404 output git
reset --soft HEAD^
405 pick_one
-n $sha1 || failed
=t
406 case "$(peek_next_command)" in
411 cp "$MSG" "$SQUASH_MSG"
417 rm -f "$SQUASH_MSG" ||
exit
418 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
419 rm -f "$GIT_DIR"/MERGE_MSG ||
exit
422 echo "$author_script" > "$DOTEST"/author-script
425 # This is like --amend, but with a different message
426 eval "$author_script"
427 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
428 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
429 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
430 $USE_OUTPUT git commit
--no-verify \
431 $MSG_OPT "$EDIT_OR_FILE" || failed
=t
435 cp "$MSG" "$GIT_DIR"/MERGE_MSG
437 warn
"Could not apply $sha1... $rest"
438 die_with_patch
$sha1 ""
442 warn
"Unknown command: $command $sha1 $rest"
443 if git rev-parse
--verify -q "$sha1" >/dev
/null
445 die_with_patch
$sha1 "Please fix this in the file $TODO."
447 die
"Please fix this in the file $TODO."
451 test -s "$TODO" && return
453 comment_for_reflog finish
&&
454 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
455 OLDHEAD
=$
(cat "$DOTEST"/head) &&
456 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
457 NEWHEAD
=$
(git rev-parse HEAD
) &&
460 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
461 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
462 git symbolic-ref HEAD
$HEADNAME
465 test ! -f "$DOTEST"/verbose ||
466 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
470 warn
"Successfully rebased and updated $HEADNAME."
482 # skip picking commits whose parents are unchanged
483 skip_unnecessary_picks
() {
485 while read command sha1 rest
487 # fd=3 means we skip the command
488 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
489 3,pick
,"$ONTO"*|
3,p
,"$ONTO"*)
490 # pick a commit whose parent is current $ONTO -> skip
500 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
501 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
502 mv -f "$TODO".new
"$TODO" ||
503 die
"Could not skip unnecessary pick commands"
506 # check if no other options are set
508 test $# -eq 2 -a "$2" = '--' &&
510 test -z "$PRESERVE_MERGES" &&
511 test -z "$STRATEGY" &&
515 get_saved_options
() {
516 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
517 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
518 test -f "$DOTEST"/verbose
&& VERBOSE
=t
519 test -f "$DOTEST"/rebase-root
&& REBASE_ROOT
=t
527 if left
=${1%...*} right
=${1#*...} &&
528 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
538 git rev-parse
--verify "$1^0"
545 OK_TO_SKIP_PRE_REBASE
=yes
550 is_standalone
"$@" || usage
552 comment_for_reflog
continue
554 test -d "$DOTEST" || die
"No interactive rebase running"
557 git rev-parse
--verify HEAD
>/dev
/null ||
558 die
"Cannot read HEAD"
559 git update-index
--ignore-submodules --refresh &&
560 git diff-files
--quiet --ignore-submodules ||
561 die
"Working tree is dirty"
563 # do we have anything to commit?
564 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
566 : Nothing to commit
-- skip this
568 .
"$DOTEST"/author-script ||
569 die
"Cannot find the author identity"
571 if test -f "$DOTEST"/amend
573 amend
=$
(git rev-parse
--verify HEAD
)
574 test "$amend" = $
(cat "$DOTEST"/amend
) ||
576 You have uncommitted changes in your working tree. Please, commit them
577 first and then run 'git rebase --continue' again."
578 git
reset --soft HEAD^ ||
579 die
"Cannot rewind the HEAD"
581 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
&&
582 git commit
--no-verify -F "$DOTEST"/message
-e ||
{
583 test -n "$amend" && git
reset --soft $amend
584 die
"Could not commit staged changes."
588 require_clean_work_tree
592 is_standalone
"$@" || usage
594 comment_for_reflog abort
597 test -d "$DOTEST" || die
"No interactive rebase running"
599 HEADNAME
=$
(cat "$DOTEST"/head-name
)
600 HEAD
=$
(cat "$DOTEST"/head)
603 git symbolic-ref HEAD
$HEADNAME
606 output git
reset --hard $HEAD &&
611 is_standalone
"$@" || usage
613 comment_for_reflog skip
616 test -d "$DOTEST" || die
"No interactive rebase running"
618 output git
reset --hard && do_rest
623 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
632 # we use merge anyway
648 ONTO
=$
(parse_onto
"$1") ||
649 die
"Does not point to a valid commit: $1"
653 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
654 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
656 die
"Interactive rebase already started"
658 git var GIT_COMMITTER_IDENT
>/dev
/null ||
659 die
"You need to set your committer info first"
661 if test -z "$REBASE_ROOT"
664 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
665 test -z "$ONTO" && ONTO
=$UPSTREAM
671 die
"You must specify --onto when using --root"
673 run_pre_rebase_hook
"$UPSTREAM_ARG" "$@"
675 comment_for_reflog start
677 require_clean_work_tree
681 output git show-ref
--verify --quiet "refs/heads/$1" ||
682 die
"Invalid branchname: $1"
683 output git checkout
"$1" ||
684 die
"Could not checkout $1"
687 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
688 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
690 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
691 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
692 echo "detached HEAD" > "$DOTEST"/head-name
694 echo $HEAD > "$DOTEST"/head
695 case "$REBASE_ROOT" in
697 rm -f "$DOTEST"/rebase-root
;;
699 : >"$DOTEST"/rebase-root
;;
701 echo $ONTO > "$DOTEST"/onto
702 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
703 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
704 if test t
= "$PRESERVE_MERGES"
706 # $REWRITTEN contains files for each commit that is
707 # reachable by at least one merge base of $HEAD and
708 # $UPSTREAM. They are not necessarily rewritten, but
709 # their children might be.
710 # This ensures that commits on merged, but otherwise
711 # unrelated side branches are left alone. (Think "X"
712 # in the man page's example.)
713 if test -z "$REBASE_ROOT"
715 mkdir
"$REWRITTEN" &&
716 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
718 echo $ONTO > "$REWRITTEN"/$c ||
719 die
"Could not init rewritten commits"
722 mkdir
"$REWRITTEN" &&
723 echo $ONTO > "$REWRITTEN"/root ||
724 die
"Could not init rewritten commits"
726 # No cherry-pick because our first pass is to determine
727 # parents to rewrite and skipping dropped commits would
728 # prematurely end our probe
730 first_after_upstream
="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
732 MERGES_OPTION
="--no-merges --cherry-pick"
735 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
736 SHORTONTO
=$
(git rev-parse
--short $ONTO)
737 if test -z "$REBASE_ROOT"
738 # this is now equivalent to ! -z "$UPSTREAM"
740 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
741 REVISIONS
=$UPSTREAM...
$HEAD
742 SHORTREVISIONS
=$SHORTUPSTREAM..
$SHORTHEAD
744 REVISIONS
=$ONTO...
$HEAD
745 SHORTREVISIONS
=$SHORTHEAD
747 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
748 --abbrev=7 --reverse --left-right --topo-order \
750 sed -n "s/^>//p" |
while read shortsha1 rest
752 if test t
!= "$PRESERVE_MERGES"
754 echo "pick $shortsha1 $rest" >> "$TODO"
756 sha1
=$
(git rev-parse
$shortsha1)
757 if test -z "$REBASE_ROOT"
760 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -s -f2-)
762 if test -f "$REWRITTEN"/$p -a \
( $p != $ONTO -o $sha1 = $first_after_upstream \
)
770 if test f
= "$preserve"
772 touch "$REWRITTEN"/$sha1
773 echo "pick $shortsha1 $rest" >> "$TODO"
778 # Watch for commits that been dropped by --cherry-pick
779 if test t
= "$PRESERVE_MERGES"
782 # Save all non-cherry-picked changes
783 git rev-list
$REVISIONS --left-right --cherry-pick | \
784 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
785 # Now all commits and note which ones are missing in
786 # not-cherry-picks and hence being dropped
787 git rev-list
$REVISIONS |
790 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
792 # Use -f2 because if rev-list is telling us this commit is
793 # not worthwhile, we don't want to track its multiple heads,
794 # just the history of its first-parent for others that will
795 # be rebasing on top of it
796 git rev-list
--parents -1 $rev | cut
-d' ' -s -f2 > "$DROPPED"/$rev
797 short
=$
(git rev-list
-1 --abbrev-commit --abbrev=7 $rev)
798 sane_grep
-v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
804 test -s "$TODO" ||
echo noop
>> "$TODO"
805 cat >> "$TODO" << EOF
807 # Rebase $SHORTREVISIONS onto $SHORTONTO
810 # p, pick = use commit
811 # r, reword = use commit, but edit the commit message
812 # e, edit = use commit, but stop for amending
813 # s, squash = use commit, but meld into previous commit
814 # f, fixup = like "squash", but discard this commit's log message
816 # If you remove a line here THAT COMMIT WILL BE LOST.
817 # However, if you remove everything, the rebase will be aborted.
821 has_action
"$TODO" ||
822 die_abort
"Nothing to do"
824 cp "$TODO" "$TODO".backup
825 git_editor
"$TODO" ||
826 die_abort
"Could not execute editor"
828 has_action
"$TODO" ||
829 die_abort
"Nothing to do"
831 test -d "$REWRITTEN" || skip_unnecessary_picks
833 git update-ref ORIG_HEAD
$HEAD
834 output git checkout
$ONTO && do_rest