본문 바로가기

Git

[Git] 원격저장소와 로컬저장소의 커밋이 다를경우

# 문제 발생의 원인

Draw.io를 깃허브와 연동해서 FlowChart를 만들고 draw.io에서 바로커밋을 한 상태이다.
그런데인지를 못한상태에서 나의 로컬에서 새로운 커밋을 만들고 push를 하여 오류가 난상태이다.
오류는 아래와 같다.

# 오류발생전 커밋

➜ swift-starter-week2 git:(2_Quokkaaa) ✗ git add .
➜ swift-starter-week2 git:(2_Quokkaaa) ✗ git commit
[2_Quokkaaa 31d27a1] feat: generateLotterryNumbers 함수 구현
2 files changed, 12 insertions(+), 2 deletions(-)

# 오류 발생

➜ swift-starter-week2 git:(2_Quokkaaa) git push
To https://github.com/Quokkaaa/swift-starter-week2.git
! [rejected] 2_Quokkaaa -> 2_Quokkaaa (fetch first)
error: failed to push some refs to 'https://github.com/Quokkaaa/swift-starter-week2.git
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.

# 해결방법

git reset 원하는 시점
git push -f origin 2_Quokkaaa

나의 로컬상태의 커밋을 원격저장소의 커밋의 전단계로 reset해놓고 로컬의 커밋내역을 원격으로 푸쉬하여 업데이트 시킨것이다.