Gitsunmin

TIL

TIL
(=Today I Learned)

Resurrection of commit

git을 사용하다 보면, git rebase를 하거나 git reset을 해서 commit을 지우는 일을 할 경우가 많습니다. 하지만 이 또한 실수로 지우는 경우가 생기는데, 이러한 경우에는 git reflog를 사용해서 commit을 살릴 수 있습니다.

> git reflog

를 입력하면, commit, rebase 또는 reset등 git 명령어를 수행한 로그를 hash id와 함께 볼 수 있습니다.

sample

54934aa HEAD@{1}: rebase (finish): returning to refs/heads/master
54934aa HEAD@{2}: rebase (pick): add Page Visibility API til and edit README.md
7520b7d HEAD@{3}: rebase (pick): add shallow Merge And Deep Merge til
b80015e HEAD@{4}: rebase (pick): add Promise.allSettled And Promise.all til
66992c1 HEAD@{5}: rebase (start): checkout HEAD~5
bfd8b53 HEAD@{6}: pull: Fast-forward
66992c1 HEAD@{7}: reset: moving to HEAD

위 에서 원하는 hash id를 선택한 후 아래 명령어를 수행하여 commit을 살리거나,

> git reset --hard <hash id>

또는

> git checkout -b <삭제한 브랜치명> <hash id>

이런식으로 삭제된 브랜치도 살릴 수 있습니다.