[Gitlab] git push, pull 하는 법

김수윤·2023년 10월 25일
0

git을 처음 이용한다면 Git push를 어떻게 해야할지 모를 수 있다. 그렇다면 git의 기초인 git push를 하기 위해서 어떻게 절차를 밟아야 할까?

목차

1. 환경 setting

2. Git Push, Pull 하는법

내용

1. 환경 setting

a. GitBash 설치

https://gitforwindows.org/

b. Repository 생성

프로젝트 생성 시 repository 확인

c. Folder 연결

폴더에서 git bash here 클릭

git bash 커맨드 (필수)

git init  // (git으로 버전을 관리하고 싶은 프로젝트에 깃 저장소를 만든다.)

git remote add origin repository address

git remote -v 목록 확인

2. Git Push, Pull 하는법

  • push는 repository로 자료를 올리는 것
  • pull은 repository로 자료를 당기는 것

push 전 pull 필요

  • git pull origin 브랜치
    repostitory 자료를 가져오는 명령어
  • git status
    로컬 폴더와 git과의 싱크 상태 확인 명령어
  • git add . or git add 파일이름
    변경된 파일 트래킹
  • git commit -m "메시지"
    commit을 생성하는 명령어
  • git push origin 브랜치
    git에 push 요청 명령어

+ git branch 생성

원하는 branch에 push를 하기 위해서는 branch를 생성해야한다.

1. git pull

branch를 생성하기 위해 gitlab project master가 올린 레포를 pull하여 가지와야한다.

git pull "원격 레포 https 주소"

git branch 입력하여 브랜치 목록확인

2. git branch "생성할 branch 이름"

git branch "생성할 branch 이름"

  1. git checkout "branch 이름"
    명령어를 사용하여 branch끼리의 이동 가능
git checkout "branch 이름"
  1. branch에서 작업 후 add, commit

5. git merge

생성한 branch에서의 작업을 모두 커밋했다면 다시 master branch로 돌아와 작업을 진행했던 branch와 병합을 해 주어야 한다.

# master branch로 돌아가기
git checkout master

# 작업을 진행한 branch를 master에 병합하기
git merge "병합할 branch 이름"

# 원격 레포에 push
git push origin "병합한 branch 이름"

6. gitlab Merge Request

이후 gitlab 프로젝트에서 로컬에서 생성한 branch가 생성된 것을 볼 수 있다.

내가 로컬에서 생성하고 작업한 branch의 작업 내용만 커밋하여 push한 것이기 때문에 프로젝트 master의 branch에는 전혀 영향을 주지 않는다. 따라서 협업을 진행할 땐 팀원이 진행한 branch의 내용을 master에 병합을 할 필요가 있다.

이는 gitlab Merge Request를 사용하면 된다.

source branch 에는 내가 merge하려는 레포의 branch(feature)를,

target branch에는 merge 대상인 branch(master)를 선택하여 Compare branches and continue 버튼을 클릭하면 된다.

이후 merge 내용을 작성하고 merge를 승인할 담당자를 지정하면 gitlab 프로젝트 branch merge 요청이 완료된다.

이때 merge를 하면서 내가 merge하려는 레포의 branch(feature)를 삭제를 할지 해당 branch의 commit 로그를 그대로 가져갈지, 통합할지 선택할 수 있는데 이는 협업 특성에 맞게끔 선택을 하면 된다.

+)

유용하게 사용 가능한 명령어

특정 branch만 clone하고 싶을 때

git clone -b "clone할 branch 이름" --single-branch "레포 hppts 주소"

특정 branch만 pull 할 때

git pull origin "pull할 branch 이름"

git fetch하는 법
https://nulab.com/ko/learn/software-development/git-tutorial/git-commands-settings/remote-git-commands/#inspect-branch-changes-in-remote-repository

profile
늦어도 시작하는 사람

0개의 댓글