2021년 6월 13일 일요일

GIT Quick Guide

 1. Git 저장소 만들기

  - 기존 디렉토리를 Git 저장소로 만들기

    $ cd /home/user/project

    $ git init

    $ git add *.c

    $ git commit -m 'comment'

  - 기존 저장소를 Clone 하기

    $ git clone https://github.com/project

    $ git clone pi@github.com:/home/pi/project

    $ git remote -v

      origin  pi@zdiv.iptime.org:/home/pi/git (fetch)

      origin  pi@zdiv.iptime.org:/home/pi/git (push)


2. 수정하고 저장소에 저장하기

    (untracked) (unmodified) (modified) (staged)
         |--------------(add)--------------->|
         |           |--(edit)-->|           |
         |           |---------------(rm)--->|
         |           |           |---(rm)--->|
         |           |           |---(add)-->|
         |<-(remove)-|           |<--(edit)--|     
         |           |<-------(commit)-------|

  - 파일의 상태 확인하기

    $ git status

    $ git status -s (짧게확인)

  - 파일을 새로 추적하기

    $ git add <file>

  - Modified 상태의 파일을 Stage 하기

    $ git add <file>

  - 파일 무시하기 (.gitignore)

    . 아무것도 없는 라인이나, `#`로 시작하는 라인은 무시한다.

    . 표준 Glob 패턴을 사용한다. 이는 프로젝트 전체에 적용된다.

    . 슬래시(/)로 시작하면 하위 디렉토리에 적용되지(Recursivity) 않는다.

    . 디렉토리는 슬래시(/)를 끝에 사용하는 것으로 표현한다.

    . 느낌표(!)로 시작하는 패턴의 파일은 무시하지 않는다.

    . 애스터리스크 2개를 사용하여 디렉토리 안의 디렉토리 까지 지정할 수 있다

    $ cat .gitignore 

      *.[oa]

      *~

  - Staged와 Unstaged 상태의 변경 내용을 보기 

    $ git diff

    $ git diff --cached  (Stage된 파일 비교)

    $ git diff --staged  (Stage된 파일 비교)

    $ git difftool (외부 비교 툴 사용)

    $ git difftool --tool-help

  - 변경사항 커밋하기

    $ git commit

    $ git commit -m <comment>

    $ git config --global core.editor (편집기 설정)

  - Staging Area 생략하기

    $ git commit -a (모든 파일을 stage함)

  - 파일 삭제하기

    $ git rm <file>

    $ git rm -f <file>       (Stage된 파일도 삭제)

    $ git rm --cached <file> (Stage된 파일만 삭제)

  - 파일 이름 변경하기

    $ git mv <file_from> <file_to> (아래 세 동작과 동일)

    $ mv README.md README

    $ git rm README.md

    $ git add README



3. 커밋 히스토리 조회하기

    $ git log

  

4. 되돌리기

  - 마지막 commit 덮어쓰기

    $ git commit --amend 

    $ git commit -m 'initial commit'

    $ git add forgotten_file

    $ git commit --amend

  - 파일 상태를 Unstage로 변경하기

    $ git reset HEAD <file>

  - Modified 파일 되돌리기

    $ git checkout -- <file>



5. 리모트 저장소

  - 리모트 저장소 확인하기

    $ git remote 

    $ git remote -v 

  - 리모트 저장소 추가하기

    $ git clone <url> (origin 추가됨)

    $ git remote add <alias> <url>

    $ git fetch <remote>

    $ git pull (fetch + merge 수행)

  - 리모트 저장소에 Push 하기

    $ git push <remote> <branch>

    $ git push origin master

    $ git config --bool core.bare true 

  - 리모트 저장소 살펴보기

    $ git remote show <remote>

  - 리모트 저장소 이름을 바꾸거나 리모트 저장소를 삭제하기

    $ git remote rename <old> <new>

    $ git remote remove <remote>

    # git remote rm <remote>

    


댓글 없음:

댓글 쓰기