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
45 OK_TO_SKIP_PRE_REBASE
=
47 GIT_CHERRY_PICK_HELP
=" After resolving the conflicts,
48 mark the corrected paths with 'git add <paths>', and
49 run 'git rebase --continue'"
50 export GIT_CHERRY_PICK_HELP
61 test $status != 0 && printf "%s\n" "$output"
70 run_pre_rebase_hook
() {
71 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
72 test -x "$GIT_DIR/hooks/pre-rebase"
74 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
{
75 echo >&2 "The pre-rebase hook refused to rebase."
81 require_clean_work_tree
() {
82 # test if working tree is dirty
83 git rev-parse
--verify HEAD
> /dev
/null
&&
84 git update-index
--ignore-submodules --refresh &&
85 git diff-files
--quiet --ignore-submodules &&
86 git diff-index
--cached --quiet HEAD
--ignore-submodules -- ||
87 die
"Working tree is dirty"
90 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
92 comment_for_reflog
() {
93 case "$ORIG_REFLOG_ACTION" in
95 GIT_REFLOG_ACTION
="rebase -i ($1)"
96 export GIT_REFLOG_ACTION
102 mark_action_done
() {
103 sed -e 1q
< "$TODO" >> "$DONE"
104 sed -e 1d
< "$TODO" >> "$TODO".new
105 mv -f "$TODO".new
"$TODO"
106 count
=$
(grep -c '^[^#]' < "$DONE")
107 total
=$
(($count+$
(grep -c '^[^#]' < "$TODO")))
108 if test "$last_count" != "$count"
111 printf "Rebasing (%d/%d)\r" $count $total
112 test -z "$VERBOSE" ||
echo
117 parent_sha1
=$
(git rev-parse
--verify "$1"^
) ||
118 die
"Cannot get patch for $1^"
119 git diff-tree
-p "$parent_sha1"..
"$1" > "$DOTEST"/patch
120 test -f "$DOTEST"/message ||
121 git cat-file commit
"$1" |
sed "1,/^$/d" > "$DOTEST"/message
122 test -f "$DOTEST"/author-script ||
123 get_author_ident_from_commit
"$1" > "$DOTEST"/author-script
138 grep '^[^#]' "$1" >/dev
/null
143 case "$1" in -n) sha1
=$2; no_ff
=t
;; *) sha1
=$1 ;; esac
144 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
145 test -d "$REWRITTEN" &&
146 pick_one_preserving_merges
"$@" && return
147 parent_sha1
=$
(git rev-parse
--verify $sha1^
) ||
148 die
"Could not get the parent of $sha1"
149 current_sha1
=$
(git rev-parse
--verify HEAD
)
150 if test "$no_ff$current_sha1" = "$parent_sha1"; then
151 output git
reset --hard $sha1
152 test "a$1" = a-n
&& output git
reset --soft $current_sha1
153 sha1
=$
(git rev-parse
--short $sha1)
154 output warn Fast forward to
$sha1
156 output git cherry-pick
"$@"
160 pick_one_preserving_merges
() {
171 sha1
=$
(git rev-parse
$sha1)
173 if test -f "$DOTEST"/current-commit
175 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
176 git rev-parse HEAD
> "$REWRITTEN"/$current_commit &&
177 rm "$DOTEST"/current-commit ||
178 die
"Cannot write current commit's replacement sha1"
181 echo $sha1 > "$DOTEST"/current-commit
183 # rewrite parents; if none were rewritten, we can fast-forward.
185 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -f2-)
187 if test -f "$REWRITTEN"/$p
189 new_p
=$
(cat "$REWRITTEN"/$p)
190 test $p != $new_p && fast_forward
=f
191 case "$new_parents" in
193 ;; # do nothing; that parent is already there
195 new_parents
="$new_parents $new_p"
199 new_parents
="$new_parents $p"
202 case $fast_forward in
204 output warn
"Fast forward to $sha1"
205 output git
reset --hard $sha1 ||
206 die
"Cannot fast forward to $sha1"
209 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
211 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
212 # detach HEAD to current parent
213 output git checkout
$first_parent 2> /dev
/null ||
214 die
"Cannot move HEAD to $first_parent"
216 case "$new_parents" in
219 author_script
=$
(get_author_ident_from_commit
$sha1)
220 eval "$author_script"
221 msg
="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
222 # No point in merging the first parent, that's HEAD
223 new_parents
=${new_parents# $first_parent}
224 if ! GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
225 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
226 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
227 output git merge
$STRATEGY -m "$msg" \
231 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
232 die Error redoing merge
$sha1
236 output git cherry-pick
"$@" ||
237 die_with_patch
$sha1 "Could not pick $sha1"
246 *1[0-9]|
*[04-9]) echo "$1"th
;;
253 make_squash_message
() {
254 if test -f "$SQUASH_MSG"; then
255 COUNT
=$
(($
(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
256 < "$SQUASH_MSG" |
sed -ne '$p')+1))
257 echo "# This is a combination of $COUNT commits."
258 sed -e 1d
-e '2,/^./{
263 echo "# This is a combination of two commits."
264 echo "# The first commit's message is:"
266 git cat-file commit HEAD |
sed -e '1,/^$/d'
269 echo "# This is the $(nth_string $COUNT) commit message:"
271 git cat-file commit
$1 |
sed -e '1,/^$/d'
274 peek_next_command
() {
275 sed -n "1s/ .*$//p" < "$TODO"
279 rm -f "$DOTEST"/message
"$DOTEST"/author-script \
280 "$DOTEST"/amend ||
exit
281 read command sha1 rest
< "$TODO"
287 comment_for_reflog pick
291 die_with_patch
$sha1 "Could not apply $sha1... $rest"
294 comment_for_reflog edit
298 die_with_patch
$sha1 "Could not apply $sha1... $rest"
300 git rev-parse
--verify HEAD
> "$DOTEST"/amend
301 warn
"Stopped at $sha1... $rest"
302 warn
"You can amend the commit now, with"
304 warn
" git commit --amend"
306 warn
"Once you are satisfied with your changes, run"
308 warn
" git rebase --continue"
313 comment_for_reflog squash
315 has_action
"$DONE" ||
316 die
"Cannot 'squash' without a previous commit"
319 make_squash_message
$sha1 > "$MSG"
321 author_script
=$
(get_author_ident_from_commit HEAD
)
322 output git
reset --soft HEAD^
323 pick_one
-n $sha1 || failed
=t
324 case "$(peek_next_command)" in
330 cp "$MSG" "$SQUASH_MSG"
337 rm -f "$SQUASH_MSG" ||
exit
338 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
339 rm -f "$GIT_DIR"/MERGE_MSG ||
exit
342 echo "$author_script" > "$DOTEST"/author-script
345 # This is like --amend, but with a different message
346 eval "$author_script"
347 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
348 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
349 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
350 $USE_OUTPUT git commit
--no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed
=t
354 cp "$MSG" "$GIT_DIR"/MERGE_MSG
356 warn
"Could not apply $sha1... $rest"
357 die_with_patch
$sha1 ""
361 warn
"Unknown command: $command $sha1 $rest"
362 die_with_patch
$sha1 "Please fix this in the file $TODO."
365 test -s "$TODO" && return
367 comment_for_reflog finish
&&
368 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
369 OLDHEAD
=$
(cat "$DOTEST"/head) &&
370 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
371 if test -d "$REWRITTEN"
373 test -f "$DOTEST"/current-commit
&&
374 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
375 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
376 if test -f "$REWRITTEN"/$OLDHEAD
378 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
383 NEWHEAD
=$
(git rev-parse HEAD
)
387 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
388 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
389 git symbolic-ref HEAD
$HEADNAME
392 test ! -f "$DOTEST"/verbose ||
393 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
397 warn
"Successfully rebased and updated $HEADNAME."
409 # check if no other options are set
411 test $# -eq 2 -a "$2" = '--' &&
413 test -z "$PRESERVE_MERGES" &&
414 test -z "$STRATEGY" &&
418 get_saved_options
() {
419 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
420 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
421 test -f "$DOTEST"/verbose
&& VERBOSE
=t
428 OK_TO_SKIP_PRE_REBASE
=yes
433 is_standalone
"$@" || usage
435 comment_for_reflog
continue
437 test -d "$DOTEST" || die
"No interactive rebase running"
440 git rev-parse
--verify HEAD
>/dev
/null ||
441 die
"Cannot read HEAD"
442 git update-index
--ignore-submodules --refresh &&
443 git diff-files
--quiet --ignore-submodules ||
444 die
"Working tree is dirty"
446 # do we have anything to commit?
447 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
449 : Nothing to commit
-- skip this
451 .
"$DOTEST"/author-script ||
452 die
"Cannot find the author identity"
454 if test -f "$DOTEST"/amend
456 amend
=$
(git rev-parse
--verify HEAD
)
457 test "$amend" = $
(cat "$DOTEST"/amend
) ||
459 You have uncommitted changes in your working tree. Please, commit them
460 first and then run 'git rebase --continue' again."
461 git
reset --soft HEAD^ ||
462 die
"Cannot rewind the HEAD"
464 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
&&
465 git commit
--no-verify -F "$DOTEST"/message
-e ||
{
466 test -n "$amend" && git
reset --soft $amend
467 die
"Could not commit staged changes."
471 require_clean_work_tree
475 is_standalone
"$@" || usage
477 comment_for_reflog abort
480 test -d "$DOTEST" || die
"No interactive rebase running"
482 HEADNAME
=$
(cat "$DOTEST"/head-name
)
483 HEAD
=$
(cat "$DOTEST"/head)
486 git symbolic-ref HEAD
$HEADNAME
489 output git
reset --hard $HEAD &&
494 is_standalone
"$@" || usage
496 comment_for_reflog skip
499 test -d "$DOTEST" || die
"No interactive rebase running"
501 output git
reset --hard && do_rest
506 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
515 # we use merge anyway
528 ONTO
=$
(git rev-parse
--verify "$1") ||
529 die
"Does not point to a valid commit: $1"
533 run_pre_rebase_hook
${1+"$@"}
534 test $# -eq 1 -o $# -eq 2 || usage
536 die
"Interactive rebase already started"
538 git var GIT_COMMITTER_IDENT
>/dev
/null ||
539 die
"You need to set your committer info first"
541 comment_for_reflog start
543 require_clean_work_tree
545 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
546 test -z "$ONTO" && ONTO
=$UPSTREAM
550 output git show-ref
--verify --quiet "refs/heads/$2" ||
551 die
"Invalid branchname: $2"
552 output git checkout
"$2" ||
553 die
"Could not checkout $2"
556 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
557 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
559 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
560 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
561 echo "detached HEAD" > "$DOTEST"/head-name
563 echo $HEAD > "$DOTEST"/head
564 echo $UPSTREAM > "$DOTEST"/upstream
565 echo $ONTO > "$DOTEST"/onto
566 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
567 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
568 if test t
= "$PRESERVE_MERGES"
570 # $REWRITTEN contains files for each commit that is
571 # reachable by at least one merge base of $HEAD and
572 # $UPSTREAM. They are not necessarily rewritten, but
573 # their children might be.
574 # This ensures that commits on merged, but otherwise
575 # unrelated side branches are left alone. (Think "X"
576 # in the man page's example.)
577 mkdir
"$REWRITTEN" &&
578 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
580 echo $ONTO > "$REWRITTEN"/$c ||
581 die
"Could not init rewritten commits"
585 MERGES_OPTION
=--no-merges
588 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
589 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
590 SHORTONTO
=$
(git rev-parse
--short $ONTO)
591 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
592 --abbrev=7 --reverse --left-right --cherry-pick \
593 $UPSTREAM...
$HEAD | \
594 sed -n "s/^>/pick /p" > "$TODO"
595 test -s "$TODO" ||
echo noop
>> "$TODO"
596 cat >> "$TODO" << EOF
598 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
601 # p, pick = use commit
602 # e, edit = use commit, but stop for amending
603 # s, squash = use commit, but meld into previous commit
605 # If you remove a line here THAT COMMIT WILL BE LOST.
606 # However, if you remove everything, the rebase will be aborted.
610 has_action
"$TODO" ||
611 die_abort
"Nothing to do"
613 cp "$TODO" "$TODO".backup
614 git_editor
"$TODO" ||
615 die
"Could not execute editor"
617 has_action
"$TODO" ||
618 die_abort
"Nothing to do"
620 git update-ref ORIG_HEAD
$HEAD
621 output git checkout
$ONTO && do_rest