티스토리 뷰

데브옵스

Git의 기본적인 사용법

조묵헌 2018. 5. 14. 15:02

status & log & diff

# git 작업폴더 상태 확인
git status

# Log 보기
git log

# commit 간 차이점 모두 보기
git log -p

# commit 아이디를 사용해 차이점 보기
git diff commit_id..commit_id

Git의 기본적인 사용법

실습을 위한 프로젝트 경로 : workspace/project 로 가정합니다.

Working Area: init, clone

# 프로젝트를 git repository로 생성하기
cd project
git init

# 로컬 repository를 클론하여 작업폴더로 만들기
git clone /path/to/repository

# 원격 repository 클론하여 작업폴더로 만들기
git clone https://github.com/username/project-name.git project-name
cd project-name

Stage Area: add

# 선택적으로 stage에 올리기
git add index.html

# 모든 파일을 스테이지에 올리기
git add *
git add .
git add -A

Local Repository: commit

# 변경사항 commit
git commit -m "Fix typo in index.html"

Remote Repository: push, pull

# local repository를 origin이라는 이름의 remote repository와 연결하기
git remote add origin <server>
git remote add origin https://github.com/username/test_username.git

# remote 저장소 목록보기
git remote -v

# local의 master 브랜치를 heroku remote repository로 push
git push heroku master

# git push의 기본값을 git push origin master로 지정
git push -u origin master

# origin remote의 변경사항을 local repository에 업데이트
git pull origin master
git pull

# fetch - remote에서 변경사항을 가져오지만 HEAD에 merge는 하지 않는다
git fetch
git merge origin/master

revert

# 예전 Commit 해시버전으로 돌아가기 (사용자제)
git reset commit_id --hard

# 지정한 commit 해시버전을 최상단 커밋으로 올리면서 돌아가기, 즉 히스토리 남기기
git revert commit_hash

### revert cbc ###
0005 ebc      Revert "0003" fbc
0004 dbc      0005 ebc
0003 cbc ===> 0004 dbc
0002 bbc      0003 cbc
0001 abc      0002 bbc
              0001 abc

Branching

Branch: 분기

# branch 목록보기
git branch

# 새로운 branch 생성
git branch exp

# branch 스위치하기
git checkout exp
git checkout master

# 새로운 branch 생성후 바로 전환하기
git checkout -b <branch>

# branch 삭제하기
git branch -d <branch>

# remote repository로 branch를 push 하기
git push origin <branch>

Merge: 병합

# 다른 branch로부터 변경사항 합치기
git merge <branch>

# exp 브랜치를 master 브랜치에 합치기
git checkout master
git merge exp

충돌 발생시

가운데 ====== 를 기준으로 HEAD 부분의 현재 체크아웃된 브랜치의 내용과,

exp 부분의 merge 한 부분의 차이점을 보여준다.

<<<<<<< HEAD
function a(master) {}
=======
function a(exp) {}
>>>>>>> exp

rebase

branch를 하나로 유지할 수 있다.

git checkout rb
git rebase master
git checkout master
git merge rb

Stash

branch에서 작업도중, 다른 branch로 체크아웃해야 하는 상황, 하지만 현재 branch의 작업이 끝나지 않아 commit 할 수 없을 때 임시로 branch 작업 내용을 보존하기 위해 사용한다.

# stash 하기
git stash

# 복원하기
git stash apply

Tagging

# tag 붙이기
git tag <tag> <commit ID>
git tag 1.0.0

# annotated tag 생성하기
git tag -a 1.1.0 -m "Tag Description"

# tag annotation 내용 보기
git tag -v 1.1.0

# remote에 release버전 추가하기 (tag push하기)
git push --tags

Logs, Diffs

# git branch 그래프로 보여주기
git log --branches --decorate --graph

# branch간 log 비교하기
git log -p master..exp
git log -p exp..master

# 각 branch간의 변경사항 비교
git diff <source_branch> <target_branch>
git diff master..exp

'데브옵스' 카테고리의 다른 글

마크다운 사용하기  (0) 2018.05.14
git 기초  (0) 2018.05.14
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함