본문 바로가기
utils/git and github

[git] revert : 특정 commit 취소하기.

by ds31x 2023. 12. 30.

revert : 특정 commit 취소하기.

revert: 되돌리다.

 

git revert [취소할 commit id (hash value)] 는 repository에 반영된 내용을 취소해야 하는 경우 사용됨.

  • 취소를 시키고 취소된 상태의 새로운 commit을 추가하는 방식임. : 취소되는 commit도 log에서 확인 가능함.
  • 이 부분이 아예 취소되는 commit를 log에서도 삭제해버리는 reset 과의 가장 큰 차이점임.
    • reset 은 되돌아갈 commit의 commit id(=hash value)를 인자로 넘겨줌.
    • revert 는 취소시킬 commit의 commit id를 인자로 넘겨줌.
  • remote repository로 공동작업자와 같이 작업하는 경우, reset이 아닌 revert로 commit을 취소 시켜야 함.

자동으로 commit이 이루어지는데, repository에서 특정 commit을 취소시키고 이전 상태로만 돌아가려면 (즉, commit만 하지 않으려면) 다음과 같이 --no-commit 옵션을 준다.

git revert --no-commit [취소할 commit id (hash value)]
  • 이 경우, revert 하고 나서 다른 수정을 하고 나서 commit를 할 수 있음.

예제.

1. git_ex_revert 라는 directory를 만들고 해당 디렉토리 안에서 git init을 수행한다.

 

2. test00.txt 파일을 해당 directory 내에 만들고 내용을 다음과 같이 함.

# 최초 내용.

이 파일은 revert로 돌아올 때 확인할 수 있는 file임.

 

3. git add test00.txt 를 통해 stage로 이동시키고, git commit -m "first commit" 를 수행하여 repository로 이동시킴.

 

4. test01.txt 파일을 추가하고 해당 파일의 내용은 다음과 같이 함.

# content

두번째 commit에 추가되는 파일임.

 

5. git add test01.txt 를 통해 stage로 이동시키고, git commit -m "second commit" 를 수행하여 repository로 이동시킴.

 

6. test01.txt` 파일을 다음과 같이 새로운 내용을 추가하여 저장.

# content

두번째 commit에 추가되는 파일임.

# modified

세번째 commit에서 수정된 부분임.

 

7. git add . 를 통해 stage로 이동시키고, git commit -m "third commit" 를 수행하여 repository로 이동시킴.

 

8. git log를 수행하여 두번째 commit에 대한 commit id를 얻어온다 :

d20e2899b05d18b8fa37ff56bea56e419feaa47b 라고 가정.

 

9. git revert d20e2899b05d18b8fa37ff56bea56e419feaa47b 를 수행한다.

 

9번을 수행하며 , CONFLICT가 발생한다. 메시지는 다음과 같다.

❯ git revert d20e2899b05d18b8fa37ff56bea56e419feaa47b
CONFLICT (modify/delete): test02.txt deleted in parent of d20e289 (sencond commit) and modified in HEAD.  Version HEAD of test01.txt left in tree.
error: could not revert d20e289... sencond commit
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".
❯
  • 앞서 revert로 두번째 commit을 취소하면,
    test01.txt가 없는 첫번째 commit으로 되돌려지고,
    이 후 네번째 commmit이 이루어져야 한다.
  • 문제는 세번째 commit에서 test01.txt에 수정을 했는데,
    두번째 commit을 취소키기 위해 
    git은 test01.txt를 삭제하여 첫번째 commit와 동일한 상태로 네번째 commit을 하려고 시도함.
  • 이 시도는 해당 세번째 commit에서 해당 파일을 고친 내용과 충돌(conflict)이 됨.
  • 때문에 이를 해결(resolving conflicts)하고나서
    해결된 working tree를 stage로 올린(mark them with ...) 후에
    git revert --continue를 수행하라는 메시지가 뜨게 됨.

일반적으로 conflict가 발생하지 않는 경우엔 그냥 잘 되었다는 메시지가 뜨지만,
생각보다 revert의 경우 conflict가 자주 발생한다.
때문에 예제에서 conflict가 발생하도록 한 것임.


10. confilct 문제를 일으키는  test01.txtgit rm test01.txt로 삭제하고 이를 stage에 반영시켜 conflict를 해결한다.
11. 이 후 git revert --continue를 수행한다.

 

위의 예는 revert를 하는 간단한 예제이며, conflict를 해결하는 방법을 소개하고 있다.
제대로 수행하면 다음과 같은 메시지가 보임.

❯ git rm test01.txt
rm 'test01.txt'
❯ git revert --continue
[main 2f66ee0] Revert "sencond commit"
 1 file changed, 7 deletions(-)
 delete mode 100644 test02.txt
728x90