S-JIS[2018-01-13] 変更履歴

Git rebase

Gitのrebaseコマンドのメモ。


概要

過去のコミットを修正する(履歴を変更する)のがrebaseコマンド。

他の人と共有するリポジトリーを使っている場合は、rebaseコマンドは使わない方が良い。
rebaseすると履歴が変わるが、履歴が変わると共有リポジトリーに対してpushしたりpullしたりしたときにエラーになるから。


過去のコミットへの追加

ある修正Aをコミットをし、さらに別の修正Bをコミットした後で、Aの修正漏れに気付き、Aに修正を追加したい。

  1. 追加の修正をコミットする。
    $ git add .
    $ git commit -m'temp'	←適当なメッセージでコミット
  2. 修正Aのコミットのハッシュ値を確認する。
    $ git log
    commit 221d131cd6b4d3233670f8adbb5ff7932be4dad8 (HEAD -> master)
    Author: hishidama
    Date:   Sat Jan 13 11:37:26 2018 +0900
    
        temp
    
    commit 001bc6cdeeeff26ecf2404b995d0c28ce71a2a0e
    Author: hishidama
    Date:   Sat Jan 13 11:36:54 2018 +0900
    
        bbb
    
    commit 357d521b0249d1f3abe5ea4dc94acb427d5e2b14	←これ
    Author: hishidama
    Date:   Sat Jan 13 11:36:26 2018 +0900
    
        aaa
    
  3. 確認したハッシュ値を指定してrebaseコマンドを実行する。
    (ハッシュ値の末尾に付けている「^」は、指定したコミットの「直前」を意味する。
     つまり、git logで直前のコミットのハッシュ値を確認し、それを(「^」とか付けずに)指定してもよい)
    $ git rebase -i 357d521b0249d1f3abe5ea4dc94acb427d5e2b14^
    すると、エディターで以下のようなメッセージが表示される。
    pick 357d521 aaa
    pick 001bc6c bbb
    pick 221d131 temp
    
    # Rebase e6c75c8..221d131 onto e6c75c8 (3 commands)
    #
    # 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
    # d, drop = remove commit
    〜
  4. tempをaaaにマージしたいので、tempの行をaaaの下に移動させ、tempの「pick」を「s」(squash)に変更する。
    (pickはコミットを残す、squashは直前のコミットに追加する という意味)
    pick 357d521 aaa
    s 221d131 temp
    pick 001bc6c bbb
  5. エディターを(編集内容を保存して)終了する。
    1. rebase(再コミット)が実行される。(コンフリクトとか起きたら解決する必要はある^^;)
    2. コミットメッセージを編集しろという画面が表示される。
      # This is a combination of 2 commits.
      # This is the 1st commit message:
      
      aaa
      
      # This is the commit message #2:
      
      temp
    3. tempの方のメッセージは不要なので削除する。(以下のようにする)
      # This is a combination of 2 commits.
      # This is the 1st commit message:
      
      aaa
    4. エディターを(編集内容を保存して)終了すると、続きが実行される。

Git目次へ戻る / 技術メモへ戻る
メールの送信先:ひしだま