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 current_commit=$(cat "$DOTEST"/current-commit) &&
174 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
175 rm "$DOTEST"/current-commit ||
176 die "Cannot write current commit's replacement sha1"
179 echo $sha1 > "$DOTEST"/current-commit
181 # rewrite parents; if none were rewritten, we can fast-forward.
183 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
184 while [ "$pend" != "" ]
186 p=$(expr "$pend" : ' \([^ ]*\)')
189 if test -f "$REWRITTEN"/$p
191 new_p=$(cat "$REWRITTEN"/$p)
192 test $p != $new_p && fast_forward=f
193 case "$new_parents" in
195 ;; # do nothing; that parent is already there
197 new_parents="$new_parents $new_p"
201 if test -f "$DROPPED"/$p
204 pend=" $(cat "$DROPPED"/$p)$pend"
206 new_parents="$new_parents $p"
210 case $fast_forward in
212 output warn "Fast forward to $sha1"
213 output git reset --hard $sha1 ||
214 die "Cannot fast forward to $sha1"
217 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
219 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
220 # detach HEAD to current parent
221 output git checkout $first_parent 2> /dev/null ||
222 die "Cannot move HEAD to $first_parent"
224 case "$new_parents" in
227 author_script=$(get_author_ident_from_commit $sha1)
228 eval "$author_script"
229 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
230 # No point in merging the first parent, that's HEAD
231 new_parents=${new_parents# $first_parent}
232 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
233 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
234 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
235 output git merge $STRATEGY -m "$msg" \
239 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
240 die Error redoing merge $sha1
244 output git cherry-pick "$@" ||
245 die_with_patch $sha1 "Could not pick $sha1"
254 *1[0-9]|*[04-9]) echo "$1"th;;
261 make_squash_message () {
262 if test -f "$SQUASH_MSG"; then
263 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
264 < "$SQUASH_MSG" | sed -ne '$p')+1))
265 echo "# This is a combination of $COUNT commits."
266 sed -e 1d -e '2,/^./{
271 echo "# This is a combination of two commits."
272 echo "# The first commit's message is:"
274 git cat-file commit HEAD | sed -e '1,/^$/d'
277 echo "# This is the $(nth_string $COUNT) commit message:"
279 git cat-file commit $1 | sed -e '1,/^$/d'
282 peek_next_command () {
283 sed -n "1s/ .*$//p" < "$TODO"
287 rm -f "$DOTEST"/message "$DOTEST"/author-script \
288 "$DOTEST"/amend || exit
289 read command sha1 rest < "$TODO"
295 comment_for_reflog pick
299 die_with_patch $sha1 "Could not apply $sha1... $rest"
302 comment_for_reflog edit
306 die_with_patch $sha1 "Could not apply $sha1... $rest"
308 git rev-parse --verify HEAD > "$DOTEST"/amend
309 warn "Stopped at $sha1... $rest"
310 warn "You can amend the commit now, with"
312 warn " git commit --amend"
314 warn "Once you are satisfied with your changes, run"
316 warn " git rebase --continue"
321 comment_for_reflog squash
323 has_action "$DONE" ||
324 die "Cannot 'squash' without a previous commit"
327 make_squash_message $sha1 > "$MSG"
329 author_script=$(get_author_ident_from_commit HEAD)
330 output git reset --soft HEAD^
331 pick_one -n $sha1 || failed=t
332 case "$(peek_next_command)" in
338 cp "$MSG" "$SQUASH_MSG"
345 rm -f "$SQUASH_MSG" || exit
346 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
347 rm -f "$GIT_DIR"/MERGE_MSG || exit
350 echo "$author_script" > "$DOTEST"/author-script
353 # This is like --amend, but with a different message
354 eval "$author_script"
355 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
356 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
357 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
358 $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
362 cp "$MSG" "$GIT_DIR"/MERGE_MSG
364 warn "Could not apply $sha1... $rest"
365 die_with_patch $sha1 ""
369 warn "Unknown command: $command $sha1 $rest"
370 die_with_patch $sha1 "Please fix this in the file $TODO."
373 test -s "$TODO" && return
375 comment_for_reflog finish &&
376 HEADNAME=$(cat "$DOTEST"/head-name) &&
377 OLDHEAD=$(cat "$DOTEST"/head) &&
378 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
379 NEWHEAD=$(git rev-parse HEAD) &&
382 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
383 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
384 git symbolic-ref HEAD $HEADNAME
387 test ! -f "$DOTEST"/verbose ||
388 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
392 warn "Successfully rebased and updated $HEADNAME."
404 # check if no other options are set
406 test $# -eq 2 -a "$2" = '--' &&
408 test -z "$PRESERVE_MERGES" &&
409 test -z "$STRATEGY" &&
413 get_saved_options () {
414 test -d "$REWRITTEN" && PRESERVE_MERGES=t
415 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
416 test -f "$DOTEST"/verbose && VERBOSE=t
423 is_standalone "$@" || usage
425 comment_for_reflog continue
427 test -d "$DOTEST" || die "No interactive rebase running"
430 git rev-parse --verify HEAD >/dev/null ||
431 die "Cannot read HEAD"
432 git update-index --ignore-submodules --refresh &&
433 git diff-files --quiet --ignore-submodules ||
434 die "Working tree is dirty"
436 # do we have anything to commit?
437 if git diff-index --cached --quiet --ignore-submodules HEAD --
439 : Nothing to commit -- skip this
441 . "$DOTEST"/author-script ||
442 die "Cannot find the author identity"
444 if test -f "$DOTEST"/amend
446 amend=$(git rev-parse --verify HEAD)
447 test "$amend" = $(cat "$DOTEST"/amend) ||
449 You have uncommitted changes in your working tree. Please, commit them
450 first and then run 'git rebase --continue' again."
451 git reset --soft HEAD^ ||
452 die "Cannot rewind the HEAD"
454 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
455 git commit --no-verify -F "$DOTEST"/message -e || {
456 test -n "$amend" && git reset --soft $amend
457 die "Could not commit staged changes."
461 require_clean_work_tree
465 is_standalone "$@" || usage
467 comment_for_reflog abort
470 test -d "$DOTEST" || die "No interactive rebase running"
472 HEADNAME=$(cat "$DOTEST"/head-name)
473 HEAD=$(cat "$DOTEST"/head)
476 git symbolic-ref HEAD $HEADNAME
479 output git reset --hard $HEAD &&
484 is_standalone "$@" || usage
486 comment_for_reflog skip
489 test -d "$DOTEST" || die "No interactive rebase running"
491 output git reset --hard && do_rest
496 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
505 # we use merge anyway
518 ONTO=$(git rev-parse --verify "$1") ||
519 die "Does not point to a valid commit: $1"
523 run_pre_rebase_hook ${1+"$@"}
524 test $# -eq 1 -o $# -eq 2 || usage
526 die "Interactive rebase already started"
528 git var GIT_COMMITTER_IDENT >/dev/null ||
529 die "You need to set your committer info first"
531 comment_for_reflog start
533 require_clean_work_tree
535 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
536 test -z "$ONTO" && ONTO=$UPSTREAM
540 output git show-ref --verify --quiet "refs/heads/$2" ||
541 die "Invalid branchname: $2"
542 output git checkout "$2" ||
543 die "Could not checkout $2"
546 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
547 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
549 : > "$DOTEST"/interactive || die "Could not mark as interactive"
550 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
551 echo "detached HEAD" > "$DOTEST"/head-name
553 echo $HEAD > "$DOTEST"/head
554 echo $UPSTREAM > "$DOTEST"/upstream
555 echo $ONTO > "$DOTEST"/onto
556 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
557 test t = "$VERBOSE" && : > "$DOTEST"/verbose
558 if test t = "$PRESERVE_MERGES"
560 # $REWRITTEN contains files for each commit that is
561 # reachable by at least one merge base of $HEAD and
562 # $UPSTREAM. They are not necessarily rewritten, but
563 # their children might be.
564 # This ensures that commits on merged, but otherwise
565 # unrelated side branches are left alone. (Think "X"
566 # in the man page's example.)
567 mkdir "$REWRITTEN" &&
568 for c in $(git merge-base --all $HEAD $UPSTREAM)
570 echo $ONTO > "$REWRITTEN"/$c ||
571 die "Could not init rewritten commits"
575 MERGES_OPTION=--no-merges
578 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
579 SHORTHEAD=$(git rev-parse --short $HEAD)
580 SHORTONTO=$(git rev-parse --short $ONTO)
581 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
582 --abbrev=7 --reverse --left-right --cherry-pick \
583 $UPSTREAM...$HEAD | \
584 sed -n "s/^>/pick /p" > "$TODO"
585 test -s "$TODO" || echo noop >> "$TODO"
586 cat >> "$TODO" << EOF
588 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
591 # p, pick = use commit
592 # e, edit = use commit, but stop for amending
593 # s, squash = use commit, but meld into previous commit
595 # If you remove a line here THAT COMMIT WILL BE LOST.
596 # However, if you remove everything, the rebase will be aborted.
600 # Watch for commits that been dropped by --cherry-pick
601 if test t = "$PRESERVE_MERGES"
604 # drop the --cherry-pick parameter this time
605 git rev-list $MERGES_OPTION --abbrev-commit \
606 --abbrev=7 $UPSTREAM...$HEAD --left-right | \
607 sed -n "s/^>//p" | while read rev
609 grep --quiet "$rev" "$TODO"
612 # Use -f2 because if rev-list is telling this commit is not
613 # worthwhile, we don't want to track its multiple heads,
614 # just the history of its first-parent for others that will
615 # be rebasing on top of us
616 full=$(git rev-parse $rev)
617 git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$full
622 has_action "$TODO" ||
623 die_abort "Nothing to do"
625 cp "$TODO" "$TODO".backup
626 git_editor "$TODO" ||
627 die "Could not execute editor"
629 has_action "$TODO" ||
630 die_abort "Nothing to do"
632 git update-ref ORIG_HEAD $HEAD
633 output git checkout $ONTO && do_rest