구차니 2025. 12. 11. 11:00

reset은 아예 없애는거라 내 취향은 아니고

revert 가 딱이긴 한데 의미가 내가 생각하던 것과는 조금 달랐다

 

예를 들어

git log 에서

hash 1111

hash 2222

hash 3333

hash 4444

 

git revert 2222 라고 하면

hash 2222 에서 했던 녀석만을 되돌리게 된다.

 

정리 하자면

revert 는 "hash 의 해당 commit을 돌린다" 이기 때문에

돌아가고 싶은 커밋이 아니라

없애고 싶은 커밋을 명시해야 한다.

 

man page 에도 존재하는 커밋을 되돌린다고 되어있다.

git-revert - Revert an existing commit

Synopsis
git revert [--edit | --no-edit] [-n] [-m parent-number] [-s] <commit>

Description

Given one existing commit, revert the change the patch introduces, and record a new commit that records it. This requires your working tree to be clean (no modifications from the HEAD commit).

Note: git revert is used to record a new commit to reverse the effect of an earlier commit (often a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset(1), particularly the --hard option. If you want to extract specific files as they were in another commit, you should see git-checkout(1), specifically the git checkout <commit> - <filename> syntax. Take care with these alternatives as both will discard uncommitted changes in your working directory.

[링크 : https://linux.die.net/man/1/git-revert]

 

만약 돌려야 한다면 한번에 될진 모르겠지만 1111, 2222를 revert 해야 할지도? (확인필요)

 

$ git log --oneline
17b787d bug3 fixed
1fefb57 bug2 fixed
8b3560b bug1 fixed
784065c feature1 developed
...
이제 우리는 이러한 버그 수정을 없애고 저장소를 bug1 fixed 상태 이전의 상태로 되돌리기로 결정했습니다.

따라서 --no-commit 옵션과 함께 git revert 명령을 사용할 수 있습니다. 명령 구문은 git revert --no-commit <commit>입니다.

따라서 완료된 버그 수정의 세 커밋을 되돌리려면 다음과 같이 해야 합니다.

$ git revert --no-commit 17b787d784065c
$ git revert --no-commit 1fefb57
$ git revert --no-commit 8b3560b

[링크 : https://www.delftstack.com/ko/howto/git/git-revert-multiple-commits/]

[링크 : https://medium.com/@rnrjsah789/git-revert-4c569366a77b]

[링크 : https://www.inflearn.com/community/questions/172695/%EC%97%AC%EB%9F%AC%EA%B0%9C%EC%9D%98-%EC%BB%A4%EB%B0%8B%EC%9D%84-revert?srsltid=AfmBOoo4CTQkAtobmxI0lo6YlKOM1mC1_XZ7rhbI66F8q7CUayTbRIDG]

 

git revert를 하면 자동으로 커밋메시지를 생성해서 커밋하는데 

여러개 돌릴때 마다 메시지 남기면 번거로우니 쭈르르르륵 되돌려 버릴땐 -n 옵션 주면 된다고 한다.

-n, --no-commit
Usually the command automatically creates a commit with a commit log message stating which commit was reverted. This flag applies the change necessary to revert the named commit to your working tree and the index, but does not make the commit. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.
This is useful when reverting more than one commits' effect to your index in a row.

[링크 : https://linux.die.net/man/1/git-revert]