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
34 DOTEST="$GIT_DIR/rebase-merge"
35 TODO="$DOTEST"/git-rebase-todo
38 SQUASH_MSG="$DOTEST"/message-squash
39 REWRITTEN="$DOTEST"/rewritten
40 DROPPED="$DOTEST"/dropped
46 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
47 mark the corrected paths with 'git add <paths>', and
48 run 'git rebase --continue'"
49 export GIT_CHERRY_PICK_HELP
60 test $status != 0 && printf "%s\n" "$output"
69 run_pre_rebase_hook () {
70 if test -x "$GIT_DIR/hooks/pre-rebase"
72 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
73 echo >&2 "The pre-rebase hook refused to rebase."
79 require_clean_work_tree () {
80 # test if working tree is dirty
81 git rev-parse --verify HEAD > /dev/null &&
82 git update-index --ignore-submodules --refresh &&
83 git diff-files --quiet --ignore-submodules &&
84 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
85 die "Working tree is dirty"
88 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
90 comment_for_reflog () {
91 case "$ORIG_REFLOG_ACTION" in
93 GIT_REFLOG_ACTION="rebase -i ($1)"
94 export GIT_REFLOG_ACTION
100 mark_action_done () {
101 sed -e 1q < "$TODO" >> "$DONE"
102 sed -e 1d < "$TODO" >> "$TODO".new
103 mv -f "$TODO".new "$TODO"
104 count=$(grep -c '^[^#]' < "$DONE")
105 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
106 if test "$last_count" != "$count"
109 printf "Rebasing (%d/%d)\r" $count $total
110 test -z "$VERBOSE" || echo
115 parent_sha1=$(git rev-parse --verify "$1"^) ||
116 die "Cannot get patch for $1^"
117 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
118 test -f "$DOTEST"/message ||
119 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
120 test -f "$DOTEST"/author-script ||
121 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
136 grep '^[^#]' "$1" >/dev/null
141 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
142 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
143 test -d "$REWRITTEN" &&
144 pick_one_preserving_merges "$@" && return
145 parent_sha1=$(git rev-parse --verify $sha1^) ||
146 die "Could not get the parent of $sha1"
147 current_sha1=$(git rev-parse --verify HEAD)
148 if test "$no_ff$current_sha1" = "$parent_sha1"; then
149 output git reset --hard $sha1
150 test "a$1" = a-n && output git reset --soft $current_sha1
151 sha1=$(git rev-parse --short $sha1)
152 output warn Fast forward to $sha1
154 output git cherry-pick "$@"
158 pick_one_preserving_merges () {
169 sha1=$(git rev-parse $sha1)
171 if test -f "$DOTEST"/current-commit
173 if [ "$fast_forward" == "t" ]
175 cat "$DOTEST"/current-commit | while read current_commit
177 git rev-parse HEAD > "$REWRITTEN"/$current_commit
179 rm "$DOTEST"/current-commit ||
180 die "Cannot write current commit's replacement sha1"
184 echo $sha1 >> "$DOTEST"/current-commit
186 # rewrite parents; if none were rewritten, we can fast-forward.
188 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
189 while [ "$pend" != "" ]
191 p=$(expr "$pend" : ' \([^ ]*\)')
194 if test -f "$REWRITTEN"/$p
196 new_p=$(cat "$REWRITTEN"/$p)
197 test $p != $new_p && fast_forward=f
198 case "$new_parents" in
200 ;; # do nothing; that parent is already there
202 new_parents="$new_parents $new_p"
206 if test -f "$DROPPED"/$p
209 pend=" $(cat "$DROPPED"/$p)$pend"
211 new_parents="$new_parents $p"
215 case $fast_forward in
217 output warn "Fast forward to $sha1"
218 output git reset --hard $sha1 ||
219 die "Cannot fast forward to $sha1"
222 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
224 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
225 # detach HEAD to current parent
226 output git checkout $first_parent 2> /dev/null ||
227 die "Cannot move HEAD to $first_parent"
229 case "$new_parents" in
232 author_script=$(get_author_ident_from_commit $sha1)
233 eval "$author_script"
234 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
235 # No point in merging the first parent, that's HEAD
236 new_parents=${new_parents# $first_parent}
237 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
238 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
239 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
240 output git merge $STRATEGY -m "$msg" \
244 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
245 die Error redoing merge $sha1
249 output git cherry-pick "$@" ||
250 die_with_patch $sha1 "Could not pick $sha1"
259 *1[0-9]|*[04-9]) echo "$1"th;;
266 make_squash_message () {
267 if test -f "$SQUASH_MSG"; then
268 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
269 < "$SQUASH_MSG" | sed -ne '$p')+1))
270 echo "# This is a combination of $COUNT commits."
271 sed -e 1d -e '2,/^./{
276 echo "# This is a combination of two commits."
277 echo "# The first commit's message is:"
279 git cat-file commit HEAD | sed -e '1,/^$/d'
282 echo "# This is the $(nth_string $COUNT) commit message:"
284 git cat-file commit $1 | sed -e '1,/^$/d'
287 peek_next_command () {
288 sed -n "1s/ .*$//p" < "$TODO"
292 rm -f "$DOTEST"/message "$DOTEST"/author-script \
293 "$DOTEST"/amend || exit
294 read command sha1 rest < "$TODO"
300 comment_for_reflog pick
304 die_with_patch $sha1 "Could not apply $sha1... $rest"
307 comment_for_reflog edit
311 die_with_patch $sha1 "Could not apply $sha1... $rest"
313 git rev-parse --verify HEAD > "$DOTEST"/amend
314 warn "Stopped at $sha1... $rest"
315 warn "You can amend the commit now, with"
317 warn " git commit --amend"
319 warn "Once you are satisfied with your changes, run"
321 warn " git rebase --continue"
326 comment_for_reflog squash
328 has_action "$DONE" ||
329 die "Cannot 'squash' without a previous commit"
332 make_squash_message $sha1 > "$MSG"
334 author_script=$(get_author_ident_from_commit HEAD)
335 output git reset --soft HEAD^
336 pick_one -n $sha1 || failed=t
337 case "$(peek_next_command)" in
343 cp "$MSG" "$SQUASH_MSG"
350 rm -f "$SQUASH_MSG" || exit
351 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
352 rm -f "$GIT_DIR"/MERGE_MSG || exit
355 echo "$author_script" > "$DOTEST"/author-script
358 # This is like --amend, but with a different message
359 eval "$author_script"
360 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
361 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
362 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
363 $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
367 cp "$MSG" "$GIT_DIR"/MERGE_MSG
369 warn "Could not apply $sha1... $rest"
370 die_with_patch $sha1 ""
374 warn "Unknown command: $command $sha1 $rest"
375 die_with_patch $sha1 "Please fix this in the file $TODO."
378 test -s "$TODO" && return
380 comment_for_reflog finish &&
381 HEADNAME=$(cat "$DOTEST"/head-name) &&
382 OLDHEAD=$(cat "$DOTEST"/head) &&
383 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
384 NEWHEAD=$(git rev-parse HEAD) &&
387 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
388 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
389 git symbolic-ref HEAD $HEADNAME
392 test ! -f "$DOTEST"/verbose ||
393 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
397 warn "Successfully rebased and updated $HEADNAME."
409 # check if no other options are set
411 test $# -eq 2 -a "$2" = '--' &&
413 test -z "$PRESERVE_MERGES" &&
414 test -z "$STRATEGY" &&
418 get_saved_options () {
419 test -d "$REWRITTEN" && PRESERVE_MERGES=t
420 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
421 test -f "$DOTEST"/verbose && VERBOSE=t
428 is_standalone "$@" || usage
430 comment_for_reflog continue
432 test -d "$DOTEST" || die "No interactive rebase running"
435 git rev-parse --verify HEAD >/dev/null ||
436 die "Cannot read HEAD"
437 git update-index --ignore-submodules --refresh &&
438 git diff-files --quiet --ignore-submodules ||
439 die "Working tree is dirty"
441 # do we have anything to commit?
442 if git diff-index --cached --quiet --ignore-submodules HEAD --
444 : Nothing to commit -- skip this
446 . "$DOTEST"/author-script ||
447 die "Cannot find the author identity"
449 if test -f "$DOTEST"/amend
451 amend=$(git rev-parse --verify HEAD)
452 test "$amend" = $(cat "$DOTEST"/amend) ||
454 You have uncommitted changes in your working tree. Please, commit them
455 first and then run 'git rebase --continue' again."
456 git reset --soft HEAD^ ||
457 die "Cannot rewind the HEAD"
459 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
460 git commit --no-verify -F "$DOTEST"/message -e || {
461 test -n "$amend" && git reset --soft $amend
462 die "Could not commit staged changes."
466 require_clean_work_tree
470 is_standalone "$@" || usage
472 comment_for_reflog abort
475 test -d "$DOTEST" || die "No interactive rebase running"
477 HEADNAME=$(cat "$DOTEST"/head-name)
478 HEAD=$(cat "$DOTEST"/head)
481 git symbolic-ref HEAD $HEADNAME
484 output git reset --hard $HEAD &&
489 is_standalone "$@" || usage
491 comment_for_reflog skip
494 test -d "$DOTEST" || die "No interactive rebase running"
496 output git reset --hard && do_rest
501 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
510 # we use merge anyway
523 ONTO=$(git rev-parse --verify "$1") ||
524 die "Does not point to a valid commit: $1"
528 run_pre_rebase_hook ${1+"$@"}
529 test $# -eq 1 -o $# -eq 2 || usage
531 die "Interactive rebase already started"
533 git var GIT_COMMITTER_IDENT >/dev/null ||
534 die "You need to set your committer info first"
536 comment_for_reflog start
538 require_clean_work_tree
540 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
541 test -z "$ONTO" && ONTO=$UPSTREAM
545 output git show-ref --verify --quiet "refs/heads/$2" ||
546 die "Invalid branchname: $2"
547 output git checkout "$2" ||
548 die "Could not checkout $2"
551 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
552 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
554 : > "$DOTEST"/interactive || die "Could not mark as interactive"
555 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
556 echo "detached HEAD" > "$DOTEST"/head-name
558 echo $HEAD > "$DOTEST"/head
559 echo $UPSTREAM > "$DOTEST"/upstream
560 echo $ONTO > "$DOTEST"/onto
561 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
562 test t = "$VERBOSE" && : > "$DOTEST"/verbose
563 if test t = "$PRESERVE_MERGES"
565 # $REWRITTEN contains files for each commit that is
566 # reachable by at least one merge base of $HEAD and
567 # $UPSTREAM. They are not necessarily rewritten, but
568 # their children might be.
569 # This ensures that commits on merged, but otherwise
570 # unrelated side branches are left alone. (Think "X"
571 # in the man page's example.)
572 mkdir "$REWRITTEN" &&
573 for c in $(git merge-base --all $HEAD $UPSTREAM)
575 echo $ONTO > "$REWRITTEN"/$c ||
576 die "Could not init rewritten commits"
580 MERGES_OPTION=--no-merges
583 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
584 SHORTHEAD=$(git rev-parse --short $HEAD)
585 SHORTONTO=$(git rev-parse --short $ONTO)
586 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
587 --abbrev=7 --reverse --left-right --cherry-pick \
588 $UPSTREAM...$HEAD | \
589 sed -n "s/^>/pick /p" > "$TODO"
590 test -s "$TODO" || echo noop >> "$TODO"
591 cat >> "$TODO" << EOF
593 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
596 # p, pick = use commit
597 # e, edit = use commit, but stop for amending
598 # s, squash = use commit, but meld into previous commit
600 # If you remove a line here THAT COMMIT WILL BE LOST.
601 # However, if you remove everything, the rebase will be aborted.
605 # Watch for commits that been dropped by --cherry-pick
606 if test t = "$PRESERVE_MERGES"
609 # drop the --cherry-pick parameter this time
610 git rev-list $MERGES_OPTION --abbrev-commit \
611 --abbrev=7 $UPSTREAM...$HEAD --left-right | \
612 sed -n "s/^>//p" | while read rev
614 grep --quiet "$rev" "$TODO"
617 # Use -f2 because if rev-list is telling this commit is not
618 # worthwhile, we don't want to track its multiple heads,
619 # just the history of its first-parent for others that will
620 # be rebasing on top of us
621 full=$(git rev-parse $rev)
622 git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$full
627 has_action "$TODO" ||
628 die_abort "Nothing to do"
630 cp "$TODO" "$TODO".backup
631 git_editor "$TODO" ||
632 die "Could not execute editor"
634 has_action "$TODO" ||
635 die_abort "Nothing to do"
637 git update-ref ORIG_HEAD $HEAD
638 output git checkout $ONTO && do_rest