47.Git-1

SOWA·2023년 5월 13일
0

Git

목록 보기
1/1

🧷 Git

버전 관리 시스템(형상관리)
ㄴ 버전관리를 하는 이유는 소스 데이터(.py 파일등)와 히스토리를 관리, 협업, 작업추적, 복구등이 가능

  • Configuration Management Systems
  • Version Control Systems

🖇️ Git 등장 배경

이전에는 소스 폴더+실행파일을 버전별로 카피하여 관리->컴퓨터가 다운되면 파일이 같이 날아가버림

파일 버전을 관리하기위해 Local Version Control Systems가 등장하며 내 컴퓨터에서 버전 관리가 가능해졌지만 컴퓨터가 하드가 날아가면 전체 코드가 사라짐. 버전은 관리되지만 여전히 협업에는 어려움

중앙에서 관리하는 Centralized Version Ccontrol Systems가 등장-> 협업이 가능하지만, commit하는 순간 배포되어 다수에게 버그 유발 가능, 인터넷이 안되면 작업 불가능, 자신만의 버전 히스토리가 없음

Distribured Version Control Systems가 등장. commit하더라도 개인 저장소 내에 적용(다른 개발자에게 영향없음), 언하는 순간에 배포(Push)가능, 오프라인테서도 작업 가능, 자신만의 버전 히스토리 가짐

🖇️ 버전관리 시스템의 종류

  • CVCS- CVS, SVN, etc.

    • CVS : 1980년대 만들어진 형상관리 시스템, commit 중 오류 발생시 롤백이 되지않는 문제발생, 이후 SVN으로 대체
  • DVCS- Mercurial, Git, etc.

    • Git: SVN보다 빠른 속도와 많은 기능 지원

🖇️ Git Global Configuration

  • Line ending(줄바꾸 문자)
    • Windows:CR(\r) + LF(\n)
    • Unix or Mac: LF(\n)
    • 윈도우 사용자와 맥사용자가 같은 Git Repository를 작업할때 코드에서 변경된 내용이 없어도 CRLF차이로 인해 commit이 발생할 수 있음
  • CRLF-Windows
    윈도우에서 가져올때는LF를 CRLF로 변경하고 내보낼때는 CRLF를 LF로 변경
    % git config --global core.autocrlf true
  • Editor
    디폴트 에디터 설정
    git config --global core.editor editor_name
  • 전체 설정 확인
    git config --list

  • 항목별 설정 확인
    git config key

git config user.name

🖇️ Git 기본용어

  • Repository
    • 소스코드가 저장되어 있는 여러개의 Branch가 모여있는 디스크상의 물리적 공간
    • Local Repository와 Remote Reposiory로 구분
  • Checkout
    • 특정 시점이나 Branch의 소스코드로 이동하는걸 의미
    • Checkout 대상 - Branch, Commit, Tag
    • Checkout을 통해 과거 여러 시점의 코드로 이동 가능
  • Stage
    • 작업할 내용이 올라가는 임시저장영역
    • 이 영역을 이용하여 작업한 내용중 commit에 반영할 파일만 선별하여 commit을 수행가능
  • Commit
    • 작업할 내용을 Local Repository에 저장하는 과정
    • 각각의 commit은 의미있는 변경단위이고, 변경에 대한 설명을 commit log로 남김
    • commit을 자주 사용하는 걸 권장
    • commit 단위나 commit log format을 정해놓는 회사나 팀도 있음(빌드 서버를 사용하는 경우)
  • Tag
    • 임의의 commit 위치에 쉽게 찾아갈 수 있도록 붙여놓은 이정표
    • Tag가 붙은 commit은 commit id(version) 대신 tag name으로 쉽게 checkout 가능
  • Push
    • Local Repository의 내용 중, Remote Repository에 반영되지 않은 commit을 Remote Repository로 보내는 과정
    • Push하는 순간 다른 개발자들도 영향을 받음. 검증되니않은 코드는 Push하지 않도록 함
  • Pull
    • Remote Repository에 있는 내용 중, Local Repository에 반영되지않은 내용을 가져와서 Local Repository에 저장하는 과정
    • 다른 팀원이 변경하고 Push한 내용을 Local Repository에 가져올 수 있음
    • Push 과정에서 Conflict(충돌)이 일어나서 Push가 거절된 경우, Pull을 통애 Remote Repository의 변경 내용을 Local Repository에 반영하여 Conflict를 해결한 뒤 다시 Push를 시도해야함.
  • Branch
    • 특정 시점(commit단위)에서 분기하여 새로운 commit을 쌓을 수 있는 가지를 만드는 것
    • 개발의 주축이 되는 branch를 master branch(혹은 main branch)라고 함
    • 모든 branch는 최종적으로 다시 master branch에 merge되는 형식으로 진행됨
  • Merge
    • Branch의 반대개념으로 하나의 Branch를 다른 Branch와 합치는 과정
    • Merge 되는 두 Branch는 주종관계가 성립. ex)dev branch를 main branch에 merge
    • Merge되는 과정에서 Conflict가 발생할 경우, Diff를 수정하여 Conflict를 해결한 뒤 Merge를 진행가능

🖇️ 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

  • 파일 생성
    Working Directory에 파일 생성
    ※touch 명령어 -빈 파일을 생성

test_project/ touch test.txt
test_project/ ls
test.txt


  • Git Status
    Git에 존재하는 파일 확인
    git status
git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   test.txt

  • Git Add
    Working Directory에서 변경된 파일을 Index(stage)에 추가
    git add filename

  • Git Commit
    Index(stage)에 추가된 변경사항을 HEAD에 반영(확정)
    git commit -m "commit에 대한 설명" filename
git commit -m "first commit" test.txt
[master (root-commit) 36195e9] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

🖇️ Remote Repository

─ Remote Repository 생성

  • Github에서 Create Repository 생성

  • Github Token 생성
    Remote Repository 접속 시 비밀번호 대신 Token 사용
    Token 이름 입력 → No expiratio→ repo →Generate token 선택

─ Remote Repository 등록

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

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

  • Remote Repository 등록 with Username and Token
    git remote add origin http://<username>:<token>@github.com/<repository>.git

$ git remote add origin https://swp0v0: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@github.com/swp0v0/test_project.git
  • Remote Repository 정보 확인
    git remote -v

─ Remote Repository에 변경내용 Push하기

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

  • Git Push
    git push origin branchname
git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 206 bytes | 206.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/swp0v0/test_project.git
 * [new branch]      master -> master


Remote Repository페이지에 push된 파일 확인 가능


─ Remote Repository에 Pull

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

  • ReadMe 파일 생성
    Git hub의 Remote Repository페이지에서 Add a ReadMe 클릭

  • Git Pull
    git pull origin branchname
git pull origin master
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 664 bytes | 55.00 KiB/s, done.
From https://github.com/swp0v0/test_project
 * branch            master     -> FETCH_HEAD
   36195e9..399d937  master     -> origin/master
Updating 36195e9..399d937
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README.md


from.제로베이스 데이터 취업스쿨 강의

0개의 댓글