[데이터 취업 스쿨 스터디 노트] 13. Git (Local Repository, Remote Repository, Log and Diff, Merge and Conflict, Tag, README)

WHIT·2024년 6월 11일
0

Data Scientist

목록 보기
15/18

< 수강분량 : Git CH. 1 ~ 10 >

✅ Local Repository

- 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 생성
% cd git_ws
git_ws % mkdir test_project

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

# .git 확인
test_project % ls -all

# 파일 생성
test_project % touch test.txt
test_project % ls
test.txt

# Git status
# Git 에 존재하는 파일 확인
test_project % git status

# Git Add
# Working Directory 에서 변경된 파일을 Index (stage)에 추가
# git add <file name>
test_project % git add test.txt

# Git Commit
# Index (stage) 에 추가된 변경사항을 HEAD 에 반영 (확정)
# git commit -m "commit 에 대한 설명" <filename>
test_project % git commit -m "first commit" test.txt

- Remote Repository 생성

1) GitHub 에서 생성
2) Local Repository 에 연동할 Remote Repository 를 등록 (Token 사용)

# Remote Reposiitory 등록 with Username and Token
git remote add origin https://<username>:<token>@github.com/<repository>.git

# 확인
test_project % git remote -v

- Remote Repository 에 변경내용 Push 하기

# Git Push
# git push origin <branchname>
# master 안되면 main
test_project % git push origin master

- Local Repository 에 Pull 하기

# Git Pull
# git pull origin <branchname>
# master 안되면 main
test_project % git pull origin master

✅ Remote Repository

- Remote Repository 생성

  • GitHub 에서 생성
    • README File : 프로젝트에 대한 설명, 사용방법, 라이센스, 설치방법 등에 대한 내용을 기술하는 파일
    • .gitignore : Git 버전 관리에서 제외할 파일목록을 지정하는 파일

- Default Branch

  • Main or Master?
  • 수정 가능하나, 다른 팀원들까지 영향을 받으니 수정에는 신중!

- Remote Repository 복제하기

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

- Branch

# Branch 조회 (Local Branch)
git branch

# Branch 조회 (Remote Branch)
git branch -r

# Branch 조회 (Local + Remote Branch)
git branch -a

# Branch 생성
git branch <branchname>

# Branch 이동
git checkout <branchname>

# Branch 생성 + 이동
git checkout -b <branchname>

# Branch 생성 (Remote Repository 에 넣기)
git push origin <branchname>

# Branch 삭제 (Local Repository)
# 해당 위치에서는 삭제가 안되니 필요시 다른 branch로 이동
git branch -d <branchname>

# Branch 삭제  (Remote Repository)
git push origin --delete <branchname>

✅ Log and Diff

- Git Log

  • Branch 별 변경이력을 볼 수 있음
git log

- Git Editor 설정

  • --wait 옵션은 command line 으로 VSCode 를 실행시켰을 경우, VSCode 인스턴스를 닫을 때까지 command 를 대기
git config --global core.editor <editorname> --wait

- Git Diff Tool 설정

  • --wait 옵션은 command line 으로 VSCode 를 실행시켰을 경우, VSCode 인스턴스를 닫을 때까지 command 를 대기
# Git Configuration 파일 열기
git config --global -e

# Git Diff 설정 추가
[diff]
	tool = vscode
[difftool "vscode"]
	cmd = "code --wait --diff $LOCAL $REMOTE"

- Git Diff

# Git Diff - Local Branch 간 비교
git diff <branch1> <branch2>
git difftool <branch1> <branch2>

# Git Diff - Commit 간 비교
git diff <commithash> <commithash>
git difftool <commithash> <commithash>

# Git Diff - 마지막 Commit 과 이전 Commit 비교
git diff HEAD HEAD^
git difftool HEAD HEAD^

# Git Diff - 마지막 Commit 과 현재 수정사항 확인
git diff HEAD 
git difftool HEAD 

# Git Diff - Local and Remote 간 비교
git diff <branch> origin/<branch2> 
git difftool <branch> origin/<branch2>  

✅ Merge and Conflict

- Merge

# Git Configuration 파일 열기
git config --global -e

# Git Merge 설정 추가
[merge]
	tool = vscode
[mergetool "vscode"]
	cmd = "code --wait $MERGED"

# Git Merge 
# 현재 위치한 Branch 에 다른 Branch 병합
git merge <branchname>

- Merge Conflict

  • Branch 를 Merge 하는 과정 / 혹은 Push, Pull 하는 과정에서 충돌이 일어날 수 있음
# MergeTool 실행
# Conflict 발생 이후 아래와 같이 MergeTool 을 실행하면 Conflict 난 파일들이 차례로 열림
git mergetool

# Conflict 해제
# git add + git commit
git add <filename>
git commit

✅ Tag

- Tag 란?

  • 특정 버전 (Commit) 에 Tag 를 달아놓을 필요가 있을 때 사용 (예 - 버전 릴리즈)
# 현재 버전에 Tag 달기
git tag <tagname>

# 특정 버전에 Tag 달기
git tag <tagname> <commithash>

# Tag 를 Remote Repository 에 Push
git push origin <tagname>

# Git Tag 목록 보기
git tag

# Git Tag 상세 정보
git show <tagname>

# Git Tag 삭제 (Local)
git tag --delete <tagname>

# Git Tag 삭제 (Remote)
git push --delete origin <tagname>

✅ README

- README 란?

  • 프로젝트에 대한 설명, 사용방법, 라이센스, 설치법과 같은 부분에 대해 기술하는 파일
  • 나, 직장동료, 프로그램 사용자를 위해 존재
  • Markdown 문법 활용

"이 글은 제로베이스 데이터 취업 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다."

0개의 댓글