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
35 DOTEST="$GIT_DIR/rebase-merge"
36 TODO="$DOTEST"/git-rebase-todo
39 SQUASH_MSG="$DOTEST"/message-squash
40 REWRITTEN="$DOTEST"/rewritten
41 DROPPED="$DOTEST"/dropped
46 OK_TO_SKIP_PRE_REBASE=
48 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
49 mark the corrected paths with 'git add <paths>', and
50 run 'git rebase --continue'"
51 export GIT_CHERRY_PICK_HELP
62 test $status != 0 && printf "%s\n" "$output"
71 run_pre_rebase_hook () {
72 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
73 test -x "$GIT_DIR/hooks/pre-rebase"
75 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
76 echo >&2 "The pre-rebase hook refused to rebase."
82 require_clean_work_tree () {
83 # test if working tree is dirty
84 git rev-parse --verify HEAD > /dev/null &&
85 git update-index --ignore-submodules --refresh &&
86 git diff-files --quiet --ignore-submodules &&
87 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
88 die "Working tree is dirty"
91 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
93 comment_for_reflog () {
94 case "$ORIG_REFLOG_ACTION" in
96 GIT_REFLOG_ACTION="rebase -i ($1)"
97 export GIT_REFLOG_ACTION
103 mark_action_done () {
104 sed -e 1q < "$TODO" >> "$DONE"
105 sed -e 1d < "$TODO" >> "$TODO".new
106 mv -f "$TODO".new "$TODO"
107 count=$(grep -c '^[^#]' < "$DONE")
108 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
109 if test "$last_count" != "$count"
112 printf "Rebasing (%d/%d)\r" $count $total
113 test -z "$VERBOSE" || echo
118 parent_sha1=$(git rev-parse --verify "$1"^) ||
119 die "Cannot get patch for $1^"
120 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
121 test -f "$DOTEST"/message ||
122 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
123 test -f "$DOTEST"/author-script ||
124 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
139 grep '^[^#]' "$1" >/dev/null
144 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
145 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
146 test -d "$REWRITTEN" &&
147 pick_one_preserving_merges "$@" && return
148 parent_sha1=$(git rev-parse --verify $sha1^) ||
149 die "Could not get the parent of $sha1"
150 current_sha1=$(git rev-parse --verify HEAD)
151 if test "$no_ff$current_sha1" = "$parent_sha1"; then
152 output git reset --hard $sha1
153 test "a$1" = a-n && output git reset --soft $current_sha1
154 sha1=$(git rev-parse --short $sha1)
155 output warn Fast forward to $sha1
157 output git cherry-pick "$@"
161 pick_one_preserving_merges () {
172 sha1=$(git rev-parse $sha1)
174 if test -f "$DOTEST"/current-commit
176 if test "$fast_forward" = t
178 cat "$DOTEST"/current-commit | while read current_commit
180 git rev-parse HEAD > "$REWRITTEN"/$current_commit
182 rm "$DOTEST"/current-commit ||
183 die "Cannot write current commit's replacement sha1"
187 echo $sha1 >> "$DOTEST"/current-commit
189 # rewrite parents; if none were rewritten, we can fast-forward.
191 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
192 while [ "$pend" != "" ]
194 p=$(expr "$pend" : ' \([^ ]*\)')
197 if test -f "$REWRITTEN"/$p
199 new_p=$(cat "$REWRITTEN"/$p)
201 # If the todo reordered commits, and our parent is marked for
202 # rewriting, but hasn't been gotten to yet, assume the user meant to
203 # drop it on top of the current HEAD
206 new_p=$(git rev-parse HEAD)
209 test $p != $new_p && fast_forward=f
210 case "$new_parents" in
212 ;; # do nothing; that parent is already there
214 new_parents="$new_parents $new_p"
218 if test -f "$DROPPED"/$p
221 pend=" $(cat "$DROPPED"/$p)$pend"
223 new_parents="$new_parents $p"
227 case $fast_forward in
229 output warn "Fast forward to $sha1"
230 output git reset --hard $sha1 ||
231 die "Cannot fast forward to $sha1"
234 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
238 # detach HEAD to current parent
239 output git checkout $first_parent 2> /dev/null ||
240 die "Cannot move HEAD to $first_parent"
243 case "$new_parents" in
245 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
248 author_script=$(get_author_ident_from_commit $sha1)
249 eval "$author_script"
250 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
251 # No point in merging the first parent, that's HEAD
252 new_parents=${new_parents# $first_parent}
253 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
254 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
255 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
256 output git merge $STRATEGY -m "$msg" \
260 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
261 die Error redoing merge $sha1
265 output git cherry-pick "$@" ||
266 die_with_patch $sha1 "Could not pick $sha1"
275 *1[0-9]|*[04-9]) echo "$1"th;;
282 make_squash_message () {
283 if test -f "$SQUASH_MSG"; then
284 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
285 < "$SQUASH_MSG" | sed -ne '$p')+1))
286 echo "# This is a combination of $COUNT commits."
287 sed -e 1d -e '2,/^./{
292 echo "# This is a combination of two commits."
293 echo "# The first commit's message is:"
295 git cat-file commit HEAD | sed -e '1,/^$/d'
298 echo "# This is the $(nth_string $COUNT) commit message:"
300 git cat-file commit $1 | sed -e '1,/^$/d'
303 peek_next_command () {
304 sed -n "1s/ .*$//p" < "$TODO"
308 rm -f "$DOTEST"/message "$DOTEST"/author-script \
309 "$DOTEST"/amend || exit
310 read command sha1 rest < "$TODO"
316 comment_for_reflog pick
320 die_with_patch $sha1 "Could not apply $sha1... $rest"
323 comment_for_reflog edit
327 die_with_patch $sha1 "Could not apply $sha1... $rest"
329 git rev-parse --verify HEAD > "$DOTEST"/amend
330 warn "Stopped at $sha1... $rest"
331 warn "You can amend the commit now, with"
333 warn " git commit --amend"
335 warn "Once you are satisfied with your changes, run"
337 warn " git rebase --continue"
342 comment_for_reflog squash
344 has_action "$DONE" ||
345 die "Cannot 'squash' without a previous commit"
348 make_squash_message $sha1 > "$MSG"
350 author_script=$(get_author_ident_from_commit HEAD)
351 output git reset --soft HEAD^
352 pick_one -n $sha1 || failed=t
353 case "$(peek_next_command)" in
359 cp "$MSG" "$SQUASH_MSG"
366 rm -f "$SQUASH_MSG" || exit
367 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
368 rm -f "$GIT_DIR"/MERGE_MSG || exit
371 echo "$author_script" > "$DOTEST"/author-script
374 # This is like --amend, but with a different message
375 eval "$author_script"
376 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
377 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
378 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
379 $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
383 cp "$MSG" "$GIT_DIR"/MERGE_MSG
385 warn "Could not apply $sha1... $rest"
386 die_with_patch $sha1 ""
390 warn "Unknown command: $command $sha1 $rest"
391 die_with_patch $sha1 "Please fix this in the file $TODO."
394 test -s "$TODO" && return
396 comment_for_reflog finish &&
397 HEADNAME=$(cat "$DOTEST"/head-name) &&
398 OLDHEAD=$(cat "$DOTEST"/head) &&
399 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
400 NEWHEAD=$(git rev-parse HEAD) &&
403 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
404 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
405 git symbolic-ref HEAD $HEADNAME
408 test ! -f "$DOTEST"/verbose ||
409 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
413 warn "Successfully rebased and updated $HEADNAME."
425 # check if no other options are set
427 test $# -eq 2 -a "$2" = '--' &&
429 test -z "$PRESERVE_MERGES" &&
430 test -z "$STRATEGY" &&
434 get_saved_options () {
435 test -d "$REWRITTEN" && PRESERVE_MERGES=t
436 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
437 test -f "$DOTEST"/verbose && VERBOSE=t
444 OK_TO_SKIP_PRE_REBASE=yes
449 is_standalone "$@" || usage
451 comment_for_reflog continue
453 test -d "$DOTEST" || die "No interactive rebase running"
456 git rev-parse --verify HEAD >/dev/null ||
457 die "Cannot read HEAD"
458 git update-index --ignore-submodules --refresh &&
459 git diff-files --quiet --ignore-submodules ||
460 die "Working tree is dirty"
462 # do we have anything to commit?
463 if git diff-index --cached --quiet --ignore-submodules HEAD --
465 : Nothing to commit -- skip this
467 . "$DOTEST"/author-script ||
468 die "Cannot find the author identity"
470 if test -f "$DOTEST"/amend
472 amend=$(git rev-parse --verify HEAD)
473 test "$amend" = $(cat "$DOTEST"/amend) ||
475 You have uncommitted changes in your working tree. Please, commit them
476 first and then run 'git rebase --continue' again."
477 git reset --soft HEAD^ ||
478 die "Cannot rewind the HEAD"
480 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
481 git commit --no-verify -F "$DOTEST"/message -e || {
482 test -n "$amend" && git reset --soft $amend
483 die "Could not commit staged changes."
487 require_clean_work_tree
491 is_standalone "$@" || usage
493 comment_for_reflog abort
496 test -d "$DOTEST" || die "No interactive rebase running"
498 HEADNAME=$(cat "$DOTEST"/head-name)
499 HEAD=$(cat "$DOTEST"/head)
502 git symbolic-ref HEAD $HEADNAME
505 output git reset --hard $HEAD &&
510 is_standalone "$@" || usage
512 comment_for_reflog skip
515 test -d "$DOTEST" || die "No interactive rebase running"
517 output git reset --hard && do_rest
522 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
531 # we use merge anyway
544 ONTO=$(git rev-parse --verify "$1") ||
545 die "Does not point to a valid commit: $1"
549 run_pre_rebase_hook ${1+"$@"}
550 test $# -eq 1 -o $# -eq 2 || usage
552 die "Interactive rebase already started"
554 git var GIT_COMMITTER_IDENT >/dev/null ||
555 die "You need to set your committer info first"
557 comment_for_reflog start
559 require_clean_work_tree
561 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
562 test -z "$ONTO" && ONTO=$UPSTREAM
566 output git show-ref --verify --quiet "refs/heads/$2" ||
567 die "Invalid branchname: $2"
568 output git checkout "$2" ||
569 die "Could not checkout $2"
572 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
573 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
575 : > "$DOTEST"/interactive || die "Could not mark as interactive"
576 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
577 echo "detached HEAD" > "$DOTEST"/head-name
579 echo $HEAD > "$DOTEST"/head
580 echo $UPSTREAM > "$DOTEST"/upstream
581 echo $ONTO > "$DOTEST"/onto
582 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
583 test t = "$VERBOSE" && : > "$DOTEST"/verbose
584 if test t = "$PRESERVE_MERGES"
586 # $REWRITTEN contains files for each commit that is
587 # reachable by at least one merge base of $HEAD and
588 # $UPSTREAM. They are not necessarily rewritten, but
589 # their children might be.
590 # This ensures that commits on merged, but otherwise
591 # unrelated side branches are left alone. (Think "X"
592 # in the man page's example.)
593 mkdir "$REWRITTEN" &&
594 for c in $(git merge-base --all $HEAD $UPSTREAM)
596 echo $ONTO > "$REWRITTEN"/$c ||
597 die "Could not init rewritten commits"
599 # No cherry-pick because our first pass is to determine
600 # parents to rewrite and skipping dropped commits would
601 # prematurely end our probe
603 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
605 MERGES_OPTION="--no-merges --cherry-pick"
608 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
609 SHORTHEAD=$(git rev-parse --short $HEAD)
610 SHORTONTO=$(git rev-parse --short $ONTO)
611 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
612 --abbrev=7 --reverse --left-right --topo-order \
613 $UPSTREAM...$HEAD | \
614 sed -n "s/^>//p" | while read shortsha1 rest
616 if test t != "$PRESERVE_MERGES"
618 echo "pick $shortsha1 $rest" >> "$TODO"
620 sha1=$(git rev-parse $shortsha1)
622 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
624 if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
629 if test f = "$preserve"
631 touch "$REWRITTEN"/$sha1
632 echo "pick $shortsha1 $rest" >> "$TODO"
637 # Watch for commits that been dropped by --cherry-pick
638 if test t = "$PRESERVE_MERGES"
641 # Save all non-cherry-picked changes
642 git rev-list $UPSTREAM...$HEAD --left-right --cherry-pick | \
643 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
644 # Now all commits and note which ones are missing in
645 # not-cherry-picks and hence being dropped
646 git rev-list $UPSTREAM..$HEAD |
649 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
651 # Use -f2 because if rev-list is telling us this commit is
652 # not worthwhile, we don't want to track its multiple heads,
653 # just the history of its first-parent for others that will
654 # be rebasing on top of it
655 git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$rev
656 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
657 grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
662 test -s "$TODO" || echo noop >> "$TODO"
663 cat >> "$TODO" << EOF
665 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
668 # p, pick = use commit
669 # e, edit = use commit, but stop for amending
670 # s, squash = use commit, but meld into previous commit
672 # If you remove a line here THAT COMMIT WILL BE LOST.
673 # However, if you remove everything, the rebase will be aborted.
677 has_action "$TODO" ||
678 die_abort "Nothing to do"
680 cp "$TODO" "$TODO".backup
681 git_editor "$TODO" ||
682 die "Could not execute editor"
684 has_action "$TODO" ||
685 die_abort "Nothing to do"
687 git update-ref ORIG_HEAD $HEAD
688 output git checkout $ONTO && do_rest