Git 내용 정리

TaeHwi Kang·2022년 11월 18일
0

1. Git 기본 설정

1) Git Global Configuration

  • Global Username & Email
git config --global user.name <username>
git config --global user.email <email>

2) Windows - 가져올 때는 LF를 CRLF로 변경하고 보낼때는 CRLF를 LF로 변경

git config --global core.autocrlf true

#### 참고 - Line ending 
• Windows : CR (\r) + LF (\n) 
• Unix or Mac : LF (\n) 
• Windows 사용자와 Mac 사용자가 같은 Git Repository 를 작업할 때 코드에서 
  변경된 내용이 없어도 CRLF 차이로 인해 commit 이 발생할 수 있다

3) 전체 설정 확인

git config --list

2. Git 기본 용어

1) Repository

  • 소스코드가 저장되어 있는 여러 개의 Branch 가 모여있는 디스크상의 물리적 공간
  • Local Repository와 Remote Repository 로 구분

2) Checkout

  • 특정 시점이나 Branch 의 소스코드로 이동하는 것을 의미
  • Checkout 대상 : Branch, Commit, Tag
  • Checkout을 통해 과거 여러 시점의 코드로 이동이 가능

3) Stage

  • 작업할 내용이 올라가는 임시저장영역
  • 이 영역을 이용하여 작업한 내용중 commit에 반영할 파일만 선별하여 commit을 수행할 수 있다

4) Commit

  • 작업할 내용을 Local Repository에 저장하는 과정
  • 각각의 commit은 의미있는 변경단위, 변경에 대한 설명을 commit log로 남긴다
  • (권장) commit을 자주하기.
  • (참고) commit 단위 또는 commit log format을 정해놓은 회사나 팀도 있다 (빌드 서버를 사용하는 경우)

5) Tag

  • 임의의 commit 위치에 쉽게 찾아갈 수 있도록 붙여놓은 이정표
  • Tag가 붙은 commit은 commit id (version) 대신 tag name 으로 쉽게 checkout 가능

6) Push

  • Local Repository의 내용 중, Remote Repository에 반영되지 않은 commit을 Remote Repository로 보내는 과정
  • (권장) Push 하는 순간 다른 개발자들도 영향을 받기 때문에 검증되지 않은 코드는 Push 하지 않도록 해야한다.

7) Pull

  • Remote Repository에 있는 내용 중, Local Repository에 반영되지 않은 내용을 가져와서 Local Repository에 저장하는 과정
  • 다른 팀원이 변경하고 Push 한 내용을 Local Repository에 가져올 수 있다
  • (참고) Push 과정에서 Conflict(충돌)이 일어나서 Push가 거절된 경우, Pull을 통해 Remote Repository의 변경 내용을 Local Repository에 반영하여 Conflict를 해결 한뒤 다시 Push를 시도해야 한다.

8) Branch

  • 특정 시점 (commit 단위) 에서 분기하여 새로운 commit 을 쌓을수 있는 가지를 만드는 것
  • 개발의 주축이 되는 branch 를 master branch (main branch) 라고 한다
  • 모든 branch 는 최종적으로 다시 master branch에 merge (병합) 되는 형식으로 진행된다

9) Merge

  • Branch의 반대개념으로 하나의 Branch를 다른 Branch 와 합치는 과정
  • Merge 되는 두 Branch는 주종관계가 성립.
  • Merge 되는 과정에서 Conflict (충돌)이 발생하는 경우 Diff를 수정하여 Conflict를 해결한 뒤 Merge를 진행 할 수 있다

3. Local Repository 구성

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

1) Git init

  • Working Directory 생성 후 Git을 초기화하는 명령어를 사용하면 해당 폴더를 Git 이 관리하기 시작
git init

2) Git Status

  • Git에 존재하는 파일 확인
git status

3) Git Add

git add <filename>

4) Git Commit

  • Index (stage)에 추가된 변경사항을 HEAD에 반영 (확정)
git commit -m "commit 에 대한 설명" <filename> 

4. Remote Repository

1) Local Repository에 Remote Repository 등록

# Github에서 Remote Repository 생성, token 생성

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

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

git push origin <branchname>

3) Remote Repository의 내용에 맞춰 Local Repository를 갱신하려면 Git Pull 사용

git pull origin <branchname>

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

git clone https://github.com/<repository>.git

# Git Clone 
- 폴더를 만들고
+ Git Init 으로 해당 폴더를 초기화
+ Remote Repository 를 등록
+ Remote Repository 의 내용을 Pull 하는 모든 과정을 Git Clone 으로 할수 있음


# Git Clone with username and token
git clone https://<username>:<token>@github.com/<repository>.git

4. Branch

1) Branch 조회 (Local Branch)

git branch

2) Branch 조회 (Remote Branch)

git branch -r

3) Branch 조회 (Local + Remote)

git branch -a

4) Branch 생성

git branch <branchname>

5) Branch 이동

git checkout <branchname>

6) Branch 생성 + 이동

git checkout -b <branchname>

7) Branch 삭제 (Local Repository)

git branch -d <branchname>

8) Local Repository에서 변경된 Branch는 Remote Repository (GitHub)에서 적용되지않음

# 생성된 Branch Remote Repository 적용
git push origin <branchname> 으로 적용

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

5. Git Diff

1) Git Diff (Local Branch 간 비교)

git diff <branch1> <branch2>

2) Git Diff (Commit 간 비교)

git diff <commithash> <commithash>

3) Git Diff (마지막 Commit 과 이전 Commit 비교)

git diff HEAD HEAD^

4) Git Diff (마지막 Commit 과 현재 수정사항 확인)

git diff HEAD

5) Git Diff (Local and Remote 간 비교)

git diff <branch> origin/<branch2>

6. Merge

1) Git Merge

  • 현재 위치한 Branch 에 다른 Branch 를 병합
git merge <branchname>

2) Merge Conflict

-Branch 를 Merge 하는 과정에서 충돌이 날 수 있으며 혹은 Push, Pull 하는 과정에서도 충돌이 일어날 수 있다

# MergeTool 실행
Conflict 발생 이후 아래와 같이 MergeTool 을 실행하면 Conflict 난 파일들이 차례로 열린다
git mergetool

# Conflict 해제 후 
git add + git commit

7. Tag

1) Tag 생성

git tag <tagname>

2) Tag 확인

git tag

3) 특정 버전에 Tag 달기

git tag <tagname> <commithash>

4) Tag 를 Remote Repository 에 Push

git push origin <tagname>

5) Git Tag 목록 보기

git tag

6) Git Tag 상세 정보

git show <tagname> 

7) Git Tag 삭제 (Local Repository)

git tag --delete <tagname>

8) Git Tag 삭제 (Remote Repository)

git push --delete origin <tagname>
profile
스터디 노트

0개의 댓글