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 test -f "$DONE" && 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
366 cp "$MSG" "$SQUASH_MSG"
372 rm -f "$SQUASH_MSG" ||
exit
373 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
374 rm -f "$GIT_DIR"/MERGE_MSG ||
exit
377 echo "$author_script" > "$DOTEST"/author-script
380 # This is like --amend, but with a different message
381 eval "$author_script"
382 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
383 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
384 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
385 $USE_OUTPUT git commit
--no-verify \
386 $MSG_OPT "$EDIT_OR_FILE" || failed
=t
390 cp "$MSG" "$GIT_DIR"/MERGE_MSG
392 warn
"Could not apply $sha1... $rest"
393 die_with_patch
$sha1 ""
397 warn
"Unknown command: $command $sha1 $rest"
398 die_with_patch
$sha1 "Please fix this in the file $TODO."
401 test -s "$TODO" && return
403 comment_for_reflog finish
&&
404 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
405 OLDHEAD
=$
(cat "$DOTEST"/head) &&
406 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
407 NEWHEAD
=$
(git rev-parse HEAD
) &&
410 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
411 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
412 git symbolic-ref HEAD
$HEADNAME
415 test ! -f "$DOTEST"/verbose ||
416 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
420 warn
"Successfully rebased and updated $HEADNAME."
432 # check if no other options are set
434 test $# -eq 2 -a "$2" = '--' &&
436 test -z "$PRESERVE_MERGES" &&
437 test -z "$STRATEGY" &&
441 get_saved_options
() {
442 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
443 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
444 test -f "$DOTEST"/verbose
&& VERBOSE
=t
451 OK_TO_SKIP_PRE_REBASE
=yes
456 is_standalone
"$@" || usage
458 comment_for_reflog
continue
460 test -d "$DOTEST" || die
"No interactive rebase running"
463 git rev-parse
--verify HEAD
>/dev
/null ||
464 die
"Cannot read HEAD"
465 git update-index
--ignore-submodules --refresh &&
466 git diff-files
--quiet --ignore-submodules ||
467 die
"Working tree is dirty"
469 # do we have anything to commit?
470 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
472 : Nothing to commit
-- skip this
474 .
"$DOTEST"/author-script ||
475 die
"Cannot find the author identity"
477 if test -f "$DOTEST"/amend
479 amend
=$
(git rev-parse
--verify HEAD
)
480 test "$amend" = $
(cat "$DOTEST"/amend
) ||
482 You have uncommitted changes in your working tree. Please, commit them
483 first and then run 'git rebase --continue' again."
484 git
reset --soft HEAD^ ||
485 die
"Cannot rewind the HEAD"
487 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
&&
488 git commit
--no-verify -F "$DOTEST"/message
-e ||
{
489 test -n "$amend" && git
reset --soft $amend
490 die
"Could not commit staged changes."
494 require_clean_work_tree
498 is_standalone
"$@" || usage
500 comment_for_reflog abort
503 test -d "$DOTEST" || die
"No interactive rebase running"
505 HEADNAME
=$
(cat "$DOTEST"/head-name
)
506 HEAD
=$
(cat "$DOTEST"/head)
509 git symbolic-ref HEAD
$HEADNAME
512 output git
reset --hard $HEAD &&
517 is_standalone
"$@" || usage
519 comment_for_reflog skip
522 test -d "$DOTEST" || die
"No interactive rebase running"
524 output git
reset --hard && do_rest
529 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
538 # we use merge anyway
551 ONTO
=$
(git rev-parse
--verify "$1") ||
552 die
"Does not point to a valid commit: $1"
556 run_pre_rebase_hook
${1+"$@"}
557 test $# -eq 1 -o $# -eq 2 || usage
559 die
"Interactive rebase already started"
561 git var GIT_COMMITTER_IDENT
>/dev
/null ||
562 die
"You need to set your committer info first"
564 comment_for_reflog start
566 require_clean_work_tree
568 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
569 test -z "$ONTO" && ONTO
=$UPSTREAM
573 output git show-ref
--verify --quiet "refs/heads/$2" ||
574 die
"Invalid branchname: $2"
575 output git checkout
"$2" ||
576 die
"Could not checkout $2"
579 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
580 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
582 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
583 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
584 echo "detached HEAD" > "$DOTEST"/head-name
586 echo $HEAD > "$DOTEST"/head
587 echo $UPSTREAM > "$DOTEST"/upstream
588 echo $ONTO > "$DOTEST"/onto
589 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
590 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
591 if test t
= "$PRESERVE_MERGES"
593 # $REWRITTEN contains files for each commit that is
594 # reachable by at least one merge base of $HEAD and
595 # $UPSTREAM. They are not necessarily rewritten, but
596 # their children might be.
597 # This ensures that commits on merged, but otherwise
598 # unrelated side branches are left alone. (Think "X"
599 # in the man page's example.)
600 mkdir
"$REWRITTEN" &&
601 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
603 echo $ONTO > "$REWRITTEN"/$c ||
604 die
"Could not init rewritten commits"
606 # No cherry-pick because our first pass is to determine
607 # parents to rewrite and skipping dropped commits would
608 # prematurely end our probe
610 first_after_upstream
="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
612 MERGES_OPTION
="--no-merges --cherry-pick"
615 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
616 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
617 SHORTONTO
=$
(git rev-parse
--short $ONTO)
618 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
619 --abbrev=7 --reverse --left-right --topo-order \
620 $UPSTREAM...
$HEAD | \
621 sed -n "s/^>//p" |
while read shortsha1 rest
623 if test t
!= "$PRESERVE_MERGES"
625 echo "pick $shortsha1 $rest" >> "$TODO"
627 sha1
=$
(git rev-parse
$shortsha1)
629 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -f2-)
631 if test -f "$REWRITTEN"/$p -a \
( $p != $UPSTREAM -o $sha1 = $first_after_upstream \
)
636 if test f
= "$preserve"
638 touch "$REWRITTEN"/$sha1
639 echo "pick $shortsha1 $rest" >> "$TODO"
644 # Watch for commits that been dropped by --cherry-pick
645 if test t
= "$PRESERVE_MERGES"
648 # Save all non-cherry-picked changes
649 git rev-list
$UPSTREAM...
$HEAD --left-right --cherry-pick | \
650 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
651 # Now all commits and note which ones are missing in
652 # not-cherry-picks and hence being dropped
653 git rev-list
$UPSTREAM..
$HEAD |
656 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
658 # Use -f2 because if rev-list is telling us this commit is
659 # not worthwhile, we don't want to track its multiple heads,
660 # just the history of its first-parent for others that will
661 # be rebasing on top of it
662 git rev-list
--parents -1 $rev | cut
-d' ' -f2 > "$DROPPED"/$rev
663 short
=$
(git rev-list
-1 --abbrev-commit --abbrev=7 $rev)
664 grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
669 test -s "$TODO" ||
echo noop
>> "$TODO"
670 cat >> "$TODO" << EOF
672 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
675 # p, pick = use commit
676 # e, edit = use commit, but stop for amending
677 # s, squash = use commit, but meld into previous commit
679 # If you remove a line here THAT COMMIT WILL BE LOST.
680 # However, if you remove everything, the rebase will be aborted.
684 has_action
"$TODO" ||
685 die_abort
"Nothing to do"
687 cp "$TODO" "$TODO".backup
688 git_editor
"$TODO" ||
689 die
"Could not execute editor"
691 has_action
"$TODO" ||
692 die_abort
"Nothing to do"
694 git update-ref ORIG_HEAD
$HEAD
695 output git checkout
$ONTO && do_rest