Programming

; develop a program

Git & GitHub

[Git & GitHub] 간단한 협업 실습

Clloud_ 2022. 11. 11. 11:14
반응형

이번 포스팅에서는 Git과 GitHub를 사용하여 협업하는 방법에 대하여 간단하게 공부를 해보고자 한다.

 


사전작업

remote 저장소 생성(반드시 README.md 포함해서 초기화) 후 협업할 팀원 초대

github.com에 저장소 생성 후, 협업할 팀원 초대하면 된다.


github.com > 생성한 저장소 > Settings > Manage access > Collaborators > Add people

 

 


[remote & local] 기본 세팅

협업하는 사람들 모두 개개인의 로컬 컴퓨터로 저장소 clone

git clone {저장소 url}

git 기본 설정

# set name and email
git config --global user.name "username"
git config --global user.email "useremail@gmail.com"

 


충돌을 만들기 위한 branch flow

 


[remote] 이슈 생성

1. github.com > 생성한 저장소 > Issue 메뉴
2. Issue 생성

a. 제목 : ex) 개발자 ㅇㅇ의 자기소개 추가
b. 내용 : 자유롭게 작성

 


[local] 작업(feature)브랜치 생성

1. main 브랜치를 기준으로 작업 브랜치를 생성한다.

a. 예 ) feat-sh-introd

2. developer_OOO.md 파일 생성 (OOO에 자신의 이름)
3. developer_OOO.md 파일에 자기소개를 작성
4. README.md 파일을 자유롭게 수정

# Playground
- 개발자 OOO의 사이드 프로젝트입니다.

 

5. 아래 명령으로 변경사항 커밋

# 변경사항 확인
git diff

# 상태 확인
git status
git add .

# 커밋
git commit
# 상태 확인
git status
# 로그 확인
git log

 


[local → remote] 저장소에 올리기

git branch
git fetch origin
git push origin 작업브랜치

 


[remote] 충돌 발생용으로 README.md 를 수정

레포지토리 만든 사람이 아닌 다른 사람이 수정해야 한다.

 

branch : main
Github Code 메뉴에서 README.md 파일을 직접 수정하고 main 브랜치로 커밋

 


[remote] github.com에서 브랜치 확인하기 & Pull Request 생성

github.com > 협업용 저장소
본인이 올린 브랜치가 반영이 되었는지 확인하고, 정상적으로 push가 되었는지 확인한 후 Pull Request를 생성한다.

 

1. github.com > 생성한 저장소 > Pull Request 메뉴
2. Pull Request 생성

a. base : main
b. compare : 작업 브랜치

내용은 자유롭게 작성하고 Pull Request를 생성

 

3. 생성 후에 아래처럼 conflict이 발생한다면 성공

 


[local] 충돌 해결하기

1. 원격 저장소의 변경사항을 로컬에 반영하기

git checkout main
git fetch origin
### 변경되었다는 로그가 나온다.

git merge
# or git pull 로 한번에 해결이 가능하다.

2. 로컬 저장소의 feature 브랜치로 이동

git checkout 작업브랜치

3. feature 브랜치를 main 브랜치 기준으로 rebase

코드의 기준(base)을 최신 main 기준으로 변경하는 작업

git rebase main

4. 로컬 저장소에서도 충돌이 발생했을 것이다.

$ git rebase main
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
error: could not apply 6eed5ee... Add sh's hobby and files
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 6eed5ee... Add sh's

5. README.md 파일 수정
6. 수정한 사항을 Staging area에 등록

git diff --color
git status
git add README.md

7. git status명령어로 잘 반영이 되었는지 확인

8. rebase 완료

git rebase --continue

9. 원격 저장소로 강제 푸시

git push -f origin 작업브랜치

 


[remote] Pull Request & Merge

1. github.com > 생성한 저장소 > Pull Request 메뉴
2. Pull Request 속 내용 사용해보기

a. comment 작성
b. 이슈 본인에게 assign
c. reviewer 지정

3. 코드 리뷰


[remote] 코드 리뷰가 완료된 branch는 main(base) 브랜치로 merge

1. Merge : 아래 세 가지 방법 중 택 1

a. Merge pull request = git merge --squash
현재 코드를 그냥 머지
b. Squash and merge = git merge --squash
여러 커밋을 하나의 커밋으로 만들고(squash) 머지
c. Rebase and merge = git rebase && git merge
현재 코드를 base branch 기준으로 rebase 후 머지

2. github.com > 생성한 저장소 > Issue 메뉴
3. Close Issue

 


[local] stash, reset, rebase, cherry-pick 등 사용해보기

1. 로컬 저장소로 복귀
2. 머지가 완료된 feature 브랜치를 로컬 저장소에서 삭제

git branch -d 작업브랜치

 

3. main 브랜치로 이동

git checkout main

 

4. 파일 하나를 임의로 수정함 (README.md이든, 다른 파일이든 기존에 있던 파일이면 아무
거나 상관없음)
5. 원격의 최신 코드를 로컬에 반영

git fetch origin
### 변경되었다는 로그 나온다.
git merge
# or git pull 로 한번에 해결이 가능하다.

6. 아래와 같은 로그가 뜸

Updating 73d9639..9438fd2
error: Your local changes to the following files would be overwritten by merge:
hobby.md
Please commit your changes or stash them before you merge.
Aborting

 

7. git stash 명령어로 작업하던 코드 임시 저장

git stash

 

8. 다시 pull 진행

git pull

 

9. git stash 창고에서 임시 코드 꺼내오기

git stash pop 0

 

10. 충돌이 났으면 해결하고, 그게 아니라면 개발을 마저 진행하면 된다.

 


반응형