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 sha1_and_parents="$(git rev-list --parents -1 "$1")"
119 case "$sha1_and_parents" in
121 git diff --cc $sha1_and_parents
124 git diff-tree -p "$1^!"
129 esac > "$DOTEST"/patch
130 test -f "$DOTEST"/message ||
131 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
132 test -f "$DOTEST"/author-script ||
133 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
148 grep '^[^#]' "$1" >/dev/null
153 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
154 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
155 test -d "$REWRITTEN" &&
156 pick_one_preserving_merges "$@" && return
157 parent_sha1=$(git rev-parse --verify $sha1^) ||
158 die "Could not get the parent of $sha1"
159 current_sha1=$(git rev-parse --verify HEAD)
160 if test "$no_ff$current_sha1" = "$parent_sha1"; then
161 output git reset --hard $sha1
162 test "a$1" = a-n && output git reset --soft $current_sha1
163 sha1=$(git rev-parse --short $sha1)
164 output warn Fast forward to $sha1
166 output git cherry-pick "$@"
170 pick_one_preserving_merges () {
181 sha1=$(git rev-parse $sha1)
183 if test -f "$DOTEST"/current-commit
185 if test "$fast_forward" = t
187 cat "$DOTEST"/current-commit | while read current_commit
189 git rev-parse HEAD > "$REWRITTEN"/$current_commit
191 rm "$DOTEST"/current-commit ||
192 die "Cannot write current commit's replacement sha1"
196 echo $sha1 >> "$DOTEST"/current-commit
198 # rewrite parents; if none were rewritten, we can fast-forward.
200 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
201 while [ "$pend" != "" ]
203 p=$(expr "$pend" : ' \([^ ]*\)')
206 if test -f "$REWRITTEN"/$p
208 new_p=$(cat "$REWRITTEN"/$p)
210 # If the todo reordered commits, and our parent is marked for
211 # rewriting, but hasn't been gotten to yet, assume the user meant to
212 # drop it on top of the current HEAD
215 new_p=$(git rev-parse HEAD)
218 test $p != $new_p && fast_forward=f
219 case "$new_parents" in
221 ;; # do nothing; that parent is already there
223 new_parents="$new_parents $new_p"
227 if test -f "$DROPPED"/$p
230 pend=" $(cat "$DROPPED"/$p)$pend"
232 new_parents="$new_parents $p"
236 case $fast_forward in
238 output warn "Fast forward to $sha1"
239 output git reset --hard $sha1 ||
240 die "Cannot fast forward to $sha1"
243 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
247 # detach HEAD to current parent
248 output git checkout $first_parent 2> /dev/null ||
249 die "Cannot move HEAD to $first_parent"
252 case "$new_parents" in
254 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
257 author_script=$(get_author_ident_from_commit $sha1)
258 eval "$author_script"
259 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
260 # No point in merging the first parent, that's HEAD
261 new_parents=${new_parents# $first_parent}
262 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
263 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
264 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
265 output git merge $STRATEGY -m "$msg" \
268 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
269 die_with_patch $sha1 "Error redoing merge $sha1"
273 output git cherry-pick "$@" ||
274 die_with_patch $sha1 "Could not pick $sha1"
283 *1[0-9]|*[04-9]) echo "$1"th;;
290 make_squash_message () {
291 if test -f "$SQUASH_MSG"; then
292 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
293 < "$SQUASH_MSG" | sed -ne '$p')+1))
294 echo "# This is a combination of $COUNT commits."
295 sed -e 1d -e '2,/^./{
300 echo "# This is a combination of two commits."
301 echo "# The first commit's message is:"
303 git cat-file commit HEAD | sed -e '1,/^$/d'
306 echo "# This is the $(nth_string $COUNT) commit message:"
308 git cat-file commit $1 | sed -e '1,/^$/d'
311 peek_next_command () {
312 sed -n "1s/ .*$//p" < "$TODO"
316 rm -f "$DOTEST"/message "$DOTEST"/author-script \
317 "$DOTEST"/amend || exit
318 read command sha1 rest < "$TODO"
324 comment_for_reflog pick
328 die_with_patch $sha1 "Could not apply $sha1... $rest"
331 comment_for_reflog edit
335 die_with_patch $sha1 "Could not apply $sha1... $rest"
337 git rev-parse --verify HEAD > "$DOTEST"/amend
338 warn "Stopped at $sha1... $rest"
339 warn "You can amend the commit now, with"
341 warn " git commit --amend"
343 warn "Once you are satisfied with your changes, run"
345 warn " git rebase --continue"
350 comment_for_reflog squash
352 has_action "$DONE" ||
353 die "Cannot 'squash' without a previous commit"
356 make_squash_message $sha1 > "$MSG"
358 author_script=$(get_author_ident_from_commit HEAD)
359 output git reset --soft HEAD^
360 pick_one -n $sha1 || failed=t
361 case "$(peek_next_command)" in
367 cp "$MSG" "$SQUASH_MSG"
374 rm -f "$SQUASH_MSG" || exit
375 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
376 rm -f "$GIT_DIR"/MERGE_MSG || exit
379 echo "$author_script" > "$DOTEST"/author-script
382 # This is like --amend, but with a different message
383 eval "$author_script"
384 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
385 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
386 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
387 $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
391 cp "$MSG" "$GIT_DIR"/MERGE_MSG
393 warn "Could not apply $sha1... $rest"
394 die_with_patch $sha1 ""
398 warn "Unknown command: $command $sha1 $rest"
399 die_with_patch $sha1 "Please fix this in the file $TODO."
402 test -s "$TODO" && return
404 comment_for_reflog finish &&
405 HEADNAME=$(cat "$DOTEST"/head-name) &&
406 OLDHEAD=$(cat "$DOTEST"/head) &&
407 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
408 NEWHEAD=$(git rev-parse HEAD) &&
411 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
412 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
413 git symbolic-ref HEAD $HEADNAME
416 test ! -f "$DOTEST"/verbose ||
417 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
421 warn "Successfully rebased and updated $HEADNAME."
433 # check if no other options are set
435 test $# -eq 2 -a "$2" = '--' &&
437 test -z "$PRESERVE_MERGES" &&
438 test -z "$STRATEGY" &&
442 get_saved_options () {
443 test -d "$REWRITTEN" && PRESERVE_MERGES=t
444 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
445 test -f "$DOTEST"/verbose && VERBOSE=t
452 OK_TO_SKIP_PRE_REBASE=yes
457 is_standalone "$@" || usage
459 comment_for_reflog continue
461 test -d "$DOTEST" || die "No interactive rebase running"
464 git rev-parse --verify HEAD >/dev/null ||
465 die "Cannot read HEAD"
466 git update-index --ignore-submodules --refresh &&
467 git diff-files --quiet --ignore-submodules ||
468 die "Working tree is dirty"
470 # do we have anything to commit?
471 if git diff-index --cached --quiet --ignore-submodules HEAD --
473 : Nothing to commit -- skip this
475 . "$DOTEST"/author-script ||
476 die "Cannot find the author identity"
478 if test -f "$DOTEST"/amend
480 amend=$(git rev-parse --verify HEAD)
481 test "$amend" = $(cat "$DOTEST"/amend) ||
483 You have uncommitted changes in your working tree. Please, commit them
484 first and then run 'git rebase --continue' again."
485 git reset --soft HEAD^ ||
486 die "Cannot rewind the HEAD"
488 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
489 git commit --no-verify -F "$DOTEST"/message -e || {
490 test -n "$amend" && git reset --soft $amend
491 die "Could not commit staged changes."
495 require_clean_work_tree
499 is_standalone "$@" || usage
501 comment_for_reflog abort
504 test -d "$DOTEST" || die "No interactive rebase running"
506 HEADNAME=$(cat "$DOTEST"/head-name)
507 HEAD=$(cat "$DOTEST"/head)
510 git symbolic-ref HEAD $HEADNAME
513 output git reset --hard $HEAD &&
518 is_standalone "$@" || usage
520 comment_for_reflog skip
523 test -d "$DOTEST" || die "No interactive rebase running"
525 output git reset --hard && do_rest
530 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
539 # we use merge anyway
552 ONTO=$(git rev-parse --verify "$1") ||
553 die "Does not point to a valid commit: $1"
557 run_pre_rebase_hook ${1+"$@"}
558 test $# -eq 1 -o $# -eq 2 || usage
560 die "Interactive rebase already started"
562 git var GIT_COMMITTER_IDENT >/dev/null ||
563 die "You need to set your committer info first"
565 comment_for_reflog start
567 require_clean_work_tree
569 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
570 test -z "$ONTO" && ONTO=$UPSTREAM
574 output git show-ref --verify --quiet "refs/heads/$2" ||
575 die "Invalid branchname: $2"
576 output git checkout "$2" ||
577 die "Could not checkout $2"
580 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
581 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
583 : > "$DOTEST"/interactive || die "Could not mark as interactive"
584 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
585 echo "detached HEAD" > "$DOTEST"/head-name
587 echo $HEAD > "$DOTEST"/head
588 echo $UPSTREAM > "$DOTEST"/upstream
589 echo $ONTO > "$DOTEST"/onto
590 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
591 test t = "$VERBOSE" && : > "$DOTEST"/verbose
592 if test t = "$PRESERVE_MERGES"
594 # $REWRITTEN contains files for each commit that is
595 # reachable by at least one merge base of $HEAD and
596 # $UPSTREAM. They are not necessarily rewritten, but
597 # their children might be.
598 # This ensures that commits on merged, but otherwise
599 # unrelated side branches are left alone. (Think "X"
600 # in the man page's example.)
601 mkdir "$REWRITTEN" &&
602 for c in $(git merge-base --all $HEAD $UPSTREAM)
604 echo $ONTO > "$REWRITTEN"/$c ||
605 die "Could not init rewritten commits"
607 # No cherry-pick because our first pass is to determine
608 # parents to rewrite and skipping dropped commits would
609 # prematurely end our probe
611 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
613 MERGES_OPTION="--no-merges --cherry-pick"
616 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
617 SHORTHEAD=$(git rev-parse --short $HEAD)
618 SHORTONTO=$(git rev-parse --short $ONTO)
619 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
620 --abbrev=7 --reverse --left-right --topo-order \
621 $UPSTREAM...$HEAD | \
622 sed -n "s/^>//p" | while read shortsha1 rest
624 if test t != "$PRESERVE_MERGES"
626 echo "pick $shortsha1 $rest" >> "$TODO"
628 sha1=$(git rev-parse $shortsha1)
630 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
632 if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
637 if test f = "$preserve"
639 touch "$REWRITTEN"/$sha1
640 echo "pick $shortsha1 $rest" >> "$TODO"
645 # Watch for commits that been dropped by --cherry-pick
646 if test t = "$PRESERVE_MERGES"
649 # Save all non-cherry-picked changes
650 git rev-list $UPSTREAM...$HEAD --left-right --cherry-pick | \
651 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
652 # Now all commits and note which ones are missing in
653 # not-cherry-picks and hence being dropped
654 git rev-list $UPSTREAM..$HEAD |
657 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
659 # Use -f2 because if rev-list is telling us this commit is
660 # not worthwhile, we don't want to track its multiple heads,
661 # just the history of its first-parent for others that will
662 # be rebasing on top of it
663 git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$rev
664 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
665 grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
670 test -s "$TODO" || echo noop >> "$TODO"
671 cat >> "$TODO" << EOF
673 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
676 # p, pick = use commit
677 # e, edit = use commit, but stop for amending
678 # s, squash = use commit, but meld into previous commit
680 # If you remove a line here THAT COMMIT WILL BE LOST.
681 # However, if you remove everything, the rebase will be aborted.
685 has_action "$TODO" ||
686 die_abort "Nothing to do"
688 cp "$TODO" "$TODO".backup
689 git_editor "$TODO" ||
690 die "Could not execute editor"
692 has_action "$TODO" ||
693 die_abort "Nothing to do"
695 git update-ref ORIG_HEAD $HEAD
696 output git checkout $ONTO && do_rest