[ZB] GIT

porii·2024년 9월 23일

[edu] zerobase

목록 보기
3/28

1. Version Control System

  • 형상 관리 시스템 ( Configuration Management Systems (CMS))
  • 버전 관리 시스템 ( Version Control Systems (VCS))
  • Source Data + History(이력)
  • 협업, 작업추적, 복구 등 가능
  1. Local Version Control Systems
    • 내 컴퓨터에서 관리
  2. Centralized Version Con trol Systems
    • 중앙 관리
    • 제한적 협업 가능
    • CVS
      • 1980년대
      • commit 중 오류 발생 시 rollback 안됨
    • SVN
      • 2000년대 ~ 현재
  3. Distrivuted Version Control Systems - 현재
    • Commit 하더라도 개인 저장소 내에 적용
    • 원하는 때에 배포 가능
    • 개인적 version history 가능
    • Git ( Global Information Tracker)
      • SVN보다 빠른 속도, 다기능 지원

2. Git

  1. 데이터 저장방식
    • 파일 저장 순간의 스냅샷을 저장
    • 변경사항 없으면 → 새로 저장 X
  2. 구성 요소
    1. Working Directory
      보이는 곳
    2. Staging Area
      이 단계의 내용을 Commit → 버전이 생김
    3. Git Directory
  3. 파일 상태
    • Untracked : Git에 아무것도 등록되지 않은 상태
      • Working Directory에 파일 생성만 했을 때
      • AddFile 하면 Tracked 상태로
    • Tracked
      • Staged / Committed / Modified
      • Staged — COMMIT —> Commited (버전O) — EDIT —> Modified — Stage File forCommit (ADD) —> Staged . — DELETE —>
  4. 작업 순서
    1. working directory에서 파일 수정
    2. Add 하여 staging area에 파일을 staged → commit 할 스냅샷 만듦
    3. staging area에 있는 파일들을 commit 하여 .git Directory에 스냅샷 저장 (버전이 매겨짐)

3. 설정

  1. 설정 범위

    • git config —system : (시스템범위) 시스템의 모든 사용자와 모든 저장소에 적용
    • git config —global : (사용자별) 시스템의 특정 사용자에게 적용
    • git config —local : (저장소별) 특정 저장소에 적용
  2. 사용자 정보 - global 옵션
    git config user.name <user_name>
    git config user.email <email_address>

  3. CFLF ( End of Line )
    git config core.autocrif <value>
    라인의 끝을 표기하는 문자 ( 줄바꿈 문자 )

    • CF (\r) + LF (\n) - window
    • LF (\n) - unix or mac
    • 여러 사용자가 다른 OS에서 Git으로 작업한다면 core.autocrlf = true 사용
      • core.autocrif = false : 파일의 줄바꿈 문자와 상관없이 작업 환경의 줄바꿈 문자 적용
      • core.autocrif = true : 파일에 적용된 줄바꿈 문자 CRLF → LF 변경
      • core.autocrif = input : Commit 할 때만 CRLF → LF 변경
  4. Editor
    git config core.editor <deitor_name>

  5. Default Branch
    git config init.defaultBranch <branch_name>

    • window : 설치 시 설정
    • mac : 설치 시 컴퓨터상 GitHub Branch명 master로 설정됨
    • Clound상 GitHub : default명 - main
    • branch명 충돌 해결
  6. 전체 설정 확인
    git config --l(영어 소문자 L)
    git config —list

    • 범위별 설정 확인
      git config --l --show-origin
      git config --list --show-origin
    • 항목별 설정 확인
      git config <key>
  7. Git Repository
    Git으로 관리하는 프로젝트 저장소
    파일과 디렉토리 포함 가능. 버전관리 시작 가능

    • Local Repository : 사용자의 컴퓨터 저장소
    • Remote Repository - 원격서버 저장소

    방법

    1. git init 프로젝트 폴더를 git 저장소로 만들어

      1. 폴더를 만들어
      2. 폴더 내부에서 git init
      3. ~.git이라는 폴더가 생겨남 = repository가 됨
    2. git clone remote 저장소를 local에 복사하여 사용

      git clone <remote_repository_url>
      # https://github.com/<organication_name>/<project_name>.git

      1. git clone
      2. <project_name>.git 의 폴더 생성
      3. 폴더 내부에서 git init
      4. remote의 파일을 가지고 옴
      • git clone with Token Local-Remote 의 동기화 작업 시 권한 확인 생략해줌
        git clone https://**<token>@**github.com/<user_name>/<project_name>.git

