git command

shell 기본 명령어

# 홈 디렉토리로 이동
$ cd ~

# 파일 삭제
$ rm index.html

# 폴더 삭제
$ rm -rf html_example

# 맥에서 현재 디렉토리를 application 열기
$ open .
$ sublime .
$ atom .

vim 기본 명령어

dd : 삭제
u : 실행취소
x : 한 글자 삭제
yy : 한 줄 복사
p : 붙여넣기

git 명령어

# git 설치 확인
$ git --version
git version 2.11.0

# local repository 생성
$ cd your/dir/path
$ git init

# git ignore 꼭 해주기!

# 현재 상태 확인
$ git status

# 파일 생성 하여 작업
$ vi index.html

# -

# staging area에 올리기
# git add 파일명
$ git add index.html

# local repository에 올리기
$ git commit
# vi mode 진입 > 메시지 작성 후 저장하고 끝내기
# or
$ git commit -m "메시지 추가"

# history 확인
$ git log

여러개의 파일을 add 하는 방법

$ mkdir texts
$ cd texts
$ vi intro.txt

# 파일명을 지정해 add 하기
$ git add index.html texts/
# 변경된 모든 파일 add 하기
$ git add .
$ git add -A

# unstage 하기
# HEAD 현재 브랜치, 현재 commit
$ git reset HEAD index.html

# add + commit 한꺼번에 하기
$ git commit -a -m "메시지" # 모든 파일을 한번에 add+commit 하기
$ git commit index.html -m "메시지" # 파일 하나를 한번에 add+commit 하기

git reset

특정 시점으로 돌아가는 것

$ git log
# commit 번호 복사
# git reset --hard commit 번호
$ git reset --hard 48c3e7351a7bc786a6d6cb4c1dcf07747c0d84ff

# 위에 했던 reset을 다시 취소하기
# 단, 중간에 작업을 하지 않은 상태에서 해야함
$ git reset --hard ORIG_HEAD

git ignore

git init 하자마자 해주는 것이 제일 좋다.

# dummy.txt 를 작성 후 저장
$ vi dummy.txt
# .gitigonre 파일에 작성한 파일들은 무시
$ vi .gitignore

gitignore file 생성 사이트

git revert

특정 한 개의 commit만 취소하는 것

$ git revert 48c3e7351a7bc786a6d6cb4c1dcf07747c0d84ff

git clone

$ cd your/dir/path
$ git clone https://github.com/pinstinct/first_git.git

git push

# github에서 새로운 remote repository를 생성
$ git remote add origin https://github.com/pinstinct/second_git.git
$ git push -u origin master

git branch

시나리오

  • 초기화 : working directory 지정 (init) > .gitignore 추가 > inital commit
  • 브랜치 생성 : html, css
    • html에서 작업하다 기능추가를 위한 feature, 수정을 위한 fix 브랜치 생성 > 작업 후 html로 merge
    • css에서도 작업
  • 작업이 완료되면 html, css 브랜치를 master에 merge
$ mkdir my_home
$ touch .gitignore # 파일 작업
$ git status
$ git add .
$ git commit -m "initial commit"

# branch 만들기
$ git branch html
$ git branch css
# 브랜치 확인하기
$ git branch
  css
  html
* master
# 브랜치 옮기기
$ git checkout html

# 브랜치 만들고 바로 옮기기
# $ git checkout -b html

$ touch index.html # 파일 작업
$ git add .
$ git commit "index.html의 뼈대를 작성했습니다"

# master 브랜치로 옮기면 initial commit 상태이므로
# index.html 파일을 확인 할 수 없음
$ git checkout master

$ git checkout css
$ touch style.css # 파일 작업
$ git commit -a -m "style.css 파일 생성, h1 태그 스타일 완성"

# 하위 브랜치 생성
$ git checkout html
$ git branch feature html

$ git checkout feature
$ vi index.html # 파일 작업
$ git add .
$ git commit "h1 태그를 작성했습니다."

$ git checkout html
$ git branch fix html
$ vi index.html # 파일 작업
$ git add .
$ git commit "index.html 수정 태그 대소문자 변경"

# merge
$ git log # commit number 복사
$ git checkout html
$ git merge e2442c2b8f4cab9a93596fba1ed362e2f93565f9

$ git checkout feature
$ git log # commit number 복사
$ git merge 27d9f308b96807a49e00177d1a651ad6b098e100
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
# conflic 해결 후
$ git add .
$ git commit "Merge commit '27d9f308b96807a49e00177d1a651ad6b098e100' into html"

$ git log # commit number 복사
$ git checkout feature
$ git merge 569a871f41017fc2251ed1c2db858cbbf0d74d63

# 브랜치 이름으로 merge
$ git checkout master
$ git merge css

# repository의 commit 이력을 조회
$ git log
$ git log --oneline
$ git log --oneline --decorate --graph --all
# 특정 파일의 로그 조회
$ git log -- index.html

과제

  • 버전관리란 무엇이고, 버전관리 방법에는 어떤 것들이 있는지 알아보기
  • git 명령어 정리하기
  • 위 두 가지는 Remote Repository에서 날짜폴더 하위에 이름/README.md 로 정리하기