ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • clone과 fork 를 기반으로 git 이해하기
    실전 개발해보기 2023. 8. 29. 18:01

    모 사이트에서 진행하는 과제를 git을 통해 수행하려고했다.

     

    그동안 나혼자 깃에 정리하고 개인프로젝트만 하다보니 fork,clone,pr 할 일이 없어서

     

    이번에 좀 많이 헤맸다.

     

    fork & clone 이 뭐지?

     

    중요한 차이점 '어디서 진행되는지'이다.

     

    Fork 먼저 알아보자.

     

     

    우측에 Fork를 누르면 기존 레포지토리를 복사해서 내 레포지토리에 만들수 있다. 이름도 바꿀 수 있다. 

     

    그러면 이건 어디에 생기는가? 원격 저장소에 생긴다.


    잠깐! Remote(원격 저장소) & Local (나만의 저장소) 

     

     

    Remote 저장소는  Github,Gitlab과 같은 분산 저장소를 의미한다.

    반면 Local 저장소는 내 컴퓨터에서 접근할수 있는 저장소를 의미한다.

     

    위 그림을 보면

    fork는 remote 저장소에서 복사해서 remote저장소에 생긴다. 
    clone은  remote 저장소에서 복사해서 local 저장소에 생긴다. 

    보통은 fork를 통해 나만의 remote저장소가 생겨난 경우에도 개발을 하기 위해서는 local 저장소를 만들어서 진행한다.

    그렇다면 굳이 fork를 해서 개발 과정을 더 만들어내는 이유는?

     

    개발을 어떻게 진행하느냐에 따라 다른 방식을 택한다.

    같은 베이스를 두고 진행하는 개발을 진행하는 경우는 clone을 , 

    새로운 베이스(기존 베이스와 not syncronized) 를 만들어내고 그 뒤에  기존 레포지토리와 합칠것이라면 

    fork를 사용해서 더 큰 구조를 만들어낸다. 

     

    영어 원문을 한국어로 해석하면 더 어색해져서 그대로 가져왔다. 

    When should I fork a repository?

    You will want to fork a repository if you would like to integrate changes to an existing open-source repository.
    In this case, you would create a fork, work on the changes you want to implement, then create a pull request to merge the changes you’ve made from your fork into the original repository.

     

    When should I clone a repository?

    If you would like to make changes directly to a repository you have the permission to contribute to, then cloning will be the first step before we implement the actual changes and push.

    If you don’t have permissions to contribute to the repository, but would like to implement changes anyway, a fork is the way to go.  

     


    Upstream & Downstream 

     

    두 용어는 레포지토리끼리의 관계를 나타내는 용어다. 

     

    최초의 원본과 그를 fork한 것의 관계에서는 , 원본을 Upstream 이라하고 원본을 fork한것을 Downstream 이라 한다.

     

    fork한 repo와 local에 clone한 repo의 관계에서는 , fork한 것을 Upstream이라하고 그를 clone한것을 Downstream이라 한다.

     

    사용예시) git push --set-upstream 

    push 나 pull을 사용할때,

    초기 설정 또는 나중에 git log 확인시 두 repo사이의 관계를 쉽게 확인할수 있기 때문에 사용한다.

     

     


    Fork를 사용하는 과정

    1. '원본 remote repository'(upstream)를 깃허브에서 fork

    2. 'fork한 remote repository'(origin)를 깃 클라이언트로 clone
    3. 기능을 완성할 때까지 반복
         3-1. 'clone한 repository'(local)에 commit
         3-2. local에서 origin으로 push
    4. upstream에 반영하기
         4-1. PR을 등록하기 전 upstream에 바뀐 내용이 없는 경우
              - origin에서 upstream으로 PR(Pull Request)
         4-2. PR을 등록하기 전 upstream에 바뀐 내용이 있는 경우
             - upstream을 local로 pull
             - local에서 origin으로 push
             - origin에서 upstream으로 PR(Pull Request)

     


    마치며

     

    위의 원문에서도 나왔지만, 

    remote repo에 권한이 없다면 fork를 쓰는게 옳다.

    어차피 접근을 못하기에 fork해서 나의 fork repo를 clone해서 개발하고 push&pull을 진행하면 되겠다.

     

    이번 과제를 하며 진행했던 방식이고, 다른 과제들도 대부분 이렇게 진행하고있다.

     

     

     

    Reference

     

    https://www.educative.io/answers/what-is-the-difference-between-forking-and-cloning-in-git

    http://jlord.us/git-it/challenges/forks_and_clones.html

    https://pers0n4.io/github-remote-repository-and-upstream/

    https://www.theserverside.com/answer/Git-fork-vs-clone-Whats-the-difference

Designed by Tistory.