0
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?

Gitでの変更取り消し:revert の使い方

Posted at

Gitの git revert コマンドを詳しく解説

Gitで間違った変更を取り消す方法として、git revert コマンドは非常に便利です。このコマンドは選択したコミットを効果的に「打ち消す」新しいコミットを作成し、リモートリポジトリにプッシュされた変更を安全に取り消すことができます。以下にその詳細と、使用方法を解説します。

打ち消したいコミットハッシュの取得方法

コミットハッシュは、Gitの各コミットを一意に識別するIDです。これを取得するには、以下のコマンドを使用します。

git log

このコマンドを実行すると、リポジトリのコミット履歴が表示され、各コミットの詳細とともにハッシュも表示されます。例えば以下のような出力が得られます。

commit 1a2b3c4d5e6f7g8h9i0j (HEAD -> master, origin/master, origin/HEAD)
Author: Your Name <your.email@example.com>
Date:   Mon Sep 14 16:00:49 2020 -0400

    Fix typo in the introduction

commit 123456789abcdef (feature-branch)
Author: Another Dev <another.dev@example.com>
Date:   Sun Sep 13 15:50:29 2020 -0400

    Add new feature

ハッシュ値(1a2b3c4d5e6f7g8h9i0j)が指す具体的な変更のイメージとしては、例えば以下のようなこと

ドキュメントの誤字訂正にて、READMEファイルやオンラインヘルプドキュメントなど、文書の導入部に誤字脱字があった場合、それを修正して更新したコミット。など

git revert の使用例

誤って追加したコミットを取り消す場合、そのコミットのハッシュを指定して以下のように git revert を実行します。

git revert 1a2b3c4d5e6f7g8h9i0j

このコマンドを実行すると、エディタが開き、新しいコミットメッセージを入力することができます。デフォルトのメッセージは次のようになります。

Revert "Fix typo in the introduction"

This reverts commit 1a2b3c4d5e6f7g8h9i0j.

変更を保存してエディタを閉じると、新しい「取り消し」コミットがリポジトリに追加されます。

git revert は、リモートリポジトリにプッシュされた後でも安全に使用できる方法であり、他の共同作業者に影響を与えることなく、過去の変更を効果的に取り消すことができます。このコマンドを使いこなせば、Gitでの誤操作も怖くありません。

git revert後のコミットイメージ

commit 3c4d5e6f7g8h9i0j2k3l4m5n6o7p (HEAD -> master)
Author: Your Name <your.email@example.com>
Date:   Wed Oct 7 16:00:49 2020 -0400

    Revert "Fix typo in the introduction"

    This reverts commit 1a2b3c4d5e6f7g8h9i0j.

commit 1a2b3c4d5e6f7g8h9i0j (origin/master, origin/HEAD)
Author: Your Name <your.email@example.com>
Date:   Mon Sep 14 16:00:49 2020 -0400

    Fix typo in the introduction

commit abcdef1234567890abcdef (feature-branch)
Author: Another Dev <another.dev@example.com>
Date:   Sun Sep 13 15:50:29 2020 -0400

    Add new feature

誤ったコミット (1a2b3c4d5e6f7g8h9i0j) は依然として履歴に存在しますが、その影響は取り消しコミットによって無効化されています。

0
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
0
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?