Git

오픈소스 비즈니스 컨설팅
둘러보기로 가기 검색하러 가기

오픈소스 분산 버전 컨트롤 시스템인 git를 정리 한다.


Git 개요

Git은 로컬 저장소를 가지며 SnapShot 형태로 버전을 관리 합니다.


Git 저장소

  • Work Space : 개인별 작업 공간
  • Staging Area (index) : Commit할 파일을 지정 (git add)
  • 로컬 저장소 : 자신이 작업한 내용을 반영 (git commit, git commit -a)
    • nonbare-repository : 프로젝트 리소스 저장
  • 원격 저장소 : 다른 사람과 작업을 공유할 때 사용 (git push / git pull (git fetch, git merge))
    • bare-repository : 이력, 변경 사항 저장
  • Work Space -> Staging Area (index) -> Local Storage -> Remote Storage


Master and Branch

  • Master : 로컬 저장소의 메인 코드 (git checkout master, git merge branch)
  • Origian : 초기 리모트 저장소
  • Branch : 새로 작업을 위해 생성한 코드 (git branch, git checkout, git checkbout -b)

Git 설치

CentOS에서 git 설치

Git 설치

yum install git
git version


Git 리모트 저장소 생성

groupadd git
useradd  -d /cloudnas/home/git -m -g git git
passwd git
su - git

 #--- Local Repository 생성
mkdir gitLocal
git init

 #--- Remote Repository 생성
mkdir gitRemote
cd gitRemote
git clone --bare zztemp.git
#--- /cloudnas/home/git/zztemp.git/ 폴더가 생성됨

git config --global user.name "Mountain Lover"
git config --global user.email "consult@jopenbusiness.com"
git config --list

Windows에서 git 설치

Git 설치

Git 다운로드 사이트에서 Windows용 Git 설치 파일(Git-1.9.4-preview20140815.exe)을 다운로드하여 설치 합니다.

P:/001_work/gitRepositor/ 폴더를 만들고 해당 폴더에서 오른쪽 마우스를 클릭 합니다.

File:Git install 001.png


Git Bash에서 Git 로컬 저장소 생성

git config --global user.name "Mountain Lover"
git config --global user.email "consult@jopenbusiness.com"
git config --global push.default simpe            #--- 현재 작업중인 branch만 push
git config --list

cd p:/001_work/gitRepository
git clone ssh://git@www.jopenbusiness.com/cloudnas/home/git/zztemp.git
#--- p:/001_work/gitRepository/zztemp/.git/ 폴더가 생성됨


Git 테스트

cd p:/001_work/gitRepository/zztemp
#--- aaa.xml 파일을 생성 합니다.
git status                              #--- 상태 정보 조회

git add aaa.xml                        
git status
git commit
git status
git push
git status


Git 설정 파일

  • /etc/gitconfig : git config -system (시스템별 설정)
  • ~/.gitconfig : git config --global (사용자별 설정)
    • c:/Users/ghkim/.gitconfig  for Windows
  • .git/config : git config (저장소별 설정)


참고 문헌


Eclipse에 GitHub 설치

Eclipse에서 GitHub를 사용하기 위해 GitHub를 설치 합니다.
"Help -> Install New Software... -> Add.." 메뉴에서 아래 사항을 추가하여 설치 합니다.


Eclipse 환경 설정

"Windows -> Open Perspective" 메뉴에서 "Git"를 추가 합니다. "Clone a Git repository" 링크를 선택하여 리모트 Git repository를 등록 합니다.

File:Eclipse git 001.png

File:Eclipse git 002.png

File:Eclipse git 003.png

Git의 주요 개념

Item Description
Branch
  • master : 최초 생성되는 branch (master -> refs/heads/master)
Tag
  • Branch와 동일한 reference로 reference에 의미를 부여 한다.
  • Tag의 위치는 변경되지 않는다.


