git 기초

3 분 소요

git 기본적인 개념 및 특징과 튜토리얼 등에 대해서 정리하였습니다. 또한 github, gitlab, sourcetree에 대해서도 간략하게 소개하고 있는 글입니다.

개요


Git이란

  • source code management (SCM)
    • 모든 종류의 파일에 대한 이력을 관리
  • distributed version control system (DVCS)
    • <-> centralized VCS

특징

  • Branching and Merging
    • 서로 완전히 독립된 다수의 local brach를 생성하고 작업할 수 있음
    • branch 간의 충돌없이 switching 가능


[Branches]

  • Small and Fast
    • 대부분의 작업이 local에서 실행되므로 centralized VCS에 비해 상대적으로 빠름
  • Distributed
    • checkout하는게 아니라 전체 repository를 clone
      • 각각의 local repository들이 하나의 백업본이 됨
      • 다양한 형태의 workflow를 구성할 수 있음

[Centralized Workflow] [Integration Manager Workflow] [Dictator and Lieutenants Workflow]

  • Staging Area(index)
    • commit 전의 중간 영역
      • SVN과 다르게 일일이 commit 시에 변경된 파일 목록을 써주지 않아도 됨
    • 파일 변경한 후 git add 명령으로 staging area에 추가
    • staging area 영역에 추가된 모든 내용을 git commit 명령으로 repository에 추가

  • 기타
    • Data Assurance
    • Free and Open Source


Repository 구분

  • remote repository
  • local repository


Repository hosting service

  • GitHub
    • web-based
    • public repository: 무료
    • private repository: 유료
  • GitLab
    • web-based, 설치형 github
    • GitLab CE: Community Edition, private repository 무료
    • GitLab EE: Enterprise Edition, 기술지원 유료



Install & Setup


설치

  • Centos
    • $ sudo yum install git
  • Mac OS


설정

  • 적용 범위에 따른 설정 파일 구분
    • system: /etc/gitconfig
    • user: ~/.gitconfig
    • repository: ./.git/config
  • name, email 설정
    • $ git config --global user.name "John Doe"
    • $ git config --global user.email johndoe@example.com
  • alias 설정
    • $ git config --global alias.co checkout
    • $ git config --global alias.br branch
    • $ git config --global alias.ci commit
    • $ git config --global alias.st status
    • $ git config --global alias.unstage 'reset HEAD --'
    • $ git config --global alias.last 'log -1 HEAD'
  • proxy 설정, 해제, 보기
    • $ git config --global http.proxy http://user:pwd@proxy.server.com:8080
    • $ git config --global --unset http.proxy
    • $ git config --global --get http.proxy
  • git colorful하게 출력
    • $ git config --global color.ui true



Git tutorial


[Git operations]

Repository 생성하기

  • $ git init

원격 repository 복제하기

  • $ git clone URL (local_path)

파일 추가하기(tracking)

  • $ git add filename 또는 $ git add .

추적(tracking) 제외 설정하기

  • .gitignore 파일 추가

현재 로컬 repository 상태 확인하기

  • $ git status

로컬과 원격 저장소 비교하기

  • $ git diff filename

변경 내용 커밋하기

  • $ git commit -m "Add files"

변경 내용 발행하기

  • $ git push origin master

원격 repository 추가 및 확인

  • $ git remote add origin URL
  • $ git remote -v

원격 repository 변경 내용 갱신

  • 가지고 와서(fetch) 로컬 repository와 병합(merge)
    • $ git pull

변경 내용 되돌리기

  • git add한 파일 되돌리기(unstage)
    • $ git checkout -- HEAD filename
  • git commit한 내용 되돌리기
    • $ git reset HEAD filename
    • $ git reset HEAD~1
  • git remote 브랜치 push 삭제 & local 브랜치 변경 내용 삭제
    1. 로컬 브랜치를 원격 브랜치의 이전 커밋으로 되돌리기
      • $ git reset HEAD^
    2. 로컬 브랜치 모든 변경 내용 삭제
      • $ git checkout -- .
    3. 원격 브랜치의 이전 커밋 내용 삭제
      • $ git push origin +브랜치명
  • git remote 브랜치 push 삭제 & local 브랜치 commit만 삭제
    1. 로컬 브랜치를 원격 브랜치의 이전 커밋으로 되돌리기
      • $ git reset HEAD^
    2. 원격 브랜치의 이전 커밋 내용 삭제
      • $ git push origin +브랜치명
  • local 내용 롤백
    1. $ git fetch origin
    2. $ git reset --hard origin/master
  • 특정 tag명으로 롤백
    • $ git reset --hard tagname

가지(branch)치기

  • branch 생성 후 교체
    • $ git checkout -b develop
  • branch 교체
    • $ git checkout master
  • branch 삭제
    • $ git branch -d develop
  • branch 커밋
    • $ git push origin develop

병합(merge)하기

  • 현재 brach에 다른 branch 병합하기
    • $ git merge develop

태그(tag) 추가

  • tag 추가
    • $ git tag [태그명]
  • tag 추가 (annotation 포함)
    • $ git tag -a [태그명] -m 'new release'
  • remote 서버로 tag push하기
    • $ git push origin [태그명]
  • remote 서버로 여러 tag 한번에 push하기
    • $ git push origin --tags
  • tag 확인
    • $ git tag
  • 특정 tag 확인
    • $ git show [태그명]



Sourcetree


  • free visual git client tool



댓글남기기