[Git] Git 기본 명령어 요약

Woong·2021년 12월 14일
0

Git 기본

목록 보기
3/16
  • 시리즈 구성을 위해 본 포스트에는 기본 내용만 나열함. 상세는 개별 포스트로 작성

저장소 생성

git init

  • git 저장소를 생성

git clone

  • git 저장소를 clone 하여 local 로 가져옴

Index 관련

git add

  • index 에 파일 추가, 변경사항 적용
git add {filename}

git rm

  • index 에 파일을 삭제하도록 상태 정보 갱신
git rm {filename}

git status

  • index에 저장된 상태(stage) 정보를 조회
    • Changes to be committed : commit 될 내용
    • Changes not staged for commit : 변경사항이 있으나 index에 반영하지 않아 commit 되지 않을 내용
    • Untracked Files : 추적하지 않는 파일

git status . 와 같이 경로 지정시 해당 경로의 하위 디렉토리에 대해서만 탐색


commit 관련

git commit

  • index 에 stage 된 내용을 commit 으로 저장
    • ※ push 하기 전까진 local branch 에만 반영되어있음.
    • --amend : commit 수정

git push

  • commit 된 내용을 remote 서버에 반영한다.
git push {remote} {branch_name}

git fetch

  • remote 서버에서 commit 이력을 가져와 업데이트
    • merge 는 발생하지 않음
git fetch {remote} {branch_name}
  • -all : 현재 remote 서버에 tracking 중인 모든 branch 에 대해 fetch 를 수행
git fetch --all

git pull

  • remote 서버에서 commit 이력을 가져와 병합
    • fetch + merge
    • merge 할 내용이 없을 경우엔 fast-forward 만 발생 (HEAD 만 이동)
git pull {remote} {branch_name}

기록, 비교

git log

  • commit log 출력
    • --graph : 그래프 형태로 출력
    • --oneline : commit message 를 첫 라인만 출력
      git log --graph --oneline --all
    • -p, --patch : 최근 N개 커밋의 diff 결과를 보여줌
      git log -p 2
    • 파일명, 디렉토리 지정 : 해당 파일/디렉토리 변경사항만 출력
    • --author : 특정 사용자 commit 만 출력
      git log --author={username}
      git log --author="{username}"

git diff

  • index에 staged 된 파일과 diff 수행
    • 파일, 디렉토리를 명시하면 해당 내용만 비교
git diff .

branch 이동, 변경

git checkout

  • 특정 branch 로 이동
git checkout {branch_name}
  • 파일, 디렉토리를 명시할 경우 변경 내용을 되돌림
git checkout {filename}

git reset

  • commit된 내용을 되돌리고 삭제

  • 상세 내용은 개별 포스트로 정리

  • 현재 작업디렉토리의 내용을 모두 버리기
    git reset --hard HEAD

git revert

  • commit 변경사항을 되돌리는 새로운 commit 생성
    • commit log가 삭제되지는 않음
    • 상새 내용은 개별 포스트에 작성
git revert {commit}

git cherry-pick

  • 다른 branch 에 있는 commit 을 현재 branch 에 반영

Stash

  • 아직 마무리되지 않은 작업을 저장하는데 사용
    • 대상 : Modified이면서 Tracked 상태, 또는 Staged 상태인 파일들

git stash

  • 현재 작업을 일시적으로 저장. save 생략 가능
    • git stash save
  • 저장된 stash 목록 나열
    • git stash list
  • 저장된 stash 복구
    • git stash pop
  • 저장된 stash 제거
    • git stash drop
  • 저장된 stash 모두 제거
    • git stash clear

tag

git tag

  • 태그 추가

    • git tag {tag_name}
  • 이전 commit 에 태그 추가

    • git tag {tag_name} {commit_id}
  • 태그 조회

    • git tag
    • git tag -l
  • 특정 태그 조회

    • git tag v1.*
  • 태그명으로 commit 조회

    • git show {tag_name}
  • 원격 저장소에 태그 반영하기 (push)

    • 특정 tag 만 push
      • git push {remote_name} {tag_name}
    • 모든 태그 push
      • git push {remote_name} {branch_name} --tags

blame

git blame

  • 파일의 수정 내역 출력
    • commit, author, time, line number
    • git blame {filename}
  • 특정 라인의 수정 내역만 출력 (start ~ end)
    • git blame -L {start,end} {filename}
  • 특정 라인 전후로 수정 내역 출력 (start 부터 line 만큼)
    • git blame -L {start,+line} {filename}

0개의 댓글