Tool/Git

Git fork & Pull request

heyoon2j 2020. 10. 13. 13:10

Git fork & Pull requset 

Fork vs Clone 차이

Fork

- 하나의 Remote Repository를 복사한다. 변경 권한이 없어 기존 Remote Repository에 영향을 주지 못한다.

- Pull Request를 통해 변경을 요청하게 된다.

 

Clone

- Remote Repository를 Local Repository로 복사한다.

- 권한이 없으면 push를 하지 못한다.


Pull request 순서

 

1. Project를 자신의 Github로 Fork 한다.

 

- Github에 해당 Project Repository에 들어가 Fork를 누르면 된다.

- 그렇게 되면 자신의 Repository를 확인하면, 복사가 된 것을 확인할 수 있다.

 


2. Fork 한 Repostiry를 자신의 Local Repository에 복사한다.

 

- Code 버튼을 클릭하면 Clone 할 수 있는 주소가 나온다.

- 해당 주소는 복사한다.

 

git clone https://github.com/heyoon2j/maze-game.git

- 자신의 터미털을 켜고, 다음과 같이 명령어를 입력한다(Windows를 쓰시는 분은 Git bash와 같은 터미널 프로그램을 설치하는 것을 추천한다)


3. branch 생성, 이동

- 기존 코드에 영향을 주지 않고, 독립적으로 개발을 진행하기 위해서는 branch가 필요하다.

 

 

# git checkout -b <branch Name>
$ git checkout -b dev

$ git branch -a

- $ git checkout -b dev : dev라는 branch를 생성하고, 해당 branch로 이동

- $ git branch -a : 모든 branch의 리스트를 출력

 

* 만약 Fork를 통해 기존에 생성되어 있는 branch로 이동하려면, 위의 checkout 명령어에서 "-b" 옵션을 제거하면 된다.

 


4. 개발 진행 후

 

1) Add, Commit 작업을 통해 Local Repository에 저장한다.

$ git add README.md

$ git commit -m "docs: Initialization README.md"

 

2) 원하는 Remote Repository에 Push 작업을 한다.

# 연결된 Repository list 출력
$ git remote -v

# git push [Remote Repository Name] [Branch Name]
$ git push origin dev

- $ git push [Repository Name] [Branch] : 해당 Repository의 Branch에 Push 한다.


5. Pull request 보내기

- push 후에 자신의 github 저장소에 Compare & pull request 버튼이 활성화되어 있는 것을 확인할 수 있다.

- 버튼을 클릭하고, Pull request를 생성한다.


6. Code Review 및 Merge pull request

- Pull Request를 받은 Project 관리자가 Merge를 할지 말지 결정한다.


7. 동기화

- Project는 여러 사람이 계속 업데이트를 하기 때문에 작업 후에 최신 버전으로 업데이트할 필요가 있다.

 

1) 최신 업데이트를 하기 위해 실제 Project Repository를 Remote Repository를 추가한다.

$ git remote add upstream https://github.com/heyoon2j/maze-game.git

$ git remote -v
origin	https://github.com/theVebper/maze-game.git (fetch)
origin	https://github.com/theVebper/maze-game.git (push)
upstream	https://github.com/heyoon2j/maze-game.git (fetch)
upstream	https://github.com/heyoon2j/maze-game.git (push)

- $ git remote add [Repository Name] [Remote Repository Address] : 해당 주소의 Remote Repositoy를 Repository Name으로 추가한다.

- 리스트를 확인하면 추가되어 있는 것을 확인할 수 있다.

 

 

2) upstream repository로부터 최신 데이터를 업데이트한다.

$ git fetch upstream
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
오브젝트 묶음 푸는 중: 100% (3/3), 1.34 KiB | 686.00 KiB/s, 완료.
https://github.com/heyoon2j/maze-game URL에서
 * [새로운 브랜치]   main       -> upstream/main

- $ git fetch [Repository Name] : 해당 Name을 가진 Repository를 업데이트한다.

 

 

3) 업데이트한 최신 데이터와 자신의 branch와 병합한다.

$ git checkout main

- 먼저 병합할 branch로 이동한다.

- $ git checkout [Branch Name] : 해당 branch로 이동한다.

 

$ git merge upstream/main
업데이트 중 a7894a5..f53f3b7
Fast-forward
 README.md | 76 ++++++++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 51 insertions(+), 25 deletions(-)

- $ git merge [Branch Name] : 현재 Branch와 입력한 Branch와 병합한다.

 

 

* 2)과 3)번을 한 번에 할 수 있다.

 $ git pull [Remote Repository Name]
 # Remote Repository의 fetch 후 merge 한다.

 - $ git pull [Remote Repository Name] [Branch]: Remote Repository의 Branch를 현재 Branch로 가지고 온다.

 

 

 

 

 

참고 사이트

* https://milooy.wordpress.com/2017/06/21/working-together-with-github-tutorial/

* velog.io/@hyunju-song/202010-%ED%94%84%EB%A1%9C%EC%A0%9D%ED%8A%B8git-workflow

* velog.io/@imacoolgirlyo/Git-fork%EC%99%80-clone-%EC%9D%98-%EC%B0%A8%EC%9D%B4%EC%A0%90-5sjuhwfzgp 

* wayhome25.github.io/git/2017/07/08/git-first-pull-request-story/

'Tool > Git' 카테고리의 다른 글

Git 사용 방법  (0) 2019.09.19