GIT 강제 Push 하기 (error: failed to push some refs to)

개인 프로젝트를 개발하던 중 git push 시 에러가 발생하는 문제가 발생하였습니다.

git push -u origin master
To https://github.com/hahwul/a2sv.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/hahwul/a2sv.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

이러한 에러는 보통 remote branch의 코드가 local의 코드보다 앞서있기 때문에 코드단에서 충돌을 회피하고자 git에서 에러를 발생시킨 것이고 이는 git pull로 해결 가능한 경우가 대부분입니다.

다만 임시 방편으로 push 시 --force flag를 추가하여 강제로 push가 가능합니다. 물론 이 방법은 충돌을 무시하기 때문에 소스코드가 꼬일 수도 있으니 잘 확인하시고 진행하시는걸 추천드려요.

  • 기존명령: git push -u origin master
  • 강제명령: git push -u origin master --force

실제로 테스트 해보면 forced update 된 것을 보실 수 있습니다.

git push -u origin master --force
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 10.01 KiB, done.
Total 8 (delta 0), reused 0 (delta 0)
To https://github.com/hahwul/a2sv.git
 + 17b669e...877fdd0 master -> master (forced update)
Branch master set up to track remote branch master from origin.

추가로.. 예전에는 +branch 명으로 강제 push가 가능했습니다. (현재도 되는지는 잘 모르겠네요)

git push -u origin +master 

어쨌던 –force 플래그 이후 부턴 보통 –force를 사용하니 +branch는 참고만 해주세요 :D