본문 바로가기
개발/Git

[Git] Git Push 에러 해결하기 (error: failed to push some refs to...)

by 감토끼 2018. 12. 11.




에러 메시지

git push 명령어로 remote repository에 push 를 하려고 할때

다음과 같은 에러가 날 때가 있습니다. 


error: failed to push some refs to 'XXX.git'


이 에러 밑에 함께 출력되는 hint 메시지에서 원인과 해결방법을 알 수 있습니다.


hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.


원인 : Conflict

간단히 말하면 remote 버전과 로컬의 버전이 달라서 충돌(Conflict)이 된 것입니다.


remote에 내가 받지 않은 파일의 변경사항이 있는데 그걸 무시하고 내 파일을 remote에 올리면 remote에 있는 변경사항이 없어지게 되므로 git에서 push로 내 파일을 올리지 못하게 막는 것입니다.



해결 : Merge

1. remote에 있는 변경사항을 pull 로 내려받은 후,

2. 내 파일과 merge(병합, 합치기) 하여,

3. 다시 push 로 올립니다.


pull 을 받으면 새로운 변경사항은 기본적으로 git이 자동 병합(auto merge)작업을 해줍니다.

하지만 충돌이 나는 부분은 수동으로 병합작업을 해주어야 합니다.



Step 1. pull 받기

(예제의 경우 remote의 이름이 dropbox 입니다)


$ git pull dropbox master


출력 메시지 :

From /Users/gamtoggi/Dropbox/GitRepo/handypos

 * branch            master     -> FETCH_HEAD

Auto-merging lib/hall_page.dart

CONFLICT (content): Merge conflict in lib/hall_page.dart 

Auto-merging lib/bill_history_page.dart

CONFLICT (content): Merge conflict in lib/bill_history_page.dart

Automatic merge failed; fix conflicts and then commit the result.



CONFLICT 부분은 충돌이 난 파일이 있다는 것을 말해줍니다. 위 예제의 경우 

  • lib/hall_page.dart
  • lib/bill_history_page.dart

2 파일에 충돌이 있어서 auto merge에 실패했습니다.



Step 2. merge 하기

충돌된 파일을 열면 다음과 같은 부분을 찾을 수 있습니다.


<<<<<<< HEAD
import 'help_message.dart'; // 내 코드
=======
import 'helper.dart'; // remote 의 코드
>>>>>>> a2f34950a0d63f35696cb34e1774b610dfc7d4d3


위쪽 코드 (<<<<<<< HEAD 부터 ======= 사이에 표시된 부분)가 로컬의 내용이고,

아래쪽 코드 (======= 부터 >>>>>>> a2f34950a0d63f35696cb34e1774b610dfc7d4d3 사이에 표시된 부분)가 remote 에서 pull 받은 내용입니다.


둘 중 어느코드를 쓸지, 또는 다 쓸지, 또는 아예 새로운 코드를 짤 지 결정하여

위 부분을 자유롭게 수정하면 됩니다.


예를 들어 두 코드 모두 필요한 코드라 판단되면 다음과 같이 수정합니다.

수정할 때 <<<<<<< HEAD 이런 부분들을 지워주세요.


import 'help_message.dart';
import 'helper.dart';



Step 3. push 하기

모든 충돌 부분을 수정하였으면 변경사항을 다시 커밋하고, 마지막으로 다시 push 합니다.


$ git add .

$ git commit -m '충돌 해결'

$ git push dropbox master



'개발 > Git' 카테고리의 다른 글

git  (0) 2011.05.19