1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

git リベースで履歴を書き換える

Posted at

コミットをきれいに整えてからpushしたい時は履歴を書き換えよう。

*GitHubにPushしていないコミット

直前のコミットをやり直す
git commit --amend

リモートリポジトリにPushしたコミットはやり直したらダメだよ。

複数のコミットをやり直す
git rebase -i <コミットID>
git rebase -i HEAD~3

pick gh21f6d ヘッダー修正
pick 193054e ファイル追加
pick 84gha0d README修正

-iは--interactiveの略だよ。
対話的リベースといって、やり取りしながら履歴を変更していくよ。

やり直したいcommitをeditにする

edit gh21f6d ヘッダー修正
pick 193054e ファイル追加
pick 84gha0d README修正

やり直したら実行する

git commit --amend

次のコミットへ進む(リベース完了)

git reabase --continue

HEAD~
1番目の親を指定する。
HEADを基点にして数値分の親コミットまで指定する。

HEAD^
マージした場合の2番目の親を指定する。

rebase -i コマンドの一連の流れ

①git rebase -i コマンドで対話的リベースモードに入る

②修正したいコミットをeditにしてコミットエディタを終了する

③editのコミットのところでコミットの適用が止まる

④git commit --amendコマンドで修正

⑤git rebase --continueで次のコミットへ行く

⑥pickでとそのままのコミット内容を適用して次へ行く

コミットを並び替える、削除

# コミットを並び替える、削除
①8e4ghaOdのコミットを消す
②193054eを先に適用する
pick 193054e ファイル追加
pick gh21f6d ヘッダー修正

コミットをまとめる

# コミットを1つにまとめる
pick gh21f6d ヘッダー修正
squash 193054e ファイル追加
squash 84ghaOd README修正

コミットを分割する

# コミットを分割する
pick gh21f6d ヘッダー修正
pick 193054e ファイル追加
edit 84ghaOd READMEとindex修正
git reset HEAD^
git add README
git commit -m 'README修正'
git add index.html
git commit -m 'index.html修正'
git rebase --continue
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?