Git 폴더 구조

  • .gitignore : Git 관리 대상에서 제외되는 파일 지정 (다른 사람과 공유)
  • .git/
    • branches/
    • hooks/ : 특정 조건을 만족할 경우 실행되는 Hook Script 파일을 관리
      • prepare-commit-msg
      • pre-commit : Commit 전에 실행
      • commit-msg
      • pre-push : Push 전에 실행
      • update
      • post-update
      • pre-applypatch
      • applypatch-msg
      • pre-rebase
    • info/
      • exclude : Git 관리 대상에서 제외되는 파일 저장 (다른 사람과 공유되지 않음)
      • refs : Reference 목록을 저장 (git gc 명령으로 생성)
    • logs/
    • objects/ : 파일이 BLOB 객체로 저장되는 폴더로 Staging Area에 해당함
      • SHA-1 checksum 값의 첫 두자리를 폴더명으로 나머지를 파일 이름으로 사용하여 저장
      • 파일 목록을 담은 트리 객체와 commit 정보를 담은 커밋 객체를 저장
      • info/ : 메타 데이터
      • pack/ : Packfile (Snapshot 저장 방식 대신 변경분 저장 방식으로 하나의 파일로 묶은 파일) 저장
    • refs/
      • Commit ID 중에서 reference(특정 커밋에 대한 포인터)라는 별도의 이름을 지정한 것을 관리
      • heads/ : 최상위 commit 아이디를 저장
        • master : Master Branch의 마지막 commit ID 저장
        • develop : Develop Branch의 마지막 commit ID 저장
      • remotes/origin/ : Remote Git 저장소의 최상위 commit 아이디를 저장
      • tags/ : Tag를 저장
    • HEAD : HEAD reference가 가리키는 commit ID 저장 (현재 작업 폴더에 check out된 버전을 지정)
    • config : 로컬 설정 파일 (이 파일이 있는 Git 저장소에 적용)
  • ~/.gitconfig : 사용자 설정 파일 (특정 사용자가 사용하는 모든 Git 저장소에 적용)
  • /etc/gitconf : 시스템 설정 파일 (시스템 전체의 모든 Git 저장소에 적용)


파일 이름 : SHA-1로 관리

Commit ID : SHA-1로 관리

Git 명령어

  • 저장소 : Work Space -> Staging Area (index) -> Local Storage -> Remote Storage
  • 버전 : Master, Branch
분류 명령어 상세
초기 설정 git init 현재 폴더를 로컬 저장소 생성
git remote add <repository> 리모트 저장소 연결
Repository : user@password:/~/~
  • ssh://[user@]host.xz[:port]/path/to/repo.git/
  • git://host.xz[:port]/path/to/repo.git/
  • http[s]://host.xz[:port]/path/to/repo.git/
  • ftp[s]://host.xz[:port]/path/to/repo.git/
  • rsync://host.xz/path/to/repo.git/
  • /path/to/repo.git/
  • file:///path/to/repo.git/
git clone <repository> 로컬 저장소로 리모트 저장소 복제
저장하기 git add Staging Area로 추가
git reset HEAD Staging Area에 추가할 파일을 삭제
git commit 로컬 저장소로 commit
git commit -a git add + git commit
git fetch origin
git reset --hard origin/master
로컬 저장소로 commit한 것을 복구
git push 리모트 저장소로 commit
가져오기 git fetch 로컬 저장소로 가져오기
git merge 로컬 저장소의 소스와 병합
git pull git fetch + get merge
버전 관리 git branch ~ 브랜치 생성
git checkout ~
git checkout -b ~ git branch + git checkout
git branch -d ~ 브랜치 삭제
git checkout master
git merge branch 브랜치 병합
git branch
git branch --no-merged
git branch --merged
브랜치 목록
태그 관리 git tag ~
git tag -a ~
태그 생성
git tag -d ~ 태그 삭제
git tag 태그 목록
설정 관리 git config --list
git config <설정명>
설정 정보 조회
git config [--system | --global] <name> "<value>" 설정 등록

Eclipse에서 git 사용

Eclipse에서 프로젝트 공유

"Team -> Share Project ... -> Git" 메뉴를 선택 합니다.

Git의 Local Repository를 선택하여 공유를 완료 합니다.


Git과 동기화

  • Team -> Add to Index : Git으로 관리할 파일로 추가
  • Team -> Remote -> Commit : Local Repository로 저장
  • Team -> Push : Git Server Repository로 저장
    • Source ref로 master를 선택 합니다.
    • "Add Spec" 버튼을 선택 합니다.

Hooks

Hook을 사용하여 사용자별 권한 설정을 구현할 수 있습니다.

Client Hooks

pre-commit, prepare-commit-msg, commit-msg / post-commit

applypatch-msg, pre-applypatch / post-applypatch

post-merge

pre-rebase, post-rewrite

pre-push

pre-auto-gc

Server Hooks

pre-receive, update / post-update, post-receive



참고 문헌