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 require_clean_work_tree
() {
69 # test if working tree is dirty
70 git rev-parse
--verify HEAD
> /dev
/null
&&
71 git update-index
--ignore-submodules --refresh &&
72 git diff-files
--quiet --ignore-submodules &&
73 git diff-index
--cached --quiet HEAD
--ignore-submodules -- ||
74 die
"Working tree is dirty"
77 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
79 comment_for_reflog
() {
80 case "$ORIG_REFLOG_ACTION" in
82 GIT_REFLOG_ACTION
="rebase -i ($1)"
83 export GIT_REFLOG_ACTION
90 sed -e 1q
< "$TODO" >> "$DONE"
91 sed -e 1d
< "$TODO" >> "$TODO".new
92 mv -f "$TODO".new
"$TODO"
93 count
=$
(grep -c '^[^#]' < "$DONE")
94 total
=$
(($count+$
(grep -c '^[^#]' < "$TODO")))
95 if test "$last_count" != "$count"
98 printf "Rebasing (%d/%d)\r" $count $total
99 test -z "$VERBOSE" ||
echo
104 parent_sha1
=$
(git rev-parse
--verify "$1"^
) ||
105 die
"Cannot get patch for $1^"
106 git diff-tree
-p "$parent_sha1"..
"$1" > "$DOTEST"/patch
107 test -f "$DOTEST"/message ||
108 git cat-file commit
"$1" |
sed "1,/^$/d" > "$DOTEST"/message
109 test -f "$DOTEST"/author-script ||
110 get_author_ident_from_commit
"$1" > "$DOTEST"/author-script
125 grep '^[^#]' "$1" >/dev
/null
130 case "$1" in -n) sha1
=$2; no_ff
=t
;; *) sha1
=$1 ;; esac
131 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
132 test -d "$REWRITTEN" &&
133 pick_one_preserving_merges
"$@" && return
134 parent_sha1
=$
(git rev-parse
--verify $sha1^
) ||
135 die
"Could not get the parent of $sha1"
136 current_sha1
=$
(git rev-parse
--verify HEAD
)
137 if test "$no_ff$current_sha1" = "$parent_sha1"; then
138 output git
reset --hard $sha1
139 test "a$1" = a-n
&& output git
reset --soft $current_sha1
140 sha1
=$
(git rev-parse
--short $sha1)
141 output warn Fast forward to
$sha1
143 output git cherry-pick
"$@"
147 pick_one_preserving_merges
() {
158 sha1
=$
(git rev-parse
$sha1)
160 if test -f "$DOTEST"/current-commit
162 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
163 git rev-parse HEAD
> "$REWRITTEN"/$current_commit &&
164 rm "$DOTEST"/current-commit ||
165 die
"Cannot write current commit's replacement sha1"
168 echo $sha1 > "$DOTEST"/current-commit
170 # rewrite parents; if none were rewritten, we can fast-forward.
172 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -f2-)
174 if test -f "$REWRITTEN"/$p
176 new_p
=$
(cat "$REWRITTEN"/$p)
177 test $p != $new_p && fast_forward
=f
178 case "$new_parents" in
180 ;; # do nothing; that parent is already there
182 new_parents
="$new_parents $new_p"
186 new_parents
="$new_parents $p"
189 case $fast_forward in
191 output warn
"Fast forward to $sha1"
192 output git
reset --hard $sha1 ||
193 die
"Cannot fast forward to $sha1"
196 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
198 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
199 # detach HEAD to current parent
200 output git checkout
$first_parent 2> /dev
/null ||
201 die
"Cannot move HEAD to $first_parent"
203 case "$new_parents" in
206 author_script
=$
(get_author_ident_from_commit
$sha1)
207 eval "$author_script"
208 msg
="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
209 # No point in merging the first parent, that's HEAD
210 new_parents
=${new_parents# $first_parent}
211 if ! GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
212 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
213 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
214 output git merge
$STRATEGY -m "$msg" \
218 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
219 die Error redoing merge
$sha1
223 output git cherry-pick
"$@" ||
224 die_with_patch
$sha1 "Could not pick $sha1"
233 *1[0-9]|
*[04-9]) echo "$1"th
;;
240 make_squash_message
() {
241 if test -f "$SQUASH_MSG"; then
242 COUNT
=$
(($
(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
243 < "$SQUASH_MSG" |
sed -ne '$p')+1))
244 echo "# This is a combination of $COUNT commits."
245 sed -e 1d
-e '2,/^./{
250 echo "# This is a combination of two commits."
251 echo "# The first commit's message is:"
253 git cat-file commit HEAD |
sed -e '1,/^$/d'
256 echo "# This is the $(nth_string $COUNT) commit message:"
258 git cat-file commit
$1 |
sed -e '1,/^$/d'
261 peek_next_command
() {
262 sed -n "1s/ .*$//p" < "$TODO"
266 rm -f "$DOTEST"/message
"$DOTEST"/author-script \
267 "$DOTEST"/amend ||
exit
268 read command sha1 rest
< "$TODO"
274 comment_for_reflog pick
278 die_with_patch
$sha1 "Could not apply $sha1... $rest"
281 comment_for_reflog edit
285 die_with_patch
$sha1 "Could not apply $sha1... $rest"
288 warn
"Stopped at $sha1... $rest"
289 warn
"You can amend the commit now, with"
291 warn
" git commit --amend"
293 warn
"Once you are satisfied with your changes, run"
295 warn
" git rebase --continue"
300 comment_for_reflog squash
302 has_action
"$DONE" ||
303 die
"Cannot 'squash' without a previous commit"
306 make_squash_message
$sha1 > "$MSG"
307 case "$(peek_next_command)" in
311 cp "$MSG" "$SQUASH_MSG"
316 rm -f "$SQUASH_MSG" ||
exit
321 author_script
=$
(get_author_ident_from_commit HEAD
)
322 output git
reset --soft HEAD^
323 pick_one
-n $sha1 || failed
=t
324 echo "$author_script" > "$DOTEST"/author-script
327 # This is like --amend, but with a different message
328 eval "$author_script"
329 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
330 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
331 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
332 $USE_OUTPUT git commit
--no-verify -F "$MSG" $EDIT_COMMIT || failed
=t
336 cp "$MSG" "$GIT_DIR"/MERGE_MSG
338 warn
"Could not apply $sha1... $rest"
339 die_with_patch
$sha1 ""
343 warn
"Unknown command: $command $sha1 $rest"
344 die_with_patch
$sha1 "Please fix this in the file $TODO."
347 test -s "$TODO" && return
349 comment_for_reflog finish
&&
350 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
351 OLDHEAD
=$
(cat "$DOTEST"/head) &&
352 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
353 if test -d "$REWRITTEN"
355 test -f "$DOTEST"/current-commit
&&
356 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
357 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
358 if test -f "$REWRITTEN"/$OLDHEAD
360 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
365 NEWHEAD
=$
(git rev-parse HEAD
)
369 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
370 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
371 git symbolic-ref HEAD
$HEADNAME
374 test ! -f "$DOTEST"/verbose ||
375 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
379 warn
"Successfully rebased and updated $HEADNAME."
391 # check if no other options are set
393 test $# -eq 2 -a "$2" = '--' &&
395 test -z "$PRESERVE_MERGES" &&
396 test -z "$STRATEGY" &&
400 get_saved_options
() {
401 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
402 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
403 test -f "$DOTEST"/verbose
&& VERBOSE
=t
410 is_standalone
"$@" || usage
412 comment_for_reflog
continue
414 test -d "$DOTEST" || die
"No interactive rebase running"
417 git rev-parse
--verify HEAD
>/dev
/null ||
418 die
"Cannot read HEAD"
419 git update-index
--ignore-submodules --refresh &&
420 git diff-files
--quiet --ignore-submodules ||
421 die
"Working tree is dirty"
423 # do we have anything to commit?
424 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
426 : Nothing to commit
-- skip this
428 .
"$DOTEST"/author-script ||
429 die
"Cannot find the author identity"
430 if test -f "$DOTEST"/amend
432 git
reset --soft HEAD^ ||
433 die
"Cannot rewind the HEAD"
435 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
&&
436 git commit
--no-verify -F "$DOTEST"/message
-e ||
437 die
"Could not commit staged changes."
440 require_clean_work_tree
444 is_standalone
"$@" || usage
446 comment_for_reflog abort
449 test -d "$DOTEST" || die
"No interactive rebase running"
451 HEADNAME
=$
(cat "$DOTEST"/head-name
)
452 HEAD
=$
(cat "$DOTEST"/head)
455 git symbolic-ref HEAD
$HEADNAME
458 output git
reset --hard $HEAD &&
463 is_standalone
"$@" || usage
465 comment_for_reflog skip
468 test -d "$DOTEST" || die
"No interactive rebase running"
470 output git
reset --hard && do_rest
475 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
484 # we use merge anyway
497 ONTO
=$
(git rev-parse
--verify "$1") ||
498 die
"Does not point to a valid commit: $1"
502 test $# -eq 1 -o $# -eq 2 || usage
504 die
"Interactive rebase already started"
506 git var GIT_COMMITTER_IDENT
>/dev
/null ||
507 die
"You need to set your committer info first"
509 comment_for_reflog start
511 require_clean_work_tree
513 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
514 test -z "$ONTO" && ONTO
=$UPSTREAM
518 output git show-ref
--verify --quiet "refs/heads/$2" ||
519 die
"Invalid branchname: $2"
520 output git checkout
"$2" ||
521 die
"Could not checkout $2"
524 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
525 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
527 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
528 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
529 echo "detached HEAD" > "$DOTEST"/head-name
531 echo $HEAD > "$DOTEST"/head
532 echo $UPSTREAM > "$DOTEST"/upstream
533 echo $ONTO > "$DOTEST"/onto
534 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
535 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
536 if test t
= "$PRESERVE_MERGES"
538 # $REWRITTEN contains files for each commit that is
539 # reachable by at least one merge base of $HEAD and
540 # $UPSTREAM. They are not necessarily rewritten, but
541 # their children might be.
542 # This ensures that commits on merged, but otherwise
543 # unrelated side branches are left alone. (Think "X"
544 # in the man page's example.)
545 mkdir
"$REWRITTEN" &&
546 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
548 echo $ONTO > "$REWRITTEN"/$c ||
549 die
"Could not init rewritten commits"
553 MERGES_OPTION
=--no-merges
556 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
557 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
558 SHORTONTO
=$
(git rev-parse
--short $ONTO)
559 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
560 --abbrev=7 --reverse --left-right --cherry-pick \
561 $UPSTREAM...
$HEAD | \
562 sed -n "s/^>/pick /p" > "$TODO"
563 cat >> "$TODO" << EOF
565 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
568 # p, pick = use commit
569 # e, edit = use commit, but stop for amending
570 # s, squash = use commit, but meld into previous commit
572 # If you remove a line here THAT COMMIT WILL BE LOST.
573 # However, if you remove everything, the rebase will be aborted.
577 has_action
"$TODO" ||
578 die_abort
"Nothing to do"
580 cp "$TODO" "$TODO".backup
581 git_editor
"$TODO" ||
582 die
"Could not execute editor"
584 has_action
"$TODO" ||
585 die_abort
"Nothing to do"
587 git update-ref ORIG_HEAD
$HEAD
588 output git checkout
$ONTO && do_rest