チュートリアル3 コミットを書き換えよう! 6. rebase -i でコミットを修正する

このチュートリアルでは、あらかじめ履歴が準備されているローカルリポジトリを使用します。
こちらからダウンロードしてください。

stepup-tutorial/tutorial6ディレクトリに移動します。このリポジトリの履歴は次の図で表す状態になっています。
ここでは、「commitの説明を追加」で行った変更内容を修正します。

rebase -iを使って、まずは修正するコミットを選択します。

$ git rebase -i HEAD~~

テキストエディタが開いて、HEADからHEAD~~までのコミットが次のように表示されます。

pick 05191fc commitの説明を追加
pick 05dc5b2 pullの説明を追加

# Rebase 82f0447..05dc5b2 onto 05191fc
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

1行目のpickの文字をeditに変更して保存・終了します。すると、次のような出力が表示されて、修正するコミットがチェックアウトされた状態になります。

Stopped at 05191fc... commitの説明を追加
You can amend the commit now, with

git commit --amend

Once you are satisfied with your changes, run

git rebase --continue

sample.txtを開いて、commitの説明の所を適当に変更してしてください。

サル先生のGitコマンド
add 変更をインデックスに登録する
commit インデックスの状態を記録する
pull リモートリポジトリの内容を取得する

commit --amendで変更を保存します。

$ git add sample.txt
$ git commit --amend

commitをしただけだとまだrebaseの作業は終わっていません。このコミットでの作業が終了したことを知らせるには、--continueオプションを指定してrebaseを実行します。

$ git rebase --continue
Note
この時、ほかのコミットで競合が発生することがあります。その時は、競合箇所を修正してからaddとrebase --continueを実行してください。このとき、コミットは必要ないので実行しないでください。
もし、途中でrebaseの作業を中止したくなった場合はrebaseに--abortオプションを指定して実行すると、これまでのrebaseでの作業をなかった事にして中止できます。

これで、コミットの修正が完了しました。 もし、複数のコミットをeditに変更していた場合、次に修正するコミットがチェックアウトされるので、今と同じように変更を行います。

Note
実は、rebase前のコミットはORIG_HEADという名前で残っています。もし、rebaseした後で元に戻したくなった場合は git reset --hard ORIG_HEAD でrebase前の状態に戻せます。