Git - Local Repository, Remote Repository

Yang HyunIl·2023년 2월 20일
0

Git

목록 보기
2/3
post-thumbnail

🔴Local Repository 구성

Local Repository 는 Git 이 관리하는 3가지 단계로 구성되어 있음
• Working Directory (작업공간) - 실제 소스 파일, 생성한 파일들이 존재
• Index (Stage) - Staging area (준비영역) 의 역할, git add 한 파일들이 존재
• HEAD - 최종 확정본, git commit 한 파일들이 존재

Local Repository 생성

Workspace 생성

% mkdir git_ws

Working Directory 생성

Workspace로 이동한 뒤 Working Directory 생성

% cd git_ws
git_ws % mkdir test_project

Git init

폴더에서 Git을 초기화하는 명령어를 사용하면 해당 폴더를 Git이 관리하기 시작

git init

.git 확인

.git 폴더가 생성된 것을 확인 → .git 으로 이동해서 파일을 살펴보면 Git 관련 파일들이 생성된 것을 확인

파일 생성

Working Directory 에 파일을 생성
참고> touch 명령어 : 빈 파일을 생성

test_project % touch test.txt 
test_project % ls 
test.txt

Git Status

Git 에 존재하는 파일 확인

git status

Git Add

Working Directory 에서 변경된 파일을 Index (stage)에 추가

git add <filename>

Git Commit

Index (stage) 에 추가된 변경사항을 HEAD 에 반영 (확정)

git commit -m "commit 에 대한 설명" <filename>

🔴Remote Repository 생성

Create repository

프로젝트 이름을 설정한 뒤 빈 프로젝트 상태로 > Create repository 클릭

프로젝트 이름을 설정한 뒤 빈 프로젝트 상태로 > Create repository 클릭

Remote Repository 생성 확인

Github Token 생성

보안상의 이유로 Remote Repository 접속 시 비밀번호 대신 Token을 사용

사용자 메뉴에서 Settings 선택

Developer settings 선택

Personal access tokens 선택

Generate new token 선택

Token 이름 입력 + No expiration 을 선택 + repo 선택 > Generate token 버튼 선택

Token 생성 완료 이후에는 Token 값이 보이지 않으므로 잘 보관

Remote Repository 등록

Local Repository 에 연동할 Remote Repository 를 등록 (Token 사용)

Remote Repository 주소 확인

왼쪽 상단 고양이를 눌러서 GitHub Home 으로 이동 > 생성해둔 Remote Repository 선택

HTTPS 선택 확인 > 주소 복사

Local Repository에 Remote Repository 등록

  • Remote Repository 등록
git remote add origin https://github.com/<repository>.git

Remote Repository 등록 with Username and Token

git remote add origin https://<username>:<token>@github.com/<repository>.git

Remote Repository 정보 확인

git remote -v

Remote Repository에 변경내용 Push

  • Local Repository (HEAD)에 반영된 변경내용을 Remote Repository에도 반영하기 위해 Git Push를 사용

Git Push

git push origin <branchname>

Remote Repository 확인

Remote Repository 페이지에서 새로고침 하면 Push 된 파일이 보임

Remote Repository에 Pull 하기

ReadMe 파일 생성

  • Add a README

내용 확인 (모두 Default 설정으로)

하단의 Commit new file 버튼 선택

README 파일 생성 확인

Git Pull

git pull origin <branchname>

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

Branch 조회

Local Branch

git branch

Remote Branch

git branch -r

Local + Remote

git branch -a

Branch 생성

git branch <branchname>

Branch 이동

git checkout <branchname>

Branch 생성 + 이동

git checkout -b <branchname>

Branch 삭제

Local Repository

git branch -d <branchname>

Remote Repository

git push origin --delete <branchname>
profile
ヾ(•ω•`)o

0개의 댓글

관련 채용 정보