git에서 merge commit을 revert 하는 방법
git에서 보통 commit을 revert하는게 아닌, merge commit을 revert 하는 명령어는 아래와 같다.
git revert 3433fc82 -m 1
어떤 commit을 revert할지 선택하고(이 경우 merge commit), 옵션 m 으로 merge 된 두개의 branch중 어떤 branch를 선택할지 선택할 수 있다. 여기서 중요한건, 옵션 m의 번호는 날려서 없앨려고 하는 branch를 선택하는게 아니라 남길려고 하는 branch를 선택하는 것이다.
아래 상황은 test/a branch에 test/b branch를 merge 한 상황이다.
git 에서 명령어를 통해서 branch 구조를 확인하는 방법은 --graph 옵션을 확인 하는 것인데.. 아래와 같이 확인 할 수 있다.
$ git log --graph
* commit 3433fc8222e34266da25ed00db300d7efb66e331 (HEAD -> test/a, origin/test/a)
|\ Merge: 72d5201 c4737ec
| | Author: tweak <tweak@xxx.com>
| | Date: Tue Oct 1 23:11:05 2024 +0900
| |
| | Merge branch 'test/b' into test/a
| |
| * commit c4737ecb73e1d2b15bb0119cdb76d637f02514ac (origin/test/b, test/b)
| | Author: tweak <tweak@xxx.com>
| | Date: Tue Oct 1 23:04:58 2024 +0900
| |
| | change id
| |
* | commit 72d52018ecc09c516e87a3af255efbcc3a0188ae
|/ Author: tweak <tweak@xxx.com>
| Date: Tue Oct 1 23:10:00 2024 +0900
|
| change model
그리고 merge commit을 git log 명령어로 보면 Merge: 에 어떤 두개의 commit이 merge가 되었는지 표시가 된다.
git show 명령어로 두개 중 어떤 것이 내가 revert 해야 하는 것인지 확실하게 확인 한다. git show를 통해서 보면 해당 commit이 어떤 라인을 어떻게 변경했는지 표시해준다.
$ git show 72d5201
commit 72d52018ecc09c516e87a3af255efbcc3a0188ae
Author: tweak <tweak@xxx.com>
Date: Tue Oct 1 23:10:00 2024 +0900
change model
diff --git a/src/ ...(생략) /Request.java b/src/ ...(생략) /Request.java
index d4746ad..d15d324 100644
--- a/src/ ...(생략) /Request.java
+++ b/src/ ...(생략) /Request.java
@@ -9,7 +9,7 @@ import java.util.List;
public classRequest implements Serializable {
private static final long serialVersionUID = 1L;
- private static final String MODEL = "~~~~~~";
+ private static final String MODEL = "~~~~~a";
private String model;
이제 어떤 branch를 제거 하고 어떤 branch를 살릴지 선택했다면 revert 명령어를 수행한다.
$ git revert 3433fc8222e34266da25ed00db300d7efb66e331 -m 1
[test/a 1a88bda] Revert "Merge branch 'test/b' into test/a"
1 file changed, 1 insertion(+), 1 deletion(-)
commit 메세지를 작성하고 자장하면 revert commit이 생성되어 있다.
이제 다시 git log로 확인하면 아래와 같다.
$ git log --graph
* commit 1a88bdaff8634ebd56978dc9631443ba5a0097dd (HEAD -> test/a)
| Author: tweak <tweak@xxx.com>
| Date: Tue Oct 1 23:28:21 2024 +0900
|
| Revert "Merge branch 'test/b' into test/a"
|
| This reverts commit 3433fc8222e34266da25ed00db300d7efb66e331, reversing
| changes made to 72d52018ecc09c516e87a3af255efbcc3a0188ae.
|
* commit 3433fc8222e34266da25ed00db300d7efb66e331 (origin/test/a)
|\ Merge: 72d5201 c4737ec
| | Author: tweak <tweak@xxx.com>
| | Date: Tue Oct 1 23:11:05 2024 +0900
| |
| | Merge branch 'test/b' into test/a
| |
| * commit c4737ecb73e1d2b15bb0119cdb76d637f02514ac (origin/test/b, test/b)
| | Author: tweak <tweak@xxx.com>
| | Date: Tue Oct 1 23:04:58 2024 +0900
| |
| | change id
| |
* | commit 72d52018ecc09c516e87a3af255efbcc3a0188ae
|/ Author: tweak <tweak@xxx.com>
| Date: Tue Oct 1 23:10:00 2024 +0900
|
| change model
|
마지막으로 revert commit을 git show로 한번더 확인 해서 원하는데로 revert 가 되었는지 확인한다.
git show 1a88bdaff8634ebd56978dc9631443ba5a0097dd
이상이 없다면 push 한다.
git push
보통 branch merge하게 되면 기준이 되는 branch가 있고, 수정을 한 branch를 merge하게 되므로 보통 merge commit을 revert 할 때는, m 옵션을 1을 넣으면 된다.