본문 바로가기
C.W.K.
Stream
Lesson 03 of 05 · published

revert: shared history의 안전한 undo

~18 min · revert, shared-history

Level 0추적 전 새싹
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Revert는 공유 이력을 위한 안전한 undo야

Reset은 이력을 다시 쓰지만 revert는 반대 변경을 담은 commit으로 이력을 늘려. git revert <hash>는 대상 commit과 정반대의 변경을 가진 새 commit을 만들어. 원본 commit은 그대로 남고 그 위에 '이 변경을 되돌렸다'는 기록이 붙지. 이미 push했고 다른 사람이 그 위에서 작업한 commit을 안전하게 되돌리는 방법이야.

git revert HEAD는 가장 최근 commit을, git revert abc1234는 그 뒤에 수십 commit이 있어도 특정 옛 commit을 되돌려. 대상 줄이 이후에 이동하거나 더 수정됐다면 conflict가 날 수 있어. merge 때처럼 편집하고 git add한 뒤 git revert --continue해. git revert --no-edit는 편집기를 열지 않고 Git의 기본 message를 써.

git revert OLDEST..NEWEST로 범위를 주면 source commit마다 revert commit을 하나씩 역순으로 만들어. 이어진 변경 묶음을 되돌릴 때 유용해. --no-commit은 commit을 만들지 않고 반대 변경만 stage하므로, 여러 revert를 commit 하나로 묶는 편이 더 잘 읽힐 때 쓸 수 있어.

Merge commit은 parent가 둘이라 어느 쪽을 유지할 mainline으로 볼지 알려줘야 해. git revert -m 1 <merge-hash>는 첫 parent의 이력은 남기고 두 번째 parent에서 들어온 변경을 되돌려. merge된 기능이 운영을 망가뜨렸지만 branch 전체를 reset할 수 없을 때 맞는 동작이야.

Code

Revert 기본·bash
# 최신 commit revert (위에 새 commit 생성):
git revert HEAD

# Hash 로 특정 옛 commit revert:
git revert abc1234

# Editor 안 열고 revert:
git revert --no-edit abc1234

# 범위 revert — source commit 당 revert commit 하나:
git revert OLDEST_HASH..NEWEST_HASH

# 모든 revert 를 commit 하나로 묶기:
git revert --no-commit OLDEST..NEWEST
git commit -m "Revert feature X (multi-commit rollback)"
Merge commit revert·bash
# 프로덕션 깨뜨린 merge 후:
git log --oneline --merges -n 5      # 안 좋은 merge 찾기

# parent-1 (mainline) 을 유지 쪽으로 두고 merge revert:
git revert -m 1 <merge-hash>

# -m 1 은 "첫 번째 parent 가 mainline".
# 장기 branch 에서 cherry-pick 했으면 -m 2 가 맞을 수도.

# Revert push (force-push 필요 없음 — 일반 commit):
git push

External links

Exercise

commit이 셋 이상인 branch에서 git revert <hash>로 가운데 commit을 되돌려. git log에서 원본과 revert가 모두 보이는지 확인해. 이어서 다른 branch를 현재 branch에 merge해 merge commit을 만들고, git revert -m 1 <merge-hash>로 그 merge를 되돌려. log graph를 읽고 audit trail이 독자에게 무엇을 말하는지 한 줄로 적어봐.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.