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 parent_sha1
=$
(git rev-parse
--verify "$1"^
) ||
119 die
"Cannot get patch for $1^"
120 git diff-tree
-p "$parent_sha1"..
"$1" > "$DOTEST"/patch
121 test -f "$DOTEST"/message ||
122 git cat-file commit
"$1" |
sed "1,/^$/d" > "$DOTEST"/message
123 test -f "$DOTEST"/author-script ||
124 get_author_ident_from_commit
"$1" > "$DOTEST"/author-script
139 grep '^[^#]' "$1" >/dev
/null
144 case "$1" in -n) sha1
=$2; no_ff
=t
;; *) sha1
=$1 ;; esac
145 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
146 test -d "$REWRITTEN" &&
147 pick_one_preserving_merges
"$@" && return
148 parent_sha1
=$
(git rev-parse
--verify $sha1^
) ||
149 die
"Could not get the parent of $sha1"
150 current_sha1
=$
(git rev-parse
--verify HEAD
)
151 if test "$no_ff$current_sha1" = "$parent_sha1"; then
152 output git
reset --hard $sha1
153 test "a$1" = a-n
&& output git
reset --soft $current_sha1
154 sha1
=$
(git rev-parse
--short $sha1)
155 output warn Fast forward to
$sha1
157 output git cherry-pick
"$@"
161 pick_one_preserving_merges
() {
172 sha1
=$
(git rev-parse
$sha1)
174 if test -f "$DOTEST"/current-commit
176 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
177 git rev-parse HEAD
> "$REWRITTEN"/$current_commit &&
178 rm "$DOTEST"/current-commit ||
179 die
"Cannot write current commit's replacement sha1"
182 echo $sha1 > "$DOTEST"/current-commit
184 # rewrite parents; if none were rewritten, we can fast-forward.
186 pend
=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
187 while [ "$pend" != "" ]
189 p
=$
(expr "$pend" : ' \([^ ]*\)')
192 if test -f "$REWRITTEN"/$p
194 new_p
=$
(cat "$REWRITTEN"/$p)
195 test $p != $new_p && fast_forward
=f
196 case "$new_parents" in
198 ;; # do nothing; that parent is already there
200 new_parents
="$new_parents $new_p"
204 if test -f "$DROPPED"/$p
207 pend
=" $(cat "$DROPPED"/$p)$pend"
209 new_parents
="$new_parents $p"
213 case $fast_forward in
215 output warn
"Fast forward to $sha1"
216 output git
reset --hard $sha1 ||
217 die
"Cannot fast forward to $sha1"
220 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
222 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
223 # detach HEAD to current parent
224 output git checkout
$first_parent 2> /dev
/null ||
225 die
"Cannot move HEAD to $first_parent"
227 case "$new_parents" in
230 author_script
=$
(get_author_ident_from_commit
$sha1)
231 eval "$author_script"
232 msg
="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
233 # No point in merging the first parent, that's HEAD
234 new_parents
=${new_parents# $first_parent}
235 if ! GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
236 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
237 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
238 output git merge
$STRATEGY -m "$msg" \
242 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
243 die Error redoing merge
$sha1
247 output git cherry-pick
"$@" ||
248 die_with_patch
$sha1 "Could not pick $sha1"
257 *1[0-9]|
*[04-9]) echo "$1"th
;;
264 make_squash_message
() {
265 if test -f "$SQUASH_MSG"; then
266 COUNT
=$
(($
(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
267 < "$SQUASH_MSG" |
sed -ne '$p')+1))
268 echo "# This is a combination of $COUNT commits."
269 sed -e 1d
-e '2,/^./{
274 echo "# This is a combination of two commits."
275 echo "# The first commit's message is:"
277 git cat-file commit HEAD |
sed -e '1,/^$/d'
280 echo "# This is the $(nth_string $COUNT) commit message:"
282 git cat-file commit
$1 |
sed -e '1,/^$/d'
285 peek_next_command
() {
286 sed -n "1s/ .*$//p" < "$TODO"
290 rm -f "$DOTEST"/message
"$DOTEST"/author-script \
291 "$DOTEST"/amend ||
exit
292 read command sha1 rest
< "$TODO"
298 comment_for_reflog pick
302 die_with_patch
$sha1 "Could not apply $sha1... $rest"
305 comment_for_reflog edit
309 die_with_patch
$sha1 "Could not apply $sha1... $rest"
311 git rev-parse
--verify HEAD
> "$DOTEST"/amend
312 warn
"Stopped at $sha1... $rest"
313 warn
"You can amend the commit now, with"
315 warn
" git commit --amend"
317 warn
"Once you are satisfied with your changes, run"
319 warn
" git rebase --continue"
324 comment_for_reflog squash
326 has_action
"$DONE" ||
327 die
"Cannot 'squash' without a previous commit"
330 make_squash_message
$sha1 > "$MSG"
332 author_script
=$
(get_author_ident_from_commit HEAD
)
333 output git
reset --soft HEAD^
334 pick_one
-n $sha1 || failed
=t
335 case "$(peek_next_command)" in
341 cp "$MSG" "$SQUASH_MSG"
348 rm -f "$SQUASH_MSG" ||
exit
349 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
350 rm -f "$GIT_DIR"/MERGE_MSG ||
exit
353 echo "$author_script" > "$DOTEST"/author-script
356 # This is like --amend, but with a different message
357 eval "$author_script"
358 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
359 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
360 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
361 $USE_OUTPUT git commit
--no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed
=t
365 cp "$MSG" "$GIT_DIR"/MERGE_MSG
367 warn
"Could not apply $sha1... $rest"
368 die_with_patch
$sha1 ""
372 warn
"Unknown command: $command $sha1 $rest"
373 die_with_patch
$sha1 "Please fix this in the file $TODO."
376 test -s "$TODO" && return
378 comment_for_reflog finish
&&
379 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
380 OLDHEAD
=$
(cat "$DOTEST"/head) &&
381 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
382 if test -d "$REWRITTEN"
384 test -f "$DOTEST"/current-commit
&&
385 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
386 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
387 if test -f "$REWRITTEN"/$OLDHEAD
389 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
394 NEWHEAD
=$
(git rev-parse HEAD
)
398 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
399 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
400 git symbolic-ref HEAD
$HEADNAME
403 test ! -f "$DOTEST"/verbose ||
404 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
408 warn
"Successfully rebased and updated $HEADNAME."
420 # check if no other options are set
422 test $# -eq 2 -a "$2" = '--' &&
424 test -z "$PRESERVE_MERGES" &&
425 test -z "$STRATEGY" &&
429 get_saved_options
() {
430 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
431 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
432 test -f "$DOTEST"/verbose
&& VERBOSE
=t
439 OK_TO_SKIP_PRE_REBASE
=yes
444 is_standalone
"$@" || usage
446 comment_for_reflog
continue
448 test -d "$DOTEST" || die
"No interactive rebase running"
451 git rev-parse
--verify HEAD
>/dev
/null ||
452 die
"Cannot read HEAD"
453 git update-index
--ignore-submodules --refresh &&
454 git diff-files
--quiet --ignore-submodules ||
455 die
"Working tree is dirty"
457 # do we have anything to commit?
458 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
460 : Nothing to commit
-- skip this
462 .
"$DOTEST"/author-script ||
463 die
"Cannot find the author identity"
465 if test -f "$DOTEST"/amend
467 amend
=$
(git rev-parse
--verify HEAD
)
468 test "$amend" = $
(cat "$DOTEST"/amend
) ||
470 You have uncommitted changes in your working tree. Please, commit them
471 first and then run 'git rebase --continue' again."
472 git
reset --soft HEAD^ ||
473 die
"Cannot rewind the HEAD"
475 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
&&
476 git commit
--no-verify -F "$DOTEST"/message
-e ||
{
477 test -n "$amend" && git
reset --soft $amend
478 die
"Could not commit staged changes."
482 require_clean_work_tree
486 is_standalone
"$@" || usage
488 comment_for_reflog abort
491 test -d "$DOTEST" || die
"No interactive rebase running"
493 HEADNAME
=$
(cat "$DOTEST"/head-name
)
494 HEAD
=$
(cat "$DOTEST"/head)
497 git symbolic-ref HEAD
$HEADNAME
500 output git
reset --hard $HEAD &&
505 is_standalone
"$@" || usage
507 comment_for_reflog skip
510 test -d "$DOTEST" || die
"No interactive rebase running"
512 output git
reset --hard && do_rest
517 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
526 # we use merge anyway
539 ONTO
=$
(git rev-parse
--verify "$1") ||
540 die
"Does not point to a valid commit: $1"
544 run_pre_rebase_hook
${1+"$@"}
545 test $# -eq 1 -o $# -eq 2 || usage
547 die
"Interactive rebase already started"
549 git var GIT_COMMITTER_IDENT
>/dev
/null ||
550 die
"You need to set your committer info first"
552 comment_for_reflog start
554 require_clean_work_tree
556 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
557 test -z "$ONTO" && ONTO
=$UPSTREAM
561 output git show-ref
--verify --quiet "refs/heads/$2" ||
562 die
"Invalid branchname: $2"
563 output git checkout
"$2" ||
564 die
"Could not checkout $2"
567 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
568 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
570 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
571 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
572 echo "detached HEAD" > "$DOTEST"/head-name
574 echo $HEAD > "$DOTEST"/head
575 echo $UPSTREAM > "$DOTEST"/upstream
576 echo $ONTO > "$DOTEST"/onto
577 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
578 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
579 if test t
= "$PRESERVE_MERGES"
581 # $REWRITTEN contains files for each commit that is
582 # reachable by at least one merge base of $HEAD and
583 # $UPSTREAM. They are not necessarily rewritten, but
584 # their children might be.
585 # This ensures that commits on merged, but otherwise
586 # unrelated side branches are left alone. (Think "X"
587 # in the man page's example.)
588 mkdir
"$REWRITTEN" &&
589 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
591 echo $ONTO > "$REWRITTEN"/$c ||
592 die
"Could not init rewritten commits"
596 MERGES_OPTION
=--no-merges
599 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
600 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
601 SHORTONTO
=$
(git rev-parse
--short $ONTO)
602 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
603 --abbrev=7 --reverse --left-right --cherry-pick \
604 $UPSTREAM...
$HEAD | \
605 sed -n "s/^>/pick /p" > "$TODO"
606 test -s "$TODO" ||
echo noop
>> "$TODO"
607 cat >> "$TODO" << EOF
609 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
612 # p, pick = use commit
613 # e, edit = use commit, but stop for amending
614 # s, squash = use commit, but meld into previous commit
616 # If you remove a line here THAT COMMIT WILL BE LOST.
617 # However, if you remove everything, the rebase will be aborted.
621 # Watch for commits that been dropped by --cherry-pick
622 if test t
= "$PRESERVE_MERGES"
625 # drop the --cherry-pick parameter this time
626 git rev-list
$MERGES_OPTION --abbrev-commit \
627 --abbrev=7 $UPSTREAM...
$HEAD --left-right | \
628 sed -n "s/^>//p" |
while read rev
630 grep --quiet "$rev" "$TODO"
633 # Use -f2 because if rev-list is telling this commit is not
634 # worthwhile, we don't want to track its multiple heads,
635 # just the history of its first-parent for others that will
636 # be rebasing on top of us
637 full
=$
(git rev-parse
$rev)
638 git rev-list
--parents -1 $rev | cut
-d' ' -f2 > "$DROPPED"/$full
643 has_action
"$TODO" ||
644 die_abort
"Nothing to do"
646 cp "$TODO" "$TODO".backup
647 git_editor
"$TODO" ||
648 die
"Could not execute editor"
650 has_action
"$TODO" ||
651 die_abort
"Nothing to do"
653 git update-ref ORIG_HEAD
$HEAD
654 output git checkout
$ONTO && do_rest