[Git] Local Repository, Remote Repository와 Branch

Joo·2024년 5월 2일
0

Git

목록 보기
2/5

Local Repository는 Git이 관리하는 3가지 단계로 구성되어 있음

  • Working Directory - 실제 소스 파일, 생성한 파일들이 존재
  • Index (Stage) - Staging area (준비 영역)의 역할, git add한 파일들이 존재
  • HEAD - 최종 확정본, git commit한 파일들이 존재


Working directory에서 Index, 그리고 HEAD로 가는 과정을 보자.

ㄱ. Workspace 생성

% mkdir <workspace name>

ㄴ. Workspace로 이동한 뒤 Working Directory 생성

% cd <workspace name>
<workspace name> % mkdir <working directory>

ㄷ. 디렉토리 초기화

  • 해당 디렉토리에서 Git 초기화 명령어를 사용하면 해당 디렉토리를 Git이 관리하기 시작 (Repository가 생성됨)
git init

그 후, .git 폴더로 이동해 파일을 살펴보면 Git 관련 파일들이 생성된 것을 확인할 수 있음

<working directory> % cd .git
.git % ls -all

워크스페이스를 생성하고 Git 초기화까지 했으니 본격적으로 텍스트 파일을 만들어 보자

ㄹ. 예시 텍스트 파일 만들기 (touch, cat > 사용하기)

touch test.txt
cat > test.txt # 해당 파일이 없다면 이 파일을 만들고, 있다면 내용을 덮어씀
<내용입력> # 이후 ctrl d로 저장한 후 엔터
cat >> test.txt # 해당 파일이 있다면 내용을 덧붙임

방금 만든 파일이 Git에 존재하는지 확인 (뿐만 아니라 내가 입력한 명령어가 문제없이 처리되었는지, 이 다음 뭘 해야하는지 아래 명령어로 확인할 수 있음)

git status

이제 방금 만든 파일을 Index(Stage)에 추가하고 commit 해보자

ㅁ. Index에 추가

git add <filename>

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

git commit -m "어쩌고저쩌고" <filename>

지금까지는 Local repository에서 작업을 수행했음
이걸 Github의 remote repository에 보내보자 (remote repository 만들었다고 가정)

ㅅ. Remote repository에 연결

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

Remote Repository 확인

git remote -v

Remote Repository 연결 삭제

git remote rm <repository>

ㅇ. Local Repository (HEAD)에 반영된 변경내용을 Remote Repository에도 반영하기 위해서는 git push를 사용

git push origin <branchname> # Remote Repository 페이지에서 새로고침하면 Push된 파일이 보임

Remote repository에서 작업을 수행한 후, local repository를 remote repository와 같이 갱신하려면 git pull 사용

git pull origin <branchname>

갱신 후 로컬 파일 내용 변경되었는지 확인

cat <filename>

그리고, Local Repository를 생성하지 않은 상태에서 git clone 명령을 사용해 Remote Repository를 Local에 복제할 수 있음 (자주 사용)

  • 디렉토리 생성 후, git init, remote repository 등록, git pull 과정을 git clone 하나로 가능
git clone https://<username>:<token>github.com/<username>/<repository>.git

전체적으로 보면 디렉토리/파일 생성 → git addgit commit 의 순서 를 따르는 것임

Branch란?

코드 기록을 분리해 개발 및 변경을 관리하기 위한 개념
새로운 기능이나 버그 수정 작업을 할 때 branch를 사용해 다른 팀 구성원들의 작업과 분리할 수 있음

Branch 조회 (local branch)

git branch # local branch
git branch -r # remote branch
git branch -a # local + remote

Branch 생성

git branch <branchname> # local
git push origin <branchname> # remote에 push

Branch 이동

git checkout <branchname>

Branch 생성 + 이동

git checkout -b <branchname>

Branch 삭제

git branch -d <branchname> # local
git push origin --delete <branchname> # remote


참고 1) README file

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

참고 2) .gitignore

  • Git 버전 관리에서 제외할 파일 목록을 지정하는 파일

  • 사용자가 원하지 않는 파일들을 자동으로 commit 대상에서 제외시켜 줌



그냥 리눅스 명령어군...

profile
적당히 공부한 거 정리하는 곳

0개의 댓글