본문 바로가기
SWE/LetGit

GIT 브랜치 실수로 삭제했을 때, git branch 복구

by S나라라2 2022. 10. 26.
반응형

 

lol

git branch를 실수로 삭제하였다.

삭제한 브랜치를 다시 되살려보자

 

 

1. <local_br>브랜치 삭제하기 (가정)

$ git branch -D <local_br>
	Deleted branch <local_br> (was 84fd72d08).

브랜치를 삭제할 때 나왔던 커밋id가 중요하다. 

이 id가 있어야지 복구가 가능하다.

 

2. 브랜치의 commit id로 되돌아가기

$ git checkout 84fd72d08
		Note: checking out '84fd72d08'.
		
		You are in 'detached HEAD' state. You can look around, make experimental
		changes and commit them, and you can discard any commits you make in this
		state without impacting any branches by performing another checkout.
		
		If you want to create a new branch to retain commits you create, you may
		do so (now or later) by using -b with the checkout command again. Example:
		
		  git checkout -b <new-branch-name>
		
		HEAD is now at 84fd72d08 <commit msg>

삭제된 브랜치의 마지막 commit id로 checkout한다.

그러면 현재는 HEAD 에서 떨어져나온 상태일 뿐이고, 브랜치를 새로 만들고 싶으면 아래의 명령어를 수행하라는 안내 문구를 확인할 수 있다.

 

3. 해당 commit id로 브랜치 생성

$ git checkout -b <new-branch-name>
	Switched to a new branch '<new-branch-name>'

완료! new-branch-name이라는 이름으로 삭제된 브랜치를 복구했다.

 

 

<참고>

컬, 리모트 트레킹 브랜치를 모두 확인하는 방법

$ git branch -avv

 

반응형