sharingStorage

git error 해결법 - fatal: You are not currently on a branch.To push the history leading to the current (detached HEAD)state now, use 본문

git

git error 해결법 - fatal: You are not currently on a branch.To push the history leading to the current (detached HEAD)state now, use

Anstrengung 2022. 2. 22. 15:46

git push 명령어 사용시 error발생

 

error code:

fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use


  git push origin HEAD:<name-of-remote-branch>

 

 

원인 :

현재 상태가 브랜치에 있는 것이 아니라 옛날의 한 시점의 스냅샷을 보고 있다.

 

 

해결 : 

  1. git push origin HEAD: master 명령어 사용                                                                                             
    git push origin HEAD: master​
    - ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to  오류 발생
  2. git push origin HEAD: master --force 명령어 사용
    git push origin HEAD: master --force
    - 현재 working디렉토리 변경 점이 아닌 옛날의 한 시점이 커밋됨. 분리된 상태의 버전이 정말로 유지하려는 버전인지 확신하는 경우에만 사용해야함.  즉 커밋은 성공하지만 엉뚱한 시점의 코드가 커밋될 수 있다.                                                                               추가로 강제로 푸시하면 해당 브랜치가 체크아웃된 다른 모든 사용자에게 문제를 일으킬 수 있다.
  3. 분리된 HEAD에서 임시 브랜치를 만든 다음 해당 브랜치를 병합하는 방법                                                                                                        1. temp branch생성, temp로 checkout
    git branch temp
    git checkout temp​
       2. temp branch가 정상이라면 아래 명령어를 통해 master branch가 temp를 가리키게함
    git branch -f master temp 
    git checkout master​
        3. Switched to 'master' 라는 문구가 뜨고 Your branch is ahead of 'origin/master' by 22 commits. 이렇게 뜨며 git push를 사용하라는 문구가 뜸                                                                                                                                                                                                 4. git push 명령어 or git push origin master 명령어 사용                                                                                                                             5. git branch -d temp  - 임시로 만든 브랜치 삭제
  4. git pull 원격 저장소 으로 원격과 로컬의 저장소 간의 차이가 없도록 만든 후 다시 git add,commit 하고 push를 해준다.

 

 

엄청나게 많이 알아보고 많은 시도를 했는데 결국은 git에 대한 내부 프로세스에 대한 이해가 부족했고 여태껏 명령어만 기계처럼 따라쳤던 것에 대한 반성을 한다. 결과론 적인 말이지만 git checkout master만 먼저 시도해봤어도 해결했을 수도 있다.

 

 

 

 

 

 

Reference

Comments