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
13 USAGE='(--continue | --abort | --skip | [--preserve-merges] [--verbose]
14 [--onto <branch>] <upstream> [<branch>])'
20 DOTEST="$GIT_DIR/.dotest-merge"
21 TODO="$DOTEST"/git-rebase-todo
24 SQUASH_MSG="$DOTEST"/message-squash
25 REWRITTEN="$DOTEST"/rewritten
29 test -d "$REWRITTEN" && PRESERVE_MERGES=t
30 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
31 test -f "$DOTEST"/verbose && VERBOSE=t
33 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
34 mark the corrected paths with 'git add <paths>', and
35 run 'git rebase --continue'"
36 export GIT_CHERRY_PICK_HELP
47 test $status != 0 && printf "%s\n" "$output"
56 require_clean_work_tree () {
57 # test if working tree is dirty
58 git rev-parse --verify HEAD > /dev/null &&
59 git update-index --refresh &&
60 git diff-files --quiet &&
61 git diff-index --cached --quiet HEAD -- ||
62 die "Working tree is dirty"
65 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
67 comment_for_reflog () {
68 case "$ORIG_REFLOG_ACTION" in
70 GIT_REFLOG_ACTION="rebase -i ($1)"
71 export GIT_REFLOG_ACTION
77 sed -e 1q < "$TODO" >> "$DONE"
78 sed -e 1d < "$TODO" >> "$TODO".new
79 mv -f "$TODO".new "$TODO"
80 count=$(($(grep -ve '^$' -e '^#' < "$DONE" | wc -l)))
81 total=$(($count+$(grep -ve '^$' -e '^#' < "$TODO" | wc -l)))
82 printf "Rebasing (%d/%d)\r" $count $total
83 test -z "$VERBOSE" || echo
87 parent_sha1=$(git rev-parse --verify "$1"^) ||
88 die "Cannot get patch for $1^"
89 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
90 test -f "$DOTEST"/message ||
91 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
92 test -f "$DOTEST"/author-script ||
93 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
108 grep -vqe '^$' -e '^#' "$1"
113 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
114 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
115 test -d "$REWRITTEN" &&
116 pick_one_preserving_merges "$@" && return
117 parent_sha1=$(git rev-parse --verify $sha1^) ||
118 die "Could not get the parent of $sha1"
119 current_sha1=$(git rev-parse --verify HEAD)
120 if test "$no_ff$current_sha1" = "$parent_sha1"; then
121 output git reset --hard $sha1
122 test "a$1" = a-n && output git reset --soft $current_sha1
123 sha1=$(git rev-parse --short $sha1)
124 output warn Fast forward to $sha1
126 output git cherry-pick "$@"
130 pick_one_preserving_merges () {
131 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
132 sha1=$(git rev-parse $sha1)
134 if test -f "$DOTEST"/current-commit
136 current_commit=$(cat "$DOTEST"/current-commit) &&
137 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
138 rm "$DOTEST"/current-commit ||
139 die "Cannot write current commit's replacement sha1"
142 # rewrite parents; if none were rewritten, we can fast-forward.
146 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
148 if test -f "$REWRITTEN"/$p
151 new_p=$(cat "$REWRITTEN"/$p)
152 test $p != $new_p && fast_forward=f
153 case "$new_parents" in
155 ;; # do nothing; that parent is already there
157 new_parents="$new_parents $new_p"
162 case $fast_forward in
164 output warn "Fast forward to $sha1"
165 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
168 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
170 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
171 # detach HEAD to current parent
172 output git checkout $first_parent 2> /dev/null ||
173 die "Cannot move HEAD to $first_parent"
175 echo $sha1 > "$DOTEST"/current-commit
176 case "$new_parents" in
179 author_script=$(get_author_ident_from_commit $sha1)
180 eval "$author_script"
181 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
182 # No point in merging the first parent, that's HEAD
183 new_parents=${new_parents# $first_parent}
184 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
185 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
186 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
187 output git merge $STRATEGY -m "$msg" \
191 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
192 die Error redoing merge $sha1
196 output git cherry-pick "$@" ||
197 die_with_patch $sha1 "Could not pick $sha1"
206 *1[0-9]|*[04-9]) echo "$1"th;;
213 make_squash_message () {
214 if test -f "$SQUASH_MSG"; then
215 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
216 < "$SQUASH_MSG" | tail -n 1)+1))
217 echo "# This is a combination of $COUNT commits."
218 sed -e 1d -e '2,/^./{
223 echo "# This is a combination of two commits."
224 echo "# The first commit's message is:"
226 git cat-file commit HEAD | sed -e '1,/^$/d'
229 echo "# This is the $(nth_string $COUNT) commit message:"
231 git cat-file commit $1 | sed -e '1,/^$/d'
234 peek_next_command () {
235 sed -n "1s/ .*$//p" < "$TODO"
239 rm -f "$DOTEST"/message "$DOTEST"/author-script \
240 "$DOTEST"/amend || exit
241 read command sha1 rest < "$TODO"
247 comment_for_reflog pick
251 die_with_patch $sha1 "Could not apply $sha1... $rest"
254 comment_for_reflog edit
258 die_with_patch $sha1 "Could not apply $sha1... $rest"
262 warn "You can amend the commit now, with"
264 warn " git commit --amend"
269 comment_for_reflog squash
271 has_action "$DONE" ||
272 die "Cannot 'squash' without a previous commit"
275 make_squash_message $sha1 > "$MSG"
276 case "$(peek_next_command)" in
280 cp "$MSG" "$SQUASH_MSG"
285 rm -f "$SQUASH_MSG" || exit
290 author_script=$(get_author_ident_from_commit HEAD)
291 output git reset --soft HEAD^
292 pick_one -n $sha1 || failed=t
293 echo "$author_script" > "$DOTEST"/author-script
296 # This is like --amend, but with a different message
297 eval "$author_script"
298 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
299 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
300 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
301 $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
305 cp "$MSG" "$GIT_DIR"/MERGE_MSG
307 warn "Could not apply $sha1... $rest"
308 die_with_patch $sha1 ""
312 warn "Unknown command: $command $sha1 $rest"
313 die_with_patch $sha1 "Please fix this in the file $TODO."
316 test -s "$TODO" && return
318 comment_for_reflog finish &&
319 HEADNAME=$(cat "$DOTEST"/head-name) &&
320 OLDHEAD=$(cat "$DOTEST"/head) &&
321 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
322 if test -d "$REWRITTEN"
324 test -f "$DOTEST"/current-commit &&
325 current_commit=$(cat "$DOTEST"/current-commit) &&
326 git rev-parse HEAD > "$REWRITTEN"/$current_commit
327 if test -f "$REWRITTEN"/$OLDHEAD
329 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
334 NEWHEAD=$(git rev-parse HEAD)
338 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
339 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
340 git symbolic-ref HEAD $HEADNAME
343 test ! -f "$DOTEST"/verbose ||
344 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
348 warn "Successfully rebased and updated $HEADNAME."
364 comment_for_reflog continue
366 test -d "$DOTEST" || die "No interactive rebase running"
369 git rev-parse --verify HEAD >/dev/null ||
370 die "Cannot read HEAD"
371 git update-index --refresh && git diff-files --quiet ||
372 die "Working tree is dirty"
374 # do we have anything to commit?
375 if git diff-index --cached --quiet HEAD --
377 : Nothing to commit -- skip this
379 . "$DOTEST"/author-script ||
380 die "Cannot find the author identity"
381 if test -f "$DOTEST"/amend
383 git reset --soft HEAD^ ||
384 die "Cannot rewind the HEAD"
386 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
387 git commit --no-verify -F "$DOTEST"/message -e ||
388 die "Could not commit staged changes."
391 require_clean_work_tree
395 comment_for_reflog abort
398 test -d "$DOTEST" || die "No interactive rebase running"
400 HEADNAME=$(cat "$DOTEST"/head-name)
401 HEAD=$(cat "$DOTEST"/head)
404 git symbolic-ref HEAD $HEADNAME
407 output git reset --hard $HEAD &&
412 comment_for_reflog skip
415 test -d "$DOTEST" || die "No interactive rebase running"
417 output git reset --hard && do_rest
422 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
431 # we use merge anyway
434 die "Interactive rebase uses merge, so $1 does not make sense"
439 -p|--preserve-merges)
450 die "Interactive rebase already started"
452 git var GIT_COMMITTER_IDENT >/dev/null ||
453 die "You need to set your committer info first"
455 comment_for_reflog start
460 ONTO=$(git rev-parse --verify "$2") ||
461 die "Does not point to a valid commit: $2"
466 require_clean_work_tree
470 output git show-ref --verify --quiet "refs/heads/$2" ||
471 die "Invalid branchname: $2"
472 output git checkout "$2" ||
473 die "Could not checkout $2"
476 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
477 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
479 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
481 test -z "$ONTO" && ONTO=$UPSTREAM
483 : > "$DOTEST"/interactive || die "Could not mark as interactive"
484 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
485 echo "detached HEAD" > "$DOTEST"/head-name
487 echo $HEAD > "$DOTEST"/head
488 echo $UPSTREAM > "$DOTEST"/upstream
489 echo $ONTO > "$DOTEST"/onto
490 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
491 test t = "$VERBOSE" && : > "$DOTEST"/verbose
492 if test t = "$PRESERVE_MERGES"
494 # $REWRITTEN contains files for each commit that is
495 # reachable by at least one merge base of $HEAD and
496 # $UPSTREAM. They are not necessarily rewritten, but
497 # their children might be.
498 # This ensures that commits on merged, but otherwise
499 # unrelated side branches are left alone. (Think "X"
500 # in the man page's example.)
501 mkdir "$REWRITTEN" &&
502 for c in $(git merge-base --all $HEAD $UPSTREAM)
504 echo $ONTO > "$REWRITTEN"/$c ||
505 die "Could not init rewritten commits"
509 MERGES_OPTION=--no-merges
512 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
513 SHORTHEAD=$(git rev-parse --short $HEAD)
514 SHORTONTO=$(git rev-parse --short $ONTO)
515 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
516 --abbrev=7 --reverse --left-right --cherry-pick \
517 $UPSTREAM...$HEAD | \
518 sed -n "s/^>/pick /p" > "$TODO"
519 cat >> "$TODO" << EOF
521 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
525 # edit = use commit, but stop for amending
526 # squash = use commit, but meld into previous commit
528 # If you remove a line here THAT COMMIT WILL BE LOST.
529 # However, if you remove everything, the rebase will be aborted.
533 has_action "$TODO" ||
534 die_abort "Nothing to do"
536 cp "$TODO" "$TODO".backup
537 git_editor "$TODO" ||
538 die "Could not execute editor"
540 has_action "$TODO" ||
541 die_abort "Nothing to do"
543 output git checkout $ONTO && do_rest