LoginSignup
52
51

More than 5 years have passed since last update.

git revertでマージコミットの取り消しを行う

Posted at

はじめに

git revertでマージコミットの取り消しを実施した場合に、履歴が
どうなるかを確認しました。

ここでは分岐元ブランチをmaster、分岐先ブランチをtopicとします。

マージコミットの取り消し

masterブランチでファイルを作成

$ touch master_file1                             
$ git add master_file1 
$ git commit -m "add master_file1"

topicブランチにcheckout

$ git checkout -b topic
$ git branch -l                                  
  master
* topic

topicブランチでいくつかcommitする

$ touch topic_file1
$ git add topic_file1                            
$ git commit -m "add topci_file1"

$ touch topic_file2
$ git add topic_file2
$ git commit -m "add topci_file2"

$ touch topic_file3
$ git add topic_file3
$ git commit -m "add topci_file3"

masterブランチにcheckoutし、一回コミットする

$ git checkout master
$ git branch -l
* master
  topic

$ touch master_file2
$ git add master_file2
$ git commit -m "add master_file2"

topicブランチをマージする

$ git merge topic
Merge made by recursive.
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 topic_file1
 create mode 100644 topic_file2
 create mode 100644 topic_file3

ログを確認する

$ git log --graph
*   commit ecc627c4ec21d56880c43d70ed9c651fc27bb2ac
|\  Merge: 7611117 8998d77
| | Author: spam ham
| | Date:   Fri Apr 25 19:09:41 2014 +0900
| | 
| |     Merge branch 'topic'
| |   
| * commit 8998d77d981f1410201b44e83c3640922bd8941f
| | Author: spam ham
| | Date:   Fri Apr 25 18:42:45 2014 +0900
| | 
| |     add topic_file3
| |   
| * commit cc62d621e45b462469c2b42c932e437ff1bf2fd1
| | Author: spam ham
| | Date:   Fri Apr 25 18:42:23 2014 +0900
| | 
| |     add topic_file2
| |   
| * commit b91fa995c57a94a7a12d70fed70be73aa089dd2c
| | Author: spam ham
| | Date:   Fri Apr 25 18:41:56 2014 +0900
| | 
| |     add topic_file1
| |   
* | commit 761111709af04a7e736d6ca6a1ac23ccaac94781
|/  Author: spam ham
|   Date:   Fri Apr 25 19:08:25 2014 +0900
|   
|       add master_file2
|  
* commit 02c3bd6497e4d9e43117e9802b86af204f063657
  Author: spam ham
  Date:   Fri Apr 25 18:37:02 2014 +0900

      add master_file1

マージコミットを取り消す

マージコミットをrevertする場合は、2つのブランチのうちどちらを
残すのか指定します。
ここではecc627cが取り消したいマージコミットで、7611117
含むブランチが残したいブランチだとします。
ecc627cのログを見るとMerge: 7611117 8998d77とあるので
親コミット7611117が親番号1、親コミット8998d77が親番号2となります。
git revert -mで残したい親番号を指定することで、マージコミットの
取り消しが可能となります。

$ git revert -m 1 ecc627c --no-edit
Finished one revert.
[master c77f6c6] Revert "Merge branch 'topic'"
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 topic_file1
 delete mode 100644 topic_file2

ログを確認する

$ git log --graph
* commit c77f6c6303699fa0b06b25d1becb7d44d0c5716b
| Author: spam ham
| Date:   Fri Apr 25 19:18:32 2014 +0900
| 
|     Revert "Merge branch 'topic'"
|     
|     This reverts commit ecc627c4ec21d56880c43d70ed9c651fc27bb2ac, reversin
|     changes made to 761111709af04a7e736d6ca6a1ac23ccaac94781.
|    
*   commit ecc627c4ec21d56880c43d70ed9c651fc27bb2ac
|\  Merge: 7611117 8998d77
| | Author: spam ham
| | Date:   Fri Apr 25 19:09:41 2014 +0900
| | 
| |     Merge branch 'topic'
| |   
| * commit 8998d77d981f1410201b44e83c3640922bd8941f
| | Author: spam ham
| | Date:   Fri Apr 25 18:42:45 2014 +0900
| | 
| |     add topic_file3
| |   
| * commit cc62d621e45b462469c2b42c932e437ff1bf2fd1
| | Author: spam ham
| | Date:   Fri Apr 25 18:42:23 2014 +0900
| | 
| |     add topic_file2
| |   
| * commit b91fa995c57a94a7a12d70fed70be73aa089dd2c
| | Author: spam ham
| | Date:   Fri Apr 25 18:41:56 2014 +0900
| | 
| |     add topic_file1
| |   
* | commit 761111709af04a7e736d6ca6a1ac23ccaac94781
|/  Author: spam ham
|   Date:   Fri Apr 25 19:08:25 2014 +0900
|   
|       add master_file2
|  
* commit 02c3bd6497e4d9e43117e9802b86af204f063657
  Author: spam ham
  Date:   Fri Apr 25 18:37:02 2014 +0900

      add master_file1

作業ツリーにあるファイルを確認する

$ ls                                             
master_file1  master_file2

補足

上記例では親番号1を残す対象にしましたが、親番号2を残す対象にしたら
どうなるのかも確認しました。

下記の実行結果を見るとわかりますが、git revert後は7611117で作成した
ファイルmaster_file2が作業ツリーから消えています。

$ git revert -m 2 ecc627c4 --no-edit
Finished one revert.
[detached HEAD c577704] Revert "Merge branch 'topic'"
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 master_file2

* commit c5777047cb99a72060cf94b59b2ae64fd507b981
| Author: spam ham
| Date:   Fri Apr 25 19:41:30 2014 +0900
| 
|     Revert "Merge branch 'topic'"
|     
|     This reverts commit ecc627c4ec21d56880c43d70ed9c651fc27bb2ac, rever
|     changes made to 8998d77d981f1410201b44e83c3640922bd8941f.
|    
*   commit ecc627c4ec21d56880c43d70ed9c651fc27bb2ac
|\  Merge: 7611117 8998d77
| | Author: spam ham
| | Date:   Fri Apr 25 19:09:41 2014 +0900
| | 
| |     Merge branch 'topic'
| |   
| * commit 8998d77d981f1410201b44e83c3640922bd8941f
| | Author: spam ham
| | Date:   Fri Apr 25 18:42:45 2014 +0900
| | 
| |     add topic_file3
| |   
| * commit cc62d621e45b462469c2b42c932e437ff1bf2fd1
| | Author: spam ham
| | Date:   Fri Apr 25 18:42:23 2014 +0900
| | 
| |     add topic_file2
| |   
| * commit b91fa995c57a94a7a12d70fed70be73aa089dd2c
| | Author: spam ham
| | Date:   Fri Apr 25 18:41:56 2014 +0900
| | 
| |     add topic_file1
| |   
* | commit 761111709af04a7e736d6ca6a1ac23ccaac94781
|/  Author: spam ham
|   Date:   Fri Apr 25 19:08:25 2014 +0900
|   
|       add master_file2
|  
* commit 02c3bd6497e4d9e43117e9802b86af204f063657
  Author: spam ham
  Date:   Fri Apr 25 18:37:02 2014 +0900
      add master_file1


$ ls
master_file1  topic_file1  topic_file2  topic_file3
52
51
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
52
51