4. 문법

★★★ git statusWorking Directory, Staging Area 상태 표시
파일 상태 확인 시
git add <file_name>- Working Directory → Staging Area(Index) 추가
- Modified → Staged
파일을 Commit 대상으로 바꿀 때
★★★
git commit -m <commit_message>
git commit -a -m <commit_message>
= git commit -m <message> <파일명>
<commit_message> - 통해서 version 찾음, 의미있는 문구 작성, 되도록 영문
-a 옵션 : add + commit ( Modified → Committed )
git ignoreGit에서 무시해야 하는 파일패턴 관리
git rm <file_name>Git에서 관리하는 파일 삭제 (Staging Area의 파일삭제 + commit )
- Working Directory에서 삭제됨
rm <file_name>Git 말고 Working Directory에서 바로 삭제 → unstaged 상태
→→ git rm +commit 해줘
git mv <old_name> <new_name>
=
cp <old_name> <new_name>
git rm <old_name>
git add <new_name>
Git에서 파일이름(경로 포함) 변경
=
파일 복사
원본 파일 삭제
Git Add
Git Log 문법
git logcommit history 나옴
git log -2최근 2개의 commit history
git log —skip 5가장 앞 5개의 commit을 스킵 후 보여줘
git log -pdiff 포함해서 출력
git log —onelinelogID(앞7자리) + message 만 각각 한 줄로 출력
git log —author=<user_name or user_email>commit한 사용자로 검색
git log -S <파일변경내용>파일 변경내용으로 검색
git log --grep <commit message>commit message로 검색
  • Git Ignore
    • *.a 확장자가 .a 인 파일 무시
    • !lib.a 위에서 .a 확장자 파일 무시 했지만 lib.a는 무시하지 X
    • /TODO 현재 디렉토리에 있는 TODO 파일무시, subdir/TODO처럼 하위디렉토리의 파일은 무시X
    • build/ build/디렉토리의 모든 파일 무시
    • doc/*.txt doc/notes.txt 파일 무시, doc/server/arch.txt 파일 무시 X
    • doc/**/*.pdf doc디렉토리 아래모든 .pdf 파일 무시
  • 무시할 항목 — .gitignore 파일에 추가
파일 수정 문법
touch 파일명untracked file 생성
git add *조심해 !!!!! 사용하지마
cat 파일명파일 내용 출력
cat > 파일명 + ctrl D아래 타이핑 한 내용으로 파일을 덮어 써
cat >> 파일명+ ctrl D아래 타이핑 한 내용을 파일에 추가해줘
  • Add 후 바로 Commit ⇒ Unstaged하면서 Staged한 파일
    • commit → staged 상태만 변경. unstaged 는 그대로
    • 추가로 add - commit → unstaged 상태 해결
  • 파일 삭제 또한 변경사항으로 인지 - commit 필요
