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
45 GIT_CHERRY_PICK_HELP
=" After resolving the conflicts,
46 mark the corrected paths with 'git add <paths>', and
47 run 'git rebase --continue'"
48 export GIT_CHERRY_PICK_HELP
59 test $status != 0 && printf "%s\n" "$output"
68 run_pre_rebase_hook
() {
69 if test -x "$GIT_DIR/hooks/pre-rebase"
71 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
{
72 echo >&2 "The pre-rebase hook refused to rebase."
78 require_clean_work_tree
() {
79 # test if working tree is dirty
80 git rev-parse
--verify HEAD
> /dev
/null
&&
81 git update-index
--ignore-submodules --refresh &&
82 git diff-files
--quiet --ignore-submodules &&
83 git diff-index
--cached --quiet HEAD
--ignore-submodules -- ||
84 die
"Working tree is dirty"
87 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
89 comment_for_reflog
() {
90 case "$ORIG_REFLOG_ACTION" in
92 GIT_REFLOG_ACTION
="rebase -i ($1)"
93 export GIT_REFLOG_ACTION
100 sed -e 1q
< "$TODO" >> "$DONE"
101 sed -e 1d
< "$TODO" >> "$TODO".new
102 mv -f "$TODO".new
"$TODO"
103 count
=$
(grep -c '^[^#]' < "$DONE")
104 total
=$
(($count+$
(grep -c '^[^#]' < "$TODO")))
105 if test "$last_count" != "$count"
108 printf "Rebasing (%d/%d)\r" $count $total
109 test -z "$VERBOSE" ||
echo
114 parent_sha1
=$
(git rev-parse
--verify "$1"^
) ||
115 die
"Cannot get patch for $1^"
116 git diff-tree
-p "$parent_sha1"..
"$1" > "$DOTEST"/patch
117 test -f "$DOTEST"/message ||
118 git cat-file commit
"$1" |
sed "1,/^$/d" > "$DOTEST"/message
119 test -f "$DOTEST"/author-script ||
120 get_author_ident_from_commit
"$1" > "$DOTEST"/author-script
135 grep '^[^#]' "$1" >/dev
/null
140 case "$1" in -n) sha1
=$2; no_ff
=t
;; *) sha1
=$1 ;; esac
141 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
142 test -d "$REWRITTEN" &&
143 pick_one_preserving_merges
"$@" && return
144 parent_sha1
=$
(git rev-parse
--verify $sha1^
) ||
145 die
"Could not get the parent of $sha1"
146 current_sha1
=$
(git rev-parse
--verify HEAD
)
147 if test "$no_ff$current_sha1" = "$parent_sha1"; then
148 output git
reset --hard $sha1
149 test "a$1" = a-n
&& output git
reset --soft $current_sha1
150 sha1
=$
(git rev-parse
--short $sha1)
151 output warn Fast forward to
$sha1
153 output git cherry-pick
"$@"
157 pick_one_preserving_merges
() {
168 sha1
=$
(git rev-parse
$sha1)
170 if test -f "$DOTEST"/current-commit
172 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
173 git rev-parse HEAD
> "$REWRITTEN"/$current_commit &&
174 rm "$DOTEST"/current-commit ||
175 die
"Cannot write current commit's replacement sha1"
178 echo $sha1 > "$DOTEST"/current-commit
180 # rewrite parents; if none were rewritten, we can fast-forward.
182 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -f2-)
184 if test -f "$REWRITTEN"/$p
186 new_p
=$
(cat "$REWRITTEN"/$p)
187 test $p != $new_p && fast_forward
=f
188 case "$new_parents" in
190 ;; # do nothing; that parent is already there
192 new_parents
="$new_parents $new_p"
196 new_parents
="$new_parents $p"
199 case $fast_forward in
201 output warn
"Fast forward to $sha1"
202 output git
reset --hard $sha1 ||
203 die
"Cannot fast forward to $sha1"
206 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
208 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
209 # detach HEAD to current parent
210 output git checkout
$first_parent 2> /dev
/null ||
211 die
"Cannot move HEAD to $first_parent"
213 case "$new_parents" in
216 author_script
=$
(get_author_ident_from_commit
$sha1)
217 eval "$author_script"
218 msg
="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
219 # No point in merging the first parent, that's HEAD
220 new_parents
=${new_parents# $first_parent}
221 if ! GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
222 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
223 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
224 output git merge
$STRATEGY -m "$msg" \
228 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
229 die Error redoing merge
$sha1
233 output git cherry-pick
"$@" ||
234 die_with_patch
$sha1 "Could not pick $sha1"
243 *1[0-9]|
*[04-9]) echo "$1"th
;;
250 make_squash_message
() {
251 if test -f "$SQUASH_MSG"; then
252 COUNT
=$
(($
(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
253 < "$SQUASH_MSG" |
sed -ne '$p')+1))
254 echo "# This is a combination of $COUNT commits."
255 sed -e 1d
-e '2,/^./{
260 echo "# This is a combination of two commits."
261 echo "# The first commit's message is:"
263 git cat-file commit HEAD |
sed -e '1,/^$/d'
266 echo "# This is the $(nth_string $COUNT) commit message:"
268 git cat-file commit
$1 |
sed -e '1,/^$/d'
271 peek_next_command
() {
272 sed -n "1s/ .*$//p" < "$TODO"
276 rm -f "$DOTEST"/message
"$DOTEST"/author-script \
277 "$DOTEST"/amend ||
exit
278 read command sha1 rest
< "$TODO"
284 comment_for_reflog pick
288 die_with_patch
$sha1 "Could not apply $sha1... $rest"
291 comment_for_reflog edit
295 die_with_patch
$sha1 "Could not apply $sha1... $rest"
297 git rev-parse
--verify HEAD
> "$DOTEST"/amend
298 warn
"Stopped at $sha1... $rest"
299 warn
"You can amend the commit now, with"
301 warn
" git commit --amend"
303 warn
"Once you are satisfied with your changes, run"
305 warn
" git rebase --continue"
310 comment_for_reflog squash
312 test -f "$DONE" && has_action
"$DONE" ||
313 die
"Cannot 'squash' without a previous commit"
316 make_squash_message
$sha1 > "$MSG"
318 author_script
=$
(get_author_ident_from_commit HEAD
)
319 output git
reset --soft HEAD^
320 pick_one
-n $sha1 || failed
=t
321 case "$(peek_next_command)" in
327 cp "$MSG" "$SQUASH_MSG"
334 rm -f "$SQUASH_MSG" ||
exit
335 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
336 rm -f "$GIT_DIR"/MERGE_MSG ||
exit
339 echo "$author_script" > "$DOTEST"/author-script
342 # This is like --amend, but with a different message
343 eval "$author_script"
344 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
345 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
346 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
347 $USE_OUTPUT git commit
--no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed
=t
351 cp "$MSG" "$GIT_DIR"/MERGE_MSG
353 warn
"Could not apply $sha1... $rest"
354 die_with_patch
$sha1 ""
358 warn
"Unknown command: $command $sha1 $rest"
359 die_with_patch
$sha1 "Please fix this in the file $TODO."
362 test -s "$TODO" && return
364 comment_for_reflog finish
&&
365 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
366 OLDHEAD
=$
(cat "$DOTEST"/head) &&
367 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
368 if test -d "$REWRITTEN"
370 test -f "$DOTEST"/current-commit
&&
371 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
372 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
373 if test -f "$REWRITTEN"/$OLDHEAD
375 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
380 NEWHEAD
=$
(git rev-parse HEAD
)
384 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
385 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
386 git symbolic-ref HEAD
$HEADNAME
389 test ! -f "$DOTEST"/verbose ||
390 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
394 warn
"Successfully rebased and updated $HEADNAME."
406 # check if no other options are set
408 test $# -eq 2 -a "$2" = '--' &&
410 test -z "$PRESERVE_MERGES" &&
411 test -z "$STRATEGY" &&
415 get_saved_options
() {
416 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
417 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
418 test -f "$DOTEST"/verbose
&& VERBOSE
=t
425 is_standalone
"$@" || usage
427 comment_for_reflog
continue
429 test -d "$DOTEST" || die
"No interactive rebase running"
432 git rev-parse
--verify HEAD
>/dev
/null ||
433 die
"Cannot read HEAD"
434 git update-index
--ignore-submodules --refresh &&
435 git diff-files
--quiet --ignore-submodules ||
436 die
"Working tree is dirty"
438 # do we have anything to commit?
439 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
441 : Nothing to commit
-- skip this
443 .
"$DOTEST"/author-script ||
444 die
"Cannot find the author identity"
446 if test -f "$DOTEST"/amend
448 amend
=$
(git rev-parse
--verify HEAD
)
449 test "$amend" = $
(cat "$DOTEST"/amend
) ||
451 You have uncommitted changes in your working tree. Please, commit them
452 first and then run 'git rebase --continue' again."
453 git
reset --soft HEAD^ ||
454 die
"Cannot rewind the HEAD"
456 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
&&
457 git commit
--no-verify -F "$DOTEST"/message
-e ||
{
458 test -n "$amend" && git
reset --soft $amend
459 die
"Could not commit staged changes."
463 require_clean_work_tree
467 is_standalone
"$@" || usage
469 comment_for_reflog abort
472 test -d "$DOTEST" || die
"No interactive rebase running"
474 HEADNAME
=$
(cat "$DOTEST"/head-name
)
475 HEAD
=$
(cat "$DOTEST"/head)
478 git symbolic-ref HEAD
$HEADNAME
481 output git
reset --hard $HEAD &&
486 is_standalone
"$@" || usage
488 comment_for_reflog skip
491 test -d "$DOTEST" || die
"No interactive rebase running"
493 output git
reset --hard && do_rest
498 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
507 # we use merge anyway
520 ONTO
=$
(git rev-parse
--verify "$1") ||
521 die
"Does not point to a valid commit: $1"
525 run_pre_rebase_hook
${1+"$@"}
526 test $# -eq 1 -o $# -eq 2 || usage
528 die
"Interactive rebase already started"
530 git var GIT_COMMITTER_IDENT
>/dev
/null ||
531 die
"You need to set your committer info first"
533 comment_for_reflog start
535 require_clean_work_tree
537 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
538 test -z "$ONTO" && ONTO
=$UPSTREAM
542 output git show-ref
--verify --quiet "refs/heads/$2" ||
543 die
"Invalid branchname: $2"
544 output git checkout
"$2" ||
545 die
"Could not checkout $2"
548 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
549 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
551 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
552 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
553 echo "detached HEAD" > "$DOTEST"/head-name
555 echo $HEAD > "$DOTEST"/head
556 echo $UPSTREAM > "$DOTEST"/upstream
557 echo $ONTO > "$DOTEST"/onto
558 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
559 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
560 if test t
= "$PRESERVE_MERGES"
562 # $REWRITTEN contains files for each commit that is
563 # reachable by at least one merge base of $HEAD and
564 # $UPSTREAM. They are not necessarily rewritten, but
565 # their children might be.
566 # This ensures that commits on merged, but otherwise
567 # unrelated side branches are left alone. (Think "X"
568 # in the man page's example.)
569 mkdir
"$REWRITTEN" &&
570 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
572 echo $ONTO > "$REWRITTEN"/$c ||
573 die
"Could not init rewritten commits"
577 MERGES_OPTION
=--no-merges
580 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
581 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
582 SHORTONTO
=$
(git rev-parse
--short $ONTO)
583 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
584 --abbrev=7 --reverse --left-right --cherry-pick \
585 $UPSTREAM...
$HEAD | \
586 sed -n "s/^>/pick /p" > "$TODO"
587 test -s "$TODO" ||
echo noop
>> "$TODO"
588 cat >> "$TODO" << EOF
590 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
593 # p, pick = use commit
594 # e, edit = use commit, but stop for amending
595 # s, squash = use commit, but meld into previous commit
597 # If you remove a line here THAT COMMIT WILL BE LOST.
598 # However, if you remove everything, the rebase will be aborted.
602 has_action
"$TODO" ||
603 die_abort
"Nothing to do"
605 cp "$TODO" "$TODO".backup
606 git_editor
"$TODO" ||
607 die
"Could not execute editor"
609 has_action
"$TODO" ||
610 die_abort
"Nothing to do"
612 git update-ref ORIG_HEAD
$HEAD
613 output git checkout
$ONTO && do_rest