[제로베이스 데이터 취업 스쿨] 9기 11주차 - Git (5): Remote Repository

Inhee Kim·2023년 1월 11일
0
post-thumbnail

Remote Repository

1. Remote Repository 생성

Git Home 에서 Repositoreis 'New' 버튼 선택

HelloGit 생성

  • README File & .gitignore (python) 선택 > Create repository
    -HelloGit 생성 확인

참고
README File

  • 프로젝트에 대한 설명, 사용방법, 라이센스, 설치방법 등에 대한 내용을 기술하는 파일
  • 나, 직장동료, 프로그램 사용자를 위해 존재

.gitignore

  • Git 버전 관리에서 제외할 파일목록을 지정하는 파일
  • 사용자가 원하지 않는 파일들을 자동으로 commit 대상에서 제외시켜 줌

2. Default Branch

Main or Master?

  • local에서 remote로 push하면 Master
    (Git의 default는 Master)
  • remote에서 repository를 만들면 Main
    (GitHub의 default는 Main)

View all branches

Default branch

  • 수정 가능

  • 수정을 하면 다른 팀원들까지 영향을 받기 때문에 수정은 신중해야 함.

  • master로 변경

Default Branch 설정

  • Remote Repository를 생성할 때 Default Branch 이름을 설정할 수 있음.

  • 사용자 메뉴 > Settings

  • Repository > Repository default branch
    (default를 master로 변경)

3. Remote Repository 복제하기

Local Repository 를 생성하지 않은 상태에서 Git Clone 명령을 사용하여 Remote Repository를 Local에 복제할 수 있음

Git Clone
앞서 폴더를 만들고

  • Git Init 으로 해당 폴더를 초기화 하고
  • Remote Repository 를 등록하고
  • Remote Repository 의 내용을 Pull 하는 모든 과정을 Git Clone 으로 할수 있음
git clone https://github.com/<repository>.git

Git Clone with username and token

git clone https://<username>:<token>@github.com/<repository>.git

Git Clone 실습

  • Remote Repository 주소 복사
  • Workspace 로 이동하여 Clone
cd Documents
cd git_ws

git clone https://<username>:<token>@github.com/<repository>.git

  • Local Repository 생성 확인
ls
cd HelloGit
ls -all
cat README.md

4. Branch

(1) 조회

Local
Branch 조회 (Local Branch)

git branch

Remote
Branch 조회 (Remote Branch)

git branch -r

Local + Remote
Branch 조회 (Local + Remote)

git branch -a

(2) 생성

Local
Branch 생성

# 문법
git branch <branchname>

Branch 생성 실습

git branch branch01
git branch

(3) 이동

Local
Branch 이동

# 문법
git checkout <branchname>

Branch 이동 실습

git checkout branch01
git branch

(4) 생성 + 이동

Local
Branch 생성 + 이동

# 문법
git checkout -b <branchname>

Branch 생성 + 이동 실습

git checkout -b branch02
git branch

(5) remote로 push

GitHub에서 Branch 확인
Local Repository 에 생성된 Branch는 Remote Repository (GitHub)에서 보이지않음

Remote
Branch 생성

# 문법
git push origin <branchname>

Branch 생성 (Remote Repository) 실습 - 1

git push origin branch01
git branch -a

Branch 생성 (Remote Repository) 실습 - 2

git push origin branch02
git branch -a

(6) 삭제

Local
Branch 삭제 (Local Repository)

# 문법
git branch -d <branchname>

Branch 삭제 (Local Repository) 실습 - 1
(error: 현재 branch02에 머물러 있는 상태이기 때문에 삭제할 수 없음.
따라서 master로 이동해야 함.)

git branch -d branch02
git checkout master

Branch 삭제 (Local Repository) 실습 - 2

git branch -d branch02
git branch -a

Branch 삭제 (Local Repository) 실습 - 3

git branch -d branch01
git branch -a

Remote
Branch 삭제 (Remote Repository)

# 문법
git push origin --delete <branchname>

Branch 삭제 (Remote Repository) 실습 - 1

git push origin --delete branch02
git branch -a

Branch 삭제 (Remote Repository) 실습 - 2

git push origin --delete branch01
git branch -a

문제풀이

1. Remote Repository 생성하기

  • 이름 : branch_project
  • Option : README.md, .gitignore (Python)

2. Local에 Clone

  • 위치 : git_ws 하위
  • 생성 확인
cd Documents/git_ws
git clone https://<username>:<token>@github.com/<repository>.git

ls
cd branch_project
ls -all

3. Branch 생성 후 이동

  • 이름 : branch01, branch02
  • 이동 : branch01
  • 확인 : Local Branch 목록 (현재 Branch 위치 - branch01)
# branch01 생성
git branch branch01
git branch

# branch02 생성 후 이동
git checkout -b branch02
git branch

# branch02로 이동
git checkout branch01
git branch

4. Branch Push

  • Push : branch01, branch02
  • 확인 : Remote Branch 목록, GitHub
git branch -a
git push origin branch01
git branch -a

git push origin branch02
git branch -a

5. Local Branch 삭제

  • Local Branch 삭제 : branch01
  • 확인 : Local Branch 목록 (에러상황 처리 포함)
git branch -d branch01

# master로 이동 후 branch01 삭제
git checkout master
git branch -d branch01

git branch -a
git branch

6. 남은 Local Branch 모두 삭제

  • main branch 제외
  • 확인 : 전체 Branch 목록
git branch -d branch02
git branch -a

7.Remote Branch 모두 삭제

  • master, HEAD 제외
  • 확인 : 전체 Branch 목록, GitHub
git push origin --delete branch01
git branch -a

git push origin --delete branch02
git branch -a

profile
Date Scientist & Data Analyst

0개의 댓글