[git] repository로 협업하기 - push, pull, fetch, merge, pull request, rebase

건너별·2022년 7월 7일
0

git

목록 보기
4/4
post-thumbnail

이 글은 인프런 강의 git 빠르게 입문하기를 참고하여 작성된 글입니다.

Local Repo <-> Remote Repo


[https://www.javatpoint.com/git-push]

  • 이제 repository끼리의 상호작용이다!

git remote : Remote Repo 조회하기

  • 내 local repo 와 상호작용하고 있는 원격 저장소 목록 조회
  • -v 추가 시 url 도 함께 조회
git remote add <단축된 이름> <url>
  • 기존 Working Directory에 새 Remote Repo 추가하는 명령어
  • <단축된 이름> 은 origin 을 주로 사용함

git push : Remote Repo 밀어넣기

git push -u origin main

FROM : local repo의 main branch로부터
TO : origin 이라는 이름의 remote repo의 main branch로
push
-u : 현재 명령어를 향후 push/pull/fetch 진행시 default 로 설정함.(origin 과 main 간의 상호작용)

git pull : Remote Repo 갖고 와서 합치기

git pull (origin main)

FROM : origin 이라는 이름의 remote repo로부터
TO : local repo의 main branch로 갖고와서 동기화까지 하자!

git fetch : Remote Repo 일단 갖고만 오기

git fetch (origin main)

FROM : origin 이라는 이름의 remote repo를
TO : local repo의 main branch로 갖고오기만 해!

fetch는 local repo에 반영되지는 않는다.
그럼 어떻게 확인할까?

git checkout origin/main # git branch로는 확인 안됨
# 또믄
git checkout FETCH_HEAD

여기로 브랜치 옮기면 변화를 볼수있다!

git clone : Remote Repo 복사하기

  • 생략

협업 매뉴얼!

  1. local repo 변했고, remote repo 그대로인 경우

-> push

2. local repo 그대로인데, remote repo 변한 경우
-> pull로 동기화 후 push

3. local repo 변했고, remote repo도 변한 경우

1) pull request

  1. fork
  2. clone
  3. branch 생성
  4. add, commit, push
  5. PR 보내기
  6. branch 지우기(자동으로 되는듯)

2) rebase

i) 3-way merge의 문제점

위와 같이
1. master branch에서C2로부터의 commit이 진행되었고(C3)
2. experiment branch에서 C4로의 commit이 진행됨

  • 이에 git merge로 두 커밋을 합친 결과인 merge commit이 생김

  • 과정 자체는 합리적이나 history 관리가 안되어 어떤 commit을 되돌릴 수 있을지 알수 없음

  • 현재 작업중인 branch의 base를 옮긴다

ii) 그래서 rebase!

  • 현재 내가 작업하고 있는 branch의 base를 옮긴다
  • 병합하고자 하는 branch의 최신 커밋으로 옮겨지게 됨
  • 다시말해, 기존의 base가 병합하고자 하는 branch의 최신 커밋으로 옮겨진다

$ git checkout experiment
$ git rebase master
  • 기존의 base인 C2C3 로 옮겨졌다!

  • 이제 master branch로 옮기고, merge 하면 된다!

  • history가 그대로 남아 유용함!

$ git checkout master
$ git merge experiment

여기서 base란?

  • 현재 내가 작업하고 있는 branch합치려는 branch의 공통조상
profile
romantic ai developer

0개의 댓글