commit, pull, push 밖에 몰랐던 나를 위한 Git의 원리

1 minute read

opentutorials 생활코딩 이고잉님의 ‘지옥에서 온 GIT - git의 원리’ 수업을 듣고 정리한 내용

🔎.git 내부의 몇 가지 폴더와 파일 분석

📁objects

  • object를 저장하고 있는 폴더
  • object : 데이터를 해싱해서 사용
  • object의 종류
    • blob : 파일내용을 저장하고 있는 객체, git은 여러개 파일의 내용이 같으면 하나의 blob만 생성한다.
    • tree : blob 또는 tree들의 링크를 포함하고 있는 객체 (디렉토리를 표현한 객체라고 이해됨)
    • commit : tree와 parent(부모 commit)과 commit 정보(author, committer, commit message)를 포함하고 있는 객체

📁refs

  • branch가 들어있는 폴더
  • 📑refs/heads/<branch> : 해당 branch에서 가장 최신 commit의 hash가 기록된 파일
  • 📑refs/remotes/<remote repository>/<branch> : 해당 원격 저장소의 branch에 마지막으로 push한 commit의 hash가 기록된 파일

📑branch (refs/heads/<branch>)

  • 해당 branch에서 최신 commit의 hash가 기록된 파일

📑index

  • 실제 파일명과 blob을 매핑한 정보가 기록된 파일
  • git add 명령어를 실행하면 index에 blob의 hash와 파일명이 기록된다.
  • working directory, working tree, working copy <> index, staging area, cache <> git repository, history, tree

📑HEAD

  • 현재 checkout된 branch의 파일 경로가 기록된 파일

📑ORIG_HEAD

  • 변경사항 직전 commit의 hash가 기록된 파일
  • git reset ORIG_HEAD 명령어를 실행하면 직전 commit으로 복구할 수 있다.
  • 예시
    • reset 되기 직전 commit의 hash를 기록했다가 최신 commit을 되돌릴 수 있다.
    • pull 하기 직전 commit의 hash를 기록했다가 merge된 사항을 되돌릴 수 있다.

🔎항상 헷갈리던 명령어들

✔️git reset

  • 현재 branch 파일에 기록된 최신 commit을 바꾼다.
  • 옵션에 따라 working copy와 index까지 바꿀 수도 있다. 211001042257.png 이미지 출처 : https://opentutorials.org/course/2708/15305

✔️git log git log –decorate –graph git log –decorate –all –oneline

✔️git reflog

✔️git fetch

  • 최신 데이터를 원격저장소로부터 다운로드만 받는다.
  • 즉, refs/remotes/origin/master 의 내용은 원격저장소의 최신 commit으로 변경되고, refs/heads/master 의 내용은 변경되지 않는다.
  • 변경점을 비교 후 merge 하기위해 사용한다.
    • git diff HEAD origin/master
    • git merge origin/master
  • pull = fetch + merge

🔎의미를 정확히 몰랐던 표현들

✔️detached HEAD

  • HEAD 파일에 branch의 파일 경로가 아니라 commit의 hash 직접 기록되어 있는 상태
  • checkout 할 때 branch가 아니라 commit의 hash를 직접 작성할 경우 발생할 수 있다.
    • 예시 : git checkout 53c32e...

✔️upstream branch

  • local repository에 연결된 remote repository의 branch

Reference

  • https://opentutorials.org/course/2708

Tags: ,

Categories:

Updated: