IT-Program/git

Git 기초 알아보기

Parque Aki 2022. 6. 30. 19:50

 

 

  • GitHub 이야기 
    • GitHub, Inc. 2007년 개발 시작 ~ 2008년 2월 공개
    • 2018년 6월 4일 마이크로소프트에서 약 8조원에 인수
    • 2019년 4,000만 이상의 사용자, 4,400만 개의 신규 저장소, 천만 명의 신규 사용자
    • 채용과 구직  

1. 특징 

  • 리누스 토발즈는 Git을 왜 만들었나? 
    • 빠른 속도
    • 단순한 구조
    • 비선형적인 개발 (수천 개의 동시 다발적인 브랜치)
    • 완벽한 분산
    • Linux 커널과 같은 대형 프로젝트로 증명된 속도와 안정성
  • SVN vs Git
    • Git이 더 빠르다. 
    • 로컬 저장소가 존재해서 구조와 저장 단계가 다르다. (CVCS vs DVCS) 
    • SVN은 각각의 버전을 변경사항으로, Git은 스냅샷으로 저장한다. 
    • SVN의 Branch는 무겁고 Git은 가볍다. 

2. 구조 

  • Working Area - Staging Area - Local Repository - Remote Repository 
    • PULL  ADD  COMMIT  PUSH
  • Content-addressable 파일시스템 
    • Git은 단순한 Key-Value 데이터 저장소
    • HEAD  BRANCH  COMMIT  TREE  FILE

3. 명령어 

  • help
  • $ git --help
    $ git [command] --help
    $ git help [command]
    $ git [command] -h
  • 초기 설정
  • $ git init
    $ git config --global user.name "[id]"
    $ git config --global user.email "[id]@matey.co.kr"
    $ git remote add origin [remote repository url]
    $ git config --global pull.rebase true
    $ git config --list
    $ git config user.name
    프로젝트 루트 경로에 .gitignore 파일 생성
  • 기본 사용
  • $ git branch [new branch name]
    $ git checkout [branch name]
    ... work ...
    $ git add .
    $ git commit -m "[commit message]"
    $ git push
  • Remote Repository/Branch
  • $ git pull
    $ git fetch
    $ git branch -a //Remote tracking branch 확인
    $ git branch -vv //Upstream branch 확인
    
    $ git checkout [remote branch name]
    $ git checkout -b [new branch name] origin/[remote branch]
    $ git checkout origin/[remote branch] //detached HEAD
    
    $ git checkout -b [new branch name]
    $ git push -u origin [new remote branch name]
    
    $ git push origin -d [branch name] //원격 브랜치 삭제 방법 1
    $ git branch -d [branch name] //원격 브랜치 삭제 방법 2
    $ git push origin :[branch name]
    
    //Remote Repository에 잘못 올라간 파일 삭제 (로컬 파일은 유지)
    $ git rm --cached file
    $ git push origin branch
  • Reset vs Revert: 특정 커밋으로 돌아가기 vs 특정 커밋 제외하기
  • $ git reset --hard[soft/mixed] [revision] //지정한 커밋으로 되돌리고 이후 커밋은 삭제
    
    $ git revert [revision] //지정한 커밋 내용을 삭제한 새 커밋 생성. 지정한 커밋 자체가 삭제되지는 않는다.
    $ git revert [revision]..[revision] //대상 커밋이 여러개일 경우 범위 지정
  • Merge vs Rebase: 커밋 합치기 vs 커밋 재구성  
  • $ git merge [branch name] //3way merge vs fast-forward
    	
    $ git rebase -i [revision]
    $ git rebase [branch name]
  • Cherry-Pick: 커밋 가져오기
  • $ git cherry-pick [revision]
  • Stash and Clean: 임시저장 vs 신규 파일 삭제
  • $ git stash -m "[message]"
    $ git stash -u //Untracked File 포함
    $ git stash -k //index에 있는 파일은 제외
    $ git stash [apply|pop|drop]
    
    $ git clean -nd //삭제 대상 Untracked File 확인
    $ git clean -fd //Untracked File 폴더 포함 강제 삭제
    $ git clean -x //.gitignore 에 명시되어 무시되는 파일까지 삭제
  • Lightweight vs Annotated Tag
  • $ git tag [tag name] //Lightweight Tag. 태그명만 생성
    $ git tag [tag name] [rivision]
    
    $ git tag -a [tag name] -m "[message]" //Annotated Tag. 태그에 다양한 정보 포함
    
    $ git push origin [tag name]
    $ git push origin --tags
    
    $ git tag -d [tag name]
    $ git push origin :[tag name]
  • Tips

명령어설명

checkout -b [new branch name] 새로운 브랜치 생성과 체크아웃을 한번에 실행
commit -am "[commit message]" 변경된 파일이 Tracked인 경우 Staging과 Commit을 한번에 실행
log --graph --oneline --branches 전체 브랜치의 커밋 로그를 그래프로 출력
reflog Git이 기억하고 있는 모든 커밋 내역을 조회
hash-object [file name] 파일 체크섬 해시 확인 (-w 옵션으로 실제 저장)
count-objects 오브젝트 수, 용량 등 확인
cat-file -p [checksum] 파일 내용 확인
cat-file -p master^{tree} master가 바라보는 tree 내용 확인
show [checksum] 체크섬 내용 확인

ProGit: https://git-scm.com/book/ko/v2

# 참고 이미지 

# SVN vs Git Speed [back]


# CVCS System [back]

# DVCS System [back]

# SVN Delta vs Git Snapshot [back]

# SVN New Branch [back]


# Working Area - Staging Area - Local Repository - Remote Repository [back]


# Content-addressable File System [back]


# Git Merge [back]

# Git Rebase [back]

$ git rebase -i HEAD~2

$ git rebase develop