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 if test -d "$REWRITTEN"
381 test -f "$DOTEST"/current-commit
&&
382 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
383 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
384 if test -f "$REWRITTEN"/$OLDHEAD
386 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
391 NEWHEAD
=$
(git rev-parse HEAD
)
395 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
396 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
397 git symbolic-ref HEAD
$HEADNAME
400 test ! -f "$DOTEST"/verbose ||
401 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
405 warn
"Successfully rebased and updated $HEADNAME."
417 # check if no other options are set
419 test $# -eq 2 -a "$2" = '--' &&
421 test -z "$PRESERVE_MERGES" &&
422 test -z "$STRATEGY" &&
426 get_saved_options
() {
427 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
428 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
429 test -f "$DOTEST"/verbose
&& VERBOSE
=t
436 is_standalone
"$@" || usage
438 comment_for_reflog
continue
440 test -d "$DOTEST" || die
"No interactive rebase running"
443 git rev-parse
--verify HEAD
>/dev
/null ||
444 die
"Cannot read HEAD"
445 git update-index
--ignore-submodules --refresh &&
446 git diff-files
--quiet --ignore-submodules ||
447 die
"Working tree is dirty"
449 # do we have anything to commit?
450 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
452 : Nothing to commit
-- skip this
454 .
"$DOTEST"/author-script ||
455 die
"Cannot find the author identity"
457 if test -f "$DOTEST"/amend
459 amend
=$
(git rev-parse
--verify HEAD
)
460 test "$amend" = $
(cat "$DOTEST"/amend
) ||
462 You have uncommitted changes in your working tree. Please, commit them
463 first and then run 'git rebase --continue' again."
464 git
reset --soft HEAD^ ||
465 die
"Cannot rewind the HEAD"
467 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
&&
468 git commit
--no-verify -F "$DOTEST"/message
-e ||
{
469 test -n "$amend" && git
reset --soft $amend
470 die
"Could not commit staged changes."
474 require_clean_work_tree
478 is_standalone
"$@" || usage
480 comment_for_reflog abort
483 test -d "$DOTEST" || die
"No interactive rebase running"
485 HEADNAME
=$
(cat "$DOTEST"/head-name
)
486 HEAD
=$
(cat "$DOTEST"/head)
489 git symbolic-ref HEAD
$HEADNAME
492 output git
reset --hard $HEAD &&
497 is_standalone
"$@" || usage
499 comment_for_reflog skip
502 test -d "$DOTEST" || die
"No interactive rebase running"
504 output git
reset --hard && do_rest
509 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
518 # we use merge anyway
531 ONTO
=$
(git rev-parse
--verify "$1") ||
532 die
"Does not point to a valid commit: $1"
536 run_pre_rebase_hook
${1+"$@"}
537 test $# -eq 1 -o $# -eq 2 || usage
539 die
"Interactive rebase already started"
541 git var GIT_COMMITTER_IDENT
>/dev
/null ||
542 die
"You need to set your committer info first"
544 comment_for_reflog start
546 require_clean_work_tree
548 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
549 test -z "$ONTO" && ONTO
=$UPSTREAM
553 output git show-ref
--verify --quiet "refs/heads/$2" ||
554 die
"Invalid branchname: $2"
555 output git checkout
"$2" ||
556 die
"Could not checkout $2"
559 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
560 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
562 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
563 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
564 echo "detached HEAD" > "$DOTEST"/head-name
566 echo $HEAD > "$DOTEST"/head
567 echo $UPSTREAM > "$DOTEST"/upstream
568 echo $ONTO > "$DOTEST"/onto
569 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
570 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
571 if test t
= "$PRESERVE_MERGES"
573 # $REWRITTEN contains files for each commit that is
574 # reachable by at least one merge base of $HEAD and
575 # $UPSTREAM. They are not necessarily rewritten, but
576 # their children might be.
577 # This ensures that commits on merged, but otherwise
578 # unrelated side branches are left alone. (Think "X"
579 # in the man page's example.)
580 mkdir
"$REWRITTEN" &&
581 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
583 echo $ONTO > "$REWRITTEN"/$c ||
584 die
"Could not init rewritten commits"
588 MERGES_OPTION
=--no-merges
591 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
592 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
593 SHORTONTO
=$
(git rev-parse
--short $ONTO)
594 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
595 --abbrev=7 --reverse --left-right --cherry-pick \
596 $UPSTREAM...
$HEAD | \
597 sed -n "s/^>/pick /p" > "$TODO"
598 test -s "$TODO" ||
echo noop
>> "$TODO"
599 cat >> "$TODO" << EOF
601 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
604 # p, pick = use commit
605 # e, edit = use commit, but stop for amending
606 # s, squash = use commit, but meld into previous commit
608 # If you remove a line here THAT COMMIT WILL BE LOST.
609 # However, if you remove everything, the rebase will be aborted.
613 # Watch for commits that been dropped by --cherry-pick
614 if test t
= "$PRESERVE_MERGES"
617 # drop the --cherry-pick parameter this time
618 git rev-list
$MERGES_OPTION --abbrev-commit \
619 --abbrev=7 $UPSTREAM...
$HEAD --left-right | \
620 sed -n "s/^>//p" |
while read rev
622 grep --quiet "$rev" "$TODO"
625 # Use -f2 because if rev-list is telling this commit is not
626 # worthwhile, we don't want to track its multiple heads,
627 # just the history of its first-parent for others that will
628 # be rebasing on top of us
629 full
=$
(git rev-parse
$rev)
630 git rev-list
--parents -1 $rev | cut
-d' ' -f2 > "$DROPPED"/$full
635 has_action
"$TODO" ||
636 die_abort
"Nothing to do"
638 cp "$TODO" "$TODO".backup
639 git_editor
"$TODO" ||
640 die
"Could not execute editor"
642 has_action
"$TODO" ||
643 die_abort
"Nothing to do"
645 git update-ref ORIG_HEAD
$HEAD
646 output git checkout
$ONTO && do_rest