보통 main
branch는 무결한 branch로 개발 완료된 feature들에 대해서 병합되도록 한다. 즉 각 feature별 branch를 따로 만들고 해당 feature의 기능들이 개발되고 검증이 완료되면 main
branch에 넣는 식이다.
feat/ui: ---ui commit1 --> ui commit1
/ \
main: commit1 ---> commit2-------------------------->commit3-->commit4
\ /
feat/server: ---server commit1-----------------------
이런 식으로 개발이 된다는 것이다. main
branch를 직접 건드려서 코드를 넣는 것이 아ㅣ라, feat/ui
와 feat/server
와 같은 feature branch를 만들고 무결성이 완료되면 main
branch에 넣는 식이다. 따라서, main
branch는 언제나 배포해도 문제없이 동작해야한다.
github에서 feature branch를 main branch에 넣는 작업을 'pull requests'라고 한다. 이 과정에 있어서 github는 code review를 지원하는데, code review 결과 따라서 해당 branch의 기능을 main
branch에 넣을 지 안 넣을 지를 결정한다.
먼저 demo
repo를 만든 다음에 아래의 명령어를 입력해주도록 하자.
echo "# demo" >> README.md
git init
git add README.md
git commit -m "commit1"
git branch -M main
git remote add origin https://github.com/colt/demo.git
git push -u origin main
README.md
가 추가된 것을 볼 수 있다. 다음으로 새로운 branch인 feat/ui
를 만들고 다음의 code를 넣어보도록 하자.
# feat/ui
git switch -c feat/ui
echo "ui code" >> ./README.md
git add ./README.md
git commit -m "feat: add feat/ui commit1"
git push origin feat/ui
github로 가게되면 feat/ui had recent pushes less than a minute ago
이라는 안내 문구가 있고, Compare & pull request
라는 버튼이 활성화되었을 것이다. 이 버튼을 누르면 pull requests
를 만들 수 있다.
create pull request
버튼을 눌러서 pull requests를 만들면, 다른 이들의 code review를 받고 merge될 지 abandon될 지 결정된다.
이 경우 Merge pull request
를 눌러서 merge 시키도록 하자.
그러면 main
branch에 feat/ui
branch의 commit이 적용되었을 것이다.
다음으로 feat/server
branch를 만들고 README를 수정해보도록 하자.
git switch -c feat/server
echo "server code" >> ./README.md
git add ./README.md
git commit -m "feat: add feat/server commit1"
git push origin feat/server
똑같이 pull request
를 만들어주고 merge를 해주도록 하자. 문제없이 병합이 될 것이다.
정리하면 지금의 상황은 다음과 같다.
feat/ui: ---ui commit1
/ \
main: commit1 --------------> merge commit2 ----> merge commit3
\ /
feat/server: -------------------server commit1----
만약 다음과 같은 상황은 어떻까?
feat/ui: -u1- ---u2-----
/ \ / \
main: m1 --> m2 --> m3----?
\ /
feat/server: ------s1----
다음의 상황은 feat/ui
branch에서 u2라는 commit을 만들고 main branch에 병합시키려고 하는 것이다. 이 때 u2
commit은 main
branch의 m2 commit으로부터 나왔기 때문에 m3
가 반영되지 않났다. 심지어 u2
와 m3
가 서로 conflict가 발생한다면 pull requests가 어떻게 될까??
ui commit2
는 README.md
를 다음과 같이 고치려고 한다.
new ui commit2
기존의 값들을 모두 지워버리고 새로운 new ui commit2
값으로 덮어쓰는 것이다.
git switch feat/ui
echo "new ui commit2" > ./README.md
git add ./README.md
git commit -m "feat/ui commit2"
git push origin feat/ui
다음으로 pull requests를 만들어보자, conflict가 발생한다고 경고는 하지만 pull requests를 만들 수 있다.
pull requests를 만들었다면 This branch has conflicts that must be resolved
라는 문구가 나오고, online web에서 해결할 지 local ide에서 해결할 지 결정하도록 방법을 알려준다. merge를 하는 script에 자세하게 방법이 적혀있는데, 다음과 같다.
먼저 현재의 branch가 어디에 있는 지 확인하도록 하자.
git branch
feat/server
* feat/ui
main
다음으로 merge를 해보도록 하자.
git switch main
git pull origin main
git switch feat/ui
git merge main
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
위와 같이 Auto-merging
로그가 나올 것이다. 이는 git merge
시에 내 local에 있는 branch와 merge를 시도하기 때문에 해당 branch에 대한 정보를 가져오고, merge하려는 branch로 가서 merge를 시켜주면 되는 것이다.
이제 merge를 위해서 conflict를 수정해보도록 하자.
Accept Both changes를 누르면 다음과 같이 README.md가 설정된다.
new ui commit2
# demo
ui code
ui code
server code
다음으로 merge된 내용에 대해서 새로운 commit을 만들어 push해주도록 하자.
git add ./README.md
git commit -m "Merge feat/ui and main"
git push origin feat/ui
다시 github에 가면 재밌게도 우리가 이전에 만들었던 commit과 동일한 pull requests에서 merge commit이 추가된 모습을 볼 수 있다. 이제 Merge pull request가 활성화되었기 때문에 버튼을 눌러 실행시킬 수 있다.
이렇게 main
branch에 github에 있는 코드를 refresh하고 난 다음에, merge를 하기위한 branch로 바꾸어 merge를 시켜주고 merge commit을 만들어주면 된다. 그러면 다음과 같이 되는 것이다.
feat/ui: -u1- ---u2-----
/ \ / \
main: m1 --> m2 --> m3----m4(m3+u2)
\ /
feat/server: ------s1----
이렇게 pull reuqests의 충돌 상황을 해결할 수 있다.