상세 컨텐츠

본문 제목

git detached HEAD 상태로 커밋이 사라지는 경우

Study/GIT

by 휀스 2012. 12. 9. 22:57

본문


git detached HEAD 상태 (git 수정 내용이 없어지게 되는 경우)


git garbage collection process 에 의해 수정내용이 사라지게 되는 경우는 아래와 같다.


현재상태가 아래의 그림과 같다면


HEAD (refers to branch 'master') | v a---b---c branch 'master' (refers to commit 'c') ^ | tag 'v2.0' (refers to commit 'b')


파일 내용을 edit 후

$ git add

$ git commit

을 하면 아래와 같이 커밋이 생긴다.


	               HEAD (refers to branch 'master')
		          |
		          v
a---b---c---d  branch 'master' (refers to commit 'd')
      ^
      |
    tag 'v2.0' (refers to commit 'b')


이제

$ git checkout v2.0 

또는

$ git checkout master^^ 실행을 하면 아래와 같은 모습으로

HEAD 가 이동한다.


HEAD (refers to commit 'b')
      |
      v
a---b---c---d  branch 'master' (refers to commit 'd')
      ^
      |
  tag 'v2.0' (refers to commit 'b')


또 다시 edit 후

$ git add; 

$ git commit;

을 하면 e 커밋 이 생긴다.


    HEAD (refers to commit 'e')

        |
        v
        e
       /
a---b---c---d  branch 'master' (refers to commit 'd')
      ^
      |
    tag 'v2.0' (refers to commit 'b')




파일을 또 수정 후

$ git add; 

$ git commit;

하여 f commit 까지 생성 하였다고 하자.


           HEAD (refers to commit 'f')
	      |
	      v
        e---f
       /
a---b---c---d  branch 'master' (refers to commit 'd')
      ^
      |
    tag 'v2.0' (refers to commit 'b')


문제는 이제 부터이다.

$ git checkout master

의 결과로 


e, f 커밋 은 참조할 곳이 없는 상태가 된다. (git checkout 으로 HEAD 이동이 불능상태)

이런 상황에서 커밋은 git garbage collection 에 의해 삭제가 된다.



                         HEAD (refers to branch 'master')
        e---f             |
       /                   v
a---b---c---d  branch 'master' (refers to commit 'd')
      ^
      |
    tag 'v2.0' (refers to commit 'b')


이를 막기 위해서는 f 커밋 생성 후 


$ git checkout -b foo

$ git branch foo (still detatched HEAD 상태임)

$ git tag foo (still detatched HEAD 상태임)


위의 셋중 하나를 실행해 주면 된다.


http://git-scm.com/docs/git-checkout 참고




관련글 더보기