1. remote 저장소 최신화 전 작업한 브런치를 먼저 merge하고 push하려고 하는 경우
local 상황작업 브런치(conflict-test)에서 플로우
작업 브런치에서 commit 후 main(혹은 dev) 브런치로 이동
main(혹은 dev)브런치에 작업 브런치(conflict) merge
main(혹은 dev)브런치에서 push
$ git push
To https://github.com/hopee0411/git-practice.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/hopee0411/git-practice.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.
해석: 다른 repository에서 원격으로 push를 한 내역이 있어 내 local에 최신화를 시켜줘야한다.
방법1: 최신화를 위해 pull을 받아본다.
Error: 같은 파일 수정이 있어 충돌이 난 걸로 보인다. 그외에 파일은 merge가 된 상태
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
충돌 난 파일 수정 후 status를 확인해보자
해석: 충돌했던 파일은 수정 한 상태로 올라와 있다.
$ git status
On branch main
Your branch and 'origin/main' have diverged,
and have 3 and 2 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
modified: panthers.yaml
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: README.md
add, commit을 해보자
해석: vi가 켜지는 걸 볼 수 있다.
git add .
git commit
To https://github.com/hopee0411/git-practice.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/hopee0411/git-practice.git'
hint: Updates were rejected because the tip of your current branch is behind
Merge branch 'main' of https://github.com/hopee0411/git-practice
# Conflicts:
# README.md
#
# It looks like you may be committing a merge.
# If this is not correct, please run
# git update-ref -d MERGE_HEAD
# and try again.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Your branch and 'origin/main' have diverged,
# and have 3 and 2 different commits each, respectively.
# (use "git pull" to merge the remote branch into yours)
#
# All conflicts fixed but you are still merging.
#
# Changes to be committed:
# modified: README.md
# modified: panthers.yaml
#
여기서 :wq 로 저장하고 vi를 종료시켜보자
해석: 저장후 종료하면 vi에 있던 commit message로 커밋된이 된걸 확인할 수 있다.
:wq
[main d983f68] Merge branch 'main' of https://github.com/hopee0411/git-practice
로그 기록을 확인해보자.
해석: commit 기록이 제대로 올라왔는 지 확인 후 push를 진행하면 정상적으로 remote에 올라간다.
이 방식은 원격 브런치 작업 내용과 로컬 브런치 작업 내용의 시간순을 한곳에 모아서 처리
$ git log
commit d983f685f308a4616b66168d95ed7ff259925fbf (HEAD -> main)
Merge: 27fd4a6 4ff2a4b
Author: hopee0411 <hopee0411@naver.com>
Date: Thu May 25 10:13:07 2023 +0900
Merge branch 'main' of https://github.com/hopee0411/git-practice
commit d983f685f308a4616b66168d95ed7ff259925fbf (HEAD -> main)
Merge: 27fd4a6 4ff2a4b
Author: hopee0411 <hopee0411@naver.com>
Date: Thu May 25 10:13:07 2023 +0900
Merge branch 'main' of https://github.com/hopee0411/git-practice
commit 27fd4a6bb61613744cce1381d92b701731d0d0a9
Merge: 574f4e2 952e5ab
Author: hopee0411 <hopee0411@naver.com>
Date: Thu May 25 10:00:02 2023 +0900
Merge branch 'conflict-test'
commit 952e5ab60fb8798df054e530a21f922d93c7714a (conflict-test)
Author: hopee0411 <hopee0411@naver.com>
Date: Thu May 25 09:58:08 2023 +0900
conflictTest_230525
commit d983f685f308a4616b66168d95ed7ff259925fbf (HEAD -> main)
Merge: 27fd4a6 4ff2a4b
Author: hopee0411 <hopee0411@naver.com>
Date: Thu May 25 10:13:07 2023 +0900
Merge branch 'main' of https://github.com/hopee0411/git-practice
방법2 : reabse를 이용
해석: 다른 repository에서 원격으로 push를 한 내역이 있어 내 local에 최신화를 시켜줘야한다.
$ git push
To https://github.com/hopee0411/git-practice.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/hopee0411/git-practice.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.
해석: commit 내역은 머지 되었고 충돌난 파일만 수정하면 될 것 같다.
git pull --rebase
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 694 bytes | 57.00 KiB/s, done.
From https://github.com/hopee0411/git-practice
dc1c731..93374fe main -> origin/main
error: could not apply f7fac4c... lastTest: 로컬
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply f7fac4c... lastTest: 로컬
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
git add. git rebase --continue 명령어를 해보자
해석: 이렇게 vi가 나올 것이다.
git add .
git rebase --continue
README.md: needs merge
lastTest: 로컬
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# interactive rebase in progress; onto 93374fe
# Last command done (1 command done):
# pick f7fac4c lastTest: 로컬
# No commands remaining.
# You are currently rebasing branch 'main' on '93374fe'.
#
# Changes to be committed:
# modified: README.md
충돌부분 수정이 완료 되었고 저장하고 나오자 (저장 후 종료) :wq
해석: 정상적으로 commit이 되었고 이렇게 하면 commit 시간순을 remote commit 이후에 내 commit이 들어가게 된다.
이후에 push하면 정상 동작
:wq
[detached HEAD 566fe6a] lastTest: 로컬
1 file changed, 3 insertions(+), 1 deletion(-)
Successfully rebased and updated refs/heads/main.
방법3: 가장 이상적인 플로우(방법3를 지향하는 게 좋을 것 같다)
local 상황
작업 브런치(conflict-test)에서 플로우작업 브런치에서 commit 후 main(혹은 dev) 브런치로 이동main(혹은 dev)브런치에 작업 브런치(conflict) mergemain(혹은 dev)브런치에서 push
주의: 이 상태를 만들지 않는 것이다.
$ git push
To https://github.com/hopee0411/git-practice.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/hopee0411/git-practice.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.
local 플로우를 변경해보자
local 상황
작업 브런치(conflict-test)에서 플로우작업 브런치에서 commit 후 main(혹은 dev) 브런치로 이동main(혹은 dev)브런치에서 pull로 한 번 더 최신화main(혹은 dev)브런치에서 작업 브런치 merge충돌이 났다면 수정 후 commit 후 push 하면 된다.
$ git pull
Updating dc1c731..93374fe
Fast-forward
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
$ git merge conflict_test
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
$ git add .
$ git commit
Merge branch 'conflict_test'
# Conflicts:
# README.md
#
# It looks like you may be committing a merge.
# If this is not correct, please run
# git update-ref -d MERGE_HEAD
# and try again.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# All conflicts fixed but you are still merging.
#
:wq
$ git push
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 498 bytes | 498.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/hopee0411/git-practice.git
93374fe..c18bb2d main -> main
해석: 먼저 pull로 최신화를 시키고 내 작업 브런치를 merge한 후 충돌 난 부분 수정하고 add, commit 하면 vi가 나온다 여기서 :wq로 저장후 vi 종료 시키고 push를해주면 정상 동작한다.
'Git' 카테고리의 다른 글
[GitHub Actions] Spring boot, vue 통합, 배포(CICD) 구축 (0) | 2023.11.01 |
---|---|
Git 에서 CRLF 개행 문자 차이 해결 (0) | 2023.06.28 |
Github repository 만들기 및 함께할 팀원 추가 (0) | 2023.05.04 |
Git token 컴퓨터에 저장하기 (0) | 2023.05.04 |