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
() {
148 case "$1" in -n) sha1
=$2 ;; *) sha1
=$1 ;; esac
149 sha1
=$
(git rev-parse
$sha1)
151 if test -f "$DOTEST"/current-commit
153 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
154 git rev-parse HEAD
> "$REWRITTEN"/$current_commit &&
155 rm "$DOTEST"/current-commit ||
156 die
"Cannot write current commit's replacement sha1"
159 # rewrite parents; if none were rewritten, we can fast-forward.
163 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -f2-)
165 if test -f "$REWRITTEN"/$p
168 new_p
=$
(cat "$REWRITTEN"/$p)
169 test $p != $new_p && fast_forward
=f
170 case "$new_parents" in
172 ;; # do nothing; that parent is already there
174 new_parents
="$new_parents $new_p"
178 new_parents
="$new_parents $p"
181 case $fast_forward in
183 output warn
"Fast forward to $sha1"
184 test $preserve = f ||
echo $sha1 > "$REWRITTEN"/$sha1
187 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
189 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
190 # detach HEAD to current parent
191 output git checkout
$first_parent 2> /dev
/null ||
192 die
"Cannot move HEAD to $first_parent"
194 echo $sha1 > "$DOTEST"/current-commit
195 case "$new_parents" in
198 author_script
=$
(get_author_ident_from_commit
$sha1)
199 eval "$author_script"
200 msg
="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
201 # No point in merging the first parent, that's HEAD
202 new_parents
=${new_parents# $first_parent}
203 if ! GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
204 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
205 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
206 output git merge
$STRATEGY -m "$msg" \
210 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
211 die Error redoing merge
$sha1
215 output git cherry-pick
"$@" ||
216 die_with_patch
$sha1 "Could not pick $sha1"
225 *1[0-9]|
*[04-9]) echo "$1"th
;;
232 make_squash_message
() {
233 if test -f "$SQUASH_MSG"; then
234 COUNT
=$
(($
(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
235 < "$SQUASH_MSG" |
sed -ne '$p')+1))
236 echo "# This is a combination of $COUNT commits."
237 sed -e 1d
-e '2,/^./{
242 echo "# This is a combination of two commits."
243 echo "# The first commit's message is:"
245 git cat-file commit HEAD |
sed -e '1,/^$/d'
248 echo "# This is the $(nth_string $COUNT) commit message:"
250 git cat-file commit
$1 |
sed -e '1,/^$/d'
253 peek_next_command
() {
254 sed -n "1s/ .*$//p" < "$TODO"
258 rm -f "$DOTEST"/message
"$DOTEST"/author-script \
259 "$DOTEST"/amend ||
exit
260 read command sha1 rest
< "$TODO"
266 comment_for_reflog pick
270 die_with_patch
$sha1 "Could not apply $sha1... $rest"
273 comment_for_reflog edit
277 die_with_patch
$sha1 "Could not apply $sha1... $rest"
280 warn
"Stopped at $sha1... $rest"
281 warn
"You can amend the commit now, with"
283 warn
" git commit --amend"
285 warn
"Once you are satisfied with your changes, run"
287 warn
" git rebase --continue"
292 comment_for_reflog squash
294 has_action
"$DONE" ||
295 die
"Cannot 'squash' without a previous commit"
298 make_squash_message
$sha1 > "$MSG"
299 case "$(peek_next_command)" in
303 cp "$MSG" "$SQUASH_MSG"
308 rm -f "$SQUASH_MSG" ||
exit
313 author_script
=$
(get_author_ident_from_commit HEAD
)
314 output git
reset --soft HEAD^
315 pick_one
-n $sha1 || failed
=t
316 echo "$author_script" > "$DOTEST"/author-script
319 # This is like --amend, but with a different message
320 eval "$author_script"
321 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
322 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
323 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
324 $USE_OUTPUT git commit
--no-verify -F "$MSG" $EDIT_COMMIT || failed
=t
328 cp "$MSG" "$GIT_DIR"/MERGE_MSG
330 warn
"Could not apply $sha1... $rest"
331 die_with_patch
$sha1 ""
335 warn
"Unknown command: $command $sha1 $rest"
336 die_with_patch
$sha1 "Please fix this in the file $TODO."
339 test -s "$TODO" && return
341 comment_for_reflog finish
&&
342 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
343 OLDHEAD
=$
(cat "$DOTEST"/head) &&
344 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
345 if test -d "$REWRITTEN"
347 test -f "$DOTEST"/current-commit
&&
348 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
349 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
350 if test -f "$REWRITTEN"/$OLDHEAD
352 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
357 NEWHEAD
=$
(git rev-parse HEAD
)
361 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
362 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
363 git symbolic-ref HEAD
$HEADNAME
366 test ! -f "$DOTEST"/verbose ||
367 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
371 warn
"Successfully rebased and updated $HEADNAME."
383 # check if no other options are set
385 test $# -eq 2 -a "$2" = '--' &&
387 test -z "$PRESERVE_MERGES" &&
388 test -z "$STRATEGY" &&
392 get_saved_options
() {
393 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
394 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
395 test -f "$DOTEST"/verbose
&& VERBOSE
=t
402 is_standalone
"$@" || usage
404 comment_for_reflog
continue
406 test -d "$DOTEST" || die
"No interactive rebase running"
409 git rev-parse
--verify HEAD
>/dev
/null ||
410 die
"Cannot read HEAD"
411 git update-index
--ignore-submodules --refresh &&
412 git diff-files
--quiet --ignore-submodules ||
413 die
"Working tree is dirty"
415 # do we have anything to commit?
416 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
418 : Nothing to commit
-- skip this
420 .
"$DOTEST"/author-script ||
421 die
"Cannot find the author identity"
422 if test -f "$DOTEST"/amend
424 git
reset --soft HEAD^ ||
425 die
"Cannot rewind the HEAD"
427 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
&&
428 git commit
--no-verify -F "$DOTEST"/message
-e ||
429 die
"Could not commit staged changes."
432 require_clean_work_tree
436 is_standalone
"$@" || usage
438 comment_for_reflog abort
441 test -d "$DOTEST" || die
"No interactive rebase running"
443 HEADNAME
=$
(cat "$DOTEST"/head-name
)
444 HEAD
=$
(cat "$DOTEST"/head)
447 git symbolic-ref HEAD
$HEADNAME
450 output git
reset --hard $HEAD &&
455 is_standalone
"$@" || usage
457 comment_for_reflog skip
460 test -d "$DOTEST" || die
"No interactive rebase running"
462 output git
reset --hard && do_rest
467 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
476 # we use merge anyway
489 ONTO
=$
(git rev-parse
--verify "$1") ||
490 die
"Does not point to a valid commit: $1"
494 test $# -eq 1 -o $# -eq 2 || usage
496 die
"Interactive rebase already started"
498 git var GIT_COMMITTER_IDENT
>/dev
/null ||
499 die
"You need to set your committer info first"
501 comment_for_reflog start
503 require_clean_work_tree
505 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
506 test -z "$ONTO" && ONTO
=$UPSTREAM
510 output git show-ref
--verify --quiet "refs/heads/$2" ||
511 die
"Invalid branchname: $2"
512 output git checkout
"$2" ||
513 die
"Could not checkout $2"
516 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
517 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
519 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
520 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
521 echo "detached HEAD" > "$DOTEST"/head-name
523 echo $HEAD > "$DOTEST"/head
524 echo $UPSTREAM > "$DOTEST"/upstream
525 echo $ONTO > "$DOTEST"/onto
526 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
527 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
528 if test t
= "$PRESERVE_MERGES"
530 # $REWRITTEN contains files for each commit that is
531 # reachable by at least one merge base of $HEAD and
532 # $UPSTREAM. They are not necessarily rewritten, but
533 # their children might be.
534 # This ensures that commits on merged, but otherwise
535 # unrelated side branches are left alone. (Think "X"
536 # in the man page's example.)
537 mkdir
"$REWRITTEN" &&
538 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
540 echo $ONTO > "$REWRITTEN"/$c ||
541 die
"Could not init rewritten commits"
545 MERGES_OPTION
=--no-merges
548 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
549 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
550 SHORTONTO
=$
(git rev-parse
--short $ONTO)
551 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
552 --abbrev=7 --reverse --left-right --cherry-pick \
553 $UPSTREAM...
$HEAD | \
554 sed -n "s/^>/pick /p" > "$TODO"
555 cat >> "$TODO" << EOF
557 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
560 # p, pick = use commit
561 # e, edit = use commit, but stop for amending
562 # s, squash = use commit, but meld into previous commit
564 # If you remove a line here THAT COMMIT WILL BE LOST.
565 # However, if you remove everything, the rebase will be aborted.
569 has_action
"$TODO" ||
570 die_abort
"Nothing to do"
572 cp "$TODO" "$TODO".backup
573 git_editor
"$TODO" ||
574 die
"Could not execute editor"
576 has_action
"$TODO" ||
577 die_abort
"Nothing to do"
579 git update-ref ORIG_HEAD
$HEAD
580 output git checkout
$ONTO && do_rest