Remote Repository 문법
git remote add <remote_repo명> <remote_repo url>
git remote add origin <remote repo url>
연결할 remote repository 관리
가장 중요한 remote repo 명 = origin으로 해
git remote set-url origin <remote repo new url>remote repository url 수정
git remote rename <old_name> <new_name>remote repository 이름 수정
git remote remove <remote repo명>remote repository 삭제
git remote -vremote repository 정보 확인
(token 등록 해두었을 시 노출될 수 O)
git remote show <remote repo명>remote repository 상세보기
git pull <remote repo명> <branch명>
(git pull origin main)
remote repository 작업 내용 → local repository 에 동기화
<⇒ Fetch(download)와 Merge(기존의 것과)의 과정
git push <remote repo명> <branch명>
(git push origin main)
local repository에서 작업한 내용을 remote repository에 배포하고 싶을 때
  • Remote Repository
    • push : local → remote
    • pull : local ← remote
    • push, pull을 통해 동기화
  1. Checkout & Branch

    git checkout <commit_id>특정 버전으로 이동
    git branchbranch 목록 보기 (local의) + 현재 branch 확인
    git branch -rbranch 목록 보기 ( remote의)
    git branch -abranch 목록 보기 ( local + remote )
    git branch branch명(Local) HEAD가 가리키는 곳에 branch 생성
    git push origin branch명서버에 branch 배포
    git branch --delete branch명(Local) branch 삭제
    git push origin --delete branch명(Remote) branch 삭제
    git checkout branch명branch 간 이동
    git checkout -b branch명branch 생성 + 이동
    • commit ID ( 앞 7자리로 충분) = version 구분
    • Git Checkout
      • 그 버전 상태로 working directory 변경
      • HEAD → (checkout한 버전)
    • Git Branch
      • 기존 코드 복사하여 독립적으로 개발할 때
      • 특정 버전에서 새로운 branch 만들어서 개발 → 나중에 merge 가능
      • repository 생성 → main branch 만들어짐
  2. Merge & Conflict

    1. Merge : 현재 버전에 다른 버전 병합
      git merge <branch_name>
      • branch 병합 or Push | Pull
      • HEAD의 위치가 중요
    2. Conflict : Merge를 진행하다가 마주치는 문제상황
      • 두 Branch에서 같은 버전의 같은 부분을 수정하는 경우 - auto merge가 불가
      • 개발자가 직접 해결해야 함
      1. Branch 병합 시 Conflict 발생 파일 예시
        <<<<<<<< HEAD
        print("Hello, cat!")
        ========          # ->충돌부분 기준
        print("Hello, dog!")
        >>>>>>>> HEAD
        • Conflict 해결
          1. 파일 수정
            print("Hello, cat!")
            - 특수문자 안 남겨뒀는지 확인
          2. Git Add
            git add <conflict_file_name>
          3. Git Commit
            git commit -m <merge_message>
            - 실패 시, git commit -i로 강제 commit 가능
          4. Merge도 완료됨
      2. Push Pull 중에 Conflict 발생할 경우
        1. Push 동작 중
          • Push 실패
          • Pull 을 해서 Conflict 에러 확인
        2. Pull 동작 중
          - Fetch만 되고 Merge 실패 상태
          - Conflict 해결 → Merge Commit 완료
          ★★ Pull 먼저 하는 습관 - 문제 없는 지 확인 후 Push

5. Tag

git tag <tag_name>

  • 특정 버전에 Tag를 달아놓을 필요가 있을 때 - ex.) version release
  • Tag 이름으로 Checkout 가능
git tagTag 목록 조회
git tag tag명 <commit_id>지나간 것에 대해 tag할 때
git push origin <tag_name>Tag 배포
git show tag명Tag 상세정보 조회
git tag --delete tag명(Local) Tag 삭제
git push origin --delete tag명(Remote) Tag 삭제

6. README

: 프로젝트에 대한 설명, 사용방법, 라이센스, 설치법 등에 대해 기술하는 파일
★★★ git으로 포트폴리오 관리하는 것 중요

  • IMAGE
    • Issues 탭
      -New Issues - image 드래그 - 링크 copy - readme에 적어

Markdown

제목# 제목1
## 제목2
### 제목3
#### 제목4
##### 제목5
###### 제목6
인용> 인용문
>> 인용문의 인용문
숫자목록1. 2. 3.
목록* , + , -
코드` 코드 ` = 코드
``` 코드 ``` =
 코드 
구분선***
---
링크[단어] 링크
<링크>
<이메일>
기울임* 글자 *
_ 글자 _
굵게** 글자 **
__ 글자 __
취소선~~ 글자 ~~

0개의 댓글