fork
특정 repository를 복제하여 자신의 repository에 가져오는 것
- 어떤 상황에 사용하는가? 원본 프로젝트(repository)를 fork해서 소스 코드를 자유롭게 수정하여 자신만의 버전을 개발하거나 feature를 추가하고 싶을 때 사용
- 협업하는 상황에서 빈번하게 사용됨
fork를 활용한 협업 과정
- fork: 기여자는 프로젝트 소유자의 repository에 직접 접근할 권한이 없기 때문에, fork를 통해 프로젝트를 자신의 repository로 가져옴
- 수정 및 feature 추가: fork한 repository를 local로 clone하고, 수정 및 feature 추가
- pull request (PR): 변경 사항을 원본 repository에 변경 사항을 요청
fork 실습
1. 원본 repository에서 fork 하고, local repository로 clone하기
- “git remote -v”으로 현재 연결된 repository의 URL과 이름을 확인할 수 있음
$ git clone <https://~~>
$ git remote -v
2. 다른 repository 등록하기
- “git remote add” 명령으로 upstream을 추가, 그러면 repository는 origin, upstream 두개가 연결됨
- “git pull origin/upstream”으로 repository 이름 변경해가면서 사용할 수 있음
$ git remote add <원격 저장소 이름> <원격 저장소 URL>
$ git remote add upstream <https://~~>
3. upstream에서 변경사항을 받아옴
$ git pull upstream main
4. origin에도 변경사항 반영
git push origin main
branch 생성하고 전환하기
1. 현재 repository의 branch 확인
- “git branch” 명령으로 repository에 있는 branch들을 확인
- 현재 main이라는 branch만 생성됨
$ git branch

2. branch 생성하기
- branch는 현재 위치를 기준으로 생성됨
- branch를 생성하면 local repository에서만 생성됨 → remote repository에는 생성한 branch가 없음
- branch를 생성하는 방법은 두가지가 있음, 차이는 아래와 같음
$ git branch develop # develop이라는 branch를 생성하고, 해당 branch로 이동하지 않음
$ git checkout -b develop # develop이라는 branch를 생성하고, 바로 해당 branch로 이동
- 여기서는 “git checkout -b develop”으로 branch를 생성함

- “git branch”로 생성된 branch 확인해보면 develop이 추가됨

3. branch 전환하기
- “git switch”으로 branch를 전환할 수 있음
$ git switch <branch 이름>
- branch를 main으로 전환하고, 현재 branch 확인하면 다음과 같음

git branch에서 변경사항 push 하기
1. main branch에서 작업하고 push 하기
- main branch에서 main.txt 생성하고, commit하기
$ echo "main branch change" >> main.txt
$ git add main.txt
$ git commit -m "add main.txt"
- main branch로 commit push
$ git push origin main
2. develop branch에서 작업하고 push 하기
- develop branch로 전환
$ git switch develop
- 현재 디렉터리에 존재하는 파일 확인해보면, main branch에서 생성했던 main.txt가 존재하지 않음

- develop branch에서 develop.txt 생성하고, commit하기
$ echo "develop branch change" >> develop.txt
$ git add develop.txt
$ git commit -m "add develop.txt"
- develop branch로 commit push
$ git push origin develop
- github에서 insight → network 확인해보면, main branch에서 develop branch가 새로 생성됨

branch 병합하기
main branch에 있는 거를 develop branch에 반영해보고 문제가 없으면, main branch에 병합하는 것이 이상적 → 서비스 안정성을 위해
1. develop branch에서 merge
- 병합할 대상 branch인 develop으로 전환하고, main branch의 내용을 병합
$ git switch develop
$ git merge main
- 그러면 다음과 같이 vi 편집기가 열리는데, commit 메시지 입력하고 “:wq!”로 저장하고 나가기

- develop branch에 main branch의 main.txt가 병합됨

- remote pository의 develop branch에 push
$ git push origin develop
- github에서 insight → network 확인해보면, develop branch에 main branch가 병합됨

2. main branch에서 merge
- 이번에는 main branch로 전환하고, develop branch의 내용을 병합
$ git switch main
$ git merge develop
- merge하면 별다른 메시지가 안뜸 → 변경사항 없이 Fast-forword 하기만 하면 되서
- Fast-forward란? 두 branch 사이의 변경 사항을 병합할 때, 중간에 새로운 commit이 없어 branch 포인터만 이동시키는 병합 방식

- remote pository의 main branch에 push
$ git push origin main
- network graph 확인해보면, main branch와 develop branch가 같은 지점을 바라보게 됨

'DevOps > Git' 카테고리의 다른 글
[Git] Github Actions으로 투표 앱 EC2에 배포하기 (0) | 2024.12.29 |
---|---|
[Git] GitHub Actions 사용해보기 (0) | 2024.12.29 |
[Git] local repository와 remote repository의 변경사항이 충돌하는 경우 (0) | 2024.11.19 |
[Git] Git 설치하기 & repository 사용하기 (0) | 2024.11.19 |
[Git] Git 기본 개념과 용어 (0) | 2024.11.17 |