Git - revert 명령어(commit 되돌리기)

git revert 명령어는 반영된 commit의 변경사항을 제거하고 이전으로 되돌립니다. 코드는 commit이 반영되지 않은 것처럼 보이지만, revert commit은 추가되어 히스토리를 확인할 수 있습니다.

1. 특정 commit을 revert하는 방법

git revert <commit id>는 인자로 전달된 commit의 수정사항을 되돌립니다.

아래와 같이 commit이 등록되어있고, 여기서 commit 971c858 (4th commit)이 적용 안된 것처럼, 수정사항을 되돌리려고 합니다.

$ git log
commit 5551f386aa1c22c26ea6aaf678e7cdbf2cded39b (HEAD -> master)
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:19:30 2021 +0900

    5th commit

commit 971c858b160aebc1c6c47ae4713959d36d2e2a52
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:19:00 2021 +0900

    4th commit

commit 10d7d0f6f52539a4e7a1867449b7d6fa023a2685
Author: JS <farfs.dev@gmail.com>
Date:   Fri Jun 25 21:03:38 2021 +0900

    third commit

git revert <commit id>를 입력하시면, 이 변경사항이 적용 안된 것처럼 됩니다.

$ git revert 971c858b160aebc1c6c47ae4713959d36d2e2a52
Removing Second.java
[master 9f4586e] Revert "4th commit"
 2 files changed, 1 insertion(+), 1 deletion(-)
 delete mode 100644 Second.java

git log를 보면 Revert "4th commit"라는 이름으로 commit이 하나 추가되었습니다.

이 commit이 4th commit의 수정사항을 제거하였습니다.

$ git log
commit 9f4586eaf107d84a5b99c6cc538c4006e6e01285 (HEAD -> master)
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:20:17 2021 +0900

    Revert "4th commit"

    This reverts commit 971c858b160aebc1c6c47ae4713959d36d2e2a52.

commit 5551f386aa1c22c26ea6aaf678e7cdbf2cded39b
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:19:30 2021 +0900

    5th commit

commit 971c858b160aebc1c6c47ae4713959d36d2e2a52
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:19:00 2021 +0900

    4th commit

commit 10d7d0f6f52539a4e7a1867449b7d6fa023a2685
Author: JS <farfs.dev@gmail.com>
Date:   Fri Jun 25 21:03:38 2021 +0900

    third commit

변경 내용을 보시면 Revert "4th commit"4th commit의 수정사항을 제거한 commit이라는 것을 확인할 수 있습니다.

$ git show 971c858b160aebc1c6c47ae4713959d36d2e2a52
commit 971c858b160aebc1c6c47ae4713959d36d2e2a52
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:19:00 2021 +0900

    4th commit

diff --git a/First.java b/First.java
index 9335791..a728477 100644
--- a/First.java
+++ b/First.java
@@ -14,7 +14,7 @@ import tech.thdev.media_projection_library.R
 internal class MediaProjectionAccessActivity : AppCompatActivity() {

     companion object {
-        private const val REQ_CODE_MEDIA_PROJECTION = 1000
+        private const val REQ_CODE_MEDIA_PROJECTION = 2000
$ git show 9f4586eaf107d84a5b99c6cc538c4006e6e01285
commit 9f4586eaf107d84a5b99c6cc538c4006e6e01285 (HEAD -> master)
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:20:17 2021 +0900

    Revert "4th commit"

    This reverts commit 971c858b160aebc1c6c47ae4713959d36d2e2a52.

diff --git a/First.java b/First.java
index a728477..9335791 100644
--- a/First.java
+++ b/First.java
@@ -14,7 +14,7 @@ import tech.thdev.media_projection_library.R
 internal class MediaProjectionAccessActivity : AppCompatActivity() {

     companion object {
-        private const val REQ_CODE_MEDIA_PROJECTION = 2000
+        private const val REQ_CODE_MEDIA_PROJECTION = 1000

2. 여러 commit을 revert하는 방법

여러 commit을 revert할 때 git revert [commit id] [commit id] ... 처럼 여러 commit id를 인자로 전달하시면 됩니다.

$ git revert 5551f38 10d7d0f
Removing Third.java
[master 2260375] Revert "5th commit"
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 Third.java
[master 7ce9b41] Revert "third commit"
 1 file changed, 7 insertions(+)

로그 확인해보면, revert된 commit이 적용되었습니다.

$ git log
commit 7ce9b41b3f3c67025ba22eda75bccbdb094e754d (HEAD -> master)
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:31:12 2021 +0900

   Revert "third commit"

   This reverts commit 10d7d0f6f52539a4e7a1867449b7d6fa023a2685.

commit 226037540ee8afb4304c0a7a39ae42de3e20f710
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:31:10 2021 +0900

   Revert "5th commit"

   This reverts commit 5551f386aa1c22c26ea6aaf678e7cdbf2cded39b.

commit 9f4586eaf107d84a5b99c6cc538c4006e6e01285
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:20:17 2021 +0900

   Revert "4th commit"

   This reverts commit 971c858b160aebc1c6c47ae4713959d36d2e2a52.

commit 5551f386aa1c22c26ea6aaf678e7cdbf2cded39b
Author: JS <farfs.dev@gmail.com>
Date:   Sat Jun 26 13:19:30 2021 +0900

   5th commit
Loading script...
codechachaCopyright ©2019 codechacha