🔥 git init
353자
3분
Git 프로젝트를 시작할 때 가장 먼저 해야 할 일은 바로 git init 명령어를 실행하는 것이에요. 이 명령어는 현재 디렉토리를 Git 저장소로 초기화한답니다. 그럼 예제 코드와 함께 자세히 알아볼까요?
shell
$ mkdir my-project
$ cd my-project
$ git init
Initialized empty Git repository in /Users/codingmax/github/my-project/.git/shell
$ mkdir my-project
$ cd my-project
$ git init
Initialized empty Git repository in /Users/codingmax/github/my-project/.git/위 코드에서 볼 수 있듯이, 먼저 mkdir 명령어로 새로운 디렉토리를 생성하고 cd로 해당 디렉토리로 이동했어요. 그리고 git init을 실행하면 .git 이라는 숨김 디렉토리가 생성되면서 Git 저장소가 초기화됩니다.
text
$ ls -la
total 0
drwxr-xr-x 3 codingmax staff 96 3 29 17:20 .
drwxr-xr-x 10 codingmax staff 320 3 29 17:20 ..
drwxr-xr-x 9 codingmax staff 288 3 29 17:20 .gittext
$ ls -la
total 0
drwxr-xr-x 3 codingmax staff 96 3 29 17:20 .
drwxr-xr-x 10 codingmax staff 320 3 29 17:20 ..
drwxr-xr-x 9 codingmax staff 288 3 29 17:20 .git.git 디렉토리에는 Git이 프로젝트를 관리하는 데 필요한 모든 정보가 담겨 있어요. 예를 들면 커밋 히스토리, 브랜치, 설정 등이 있죠.
text
$ cd .git
$ ls -al
total 24
drwxr-xr-x 9 codingmax staff 288 3 29 17:20 .
drwxr-xr-x 3 codingmax staff 96 3 29 17:20 ..
-rw-r--r-- 1 codingmax staff 21 3 29 17:20 HEAD
-rw-r--r-- 1 codingmax staff 137 3 29 17:20 config
-rw-r--r-- 1 codingmax staff 73 3 29 17:20 description
drwxr-xr-x 15 codingmax staff 480 3 29 17:20 hooks
drwxr-xr-x 3 codingmax staff 96 3 29 17:20 info
drwxr-xr-x 4 codingmax staff 128 3 29 17:20 objects
drwxr-xr-x 4 codingmax staff 128 3 29 17:20 refs
codingmax@tv .git % tree
.
├── HEAD
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ ├── push-to-checkout.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
9 directories, 17 filestext
$ cd .git
$ ls -al
total 24
drwxr-xr-x 9 codingmax staff 288 3 29 17:20 .
drwxr-xr-x 3 codingmax staff 96 3 29 17:20 ..
-rw-r--r-- 1 codingmax staff 21 3 29 17:20 HEAD
-rw-r--r-- 1 codingmax staff 137 3 29 17:20 config
-rw-r--r-- 1 codingmax staff 73 3 29 17:20 description
drwxr-xr-x 15 codingmax staff 480 3 29 17:20 hooks
drwxr-xr-x 3 codingmax staff 96 3 29 17:20 info
drwxr-xr-x 4 codingmax staff 128 3 29 17:20 objects
drwxr-xr-x 4 codingmax staff 128 3 29 17:20 refs
codingmax@tv .git % tree
.
├── HEAD
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ ├── push-to-checkout.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
9 directories, 17 files이 디렉토리를 삭제하면 커밋 히스토리, 브랜치, 설정 등이 삭제되기 때문에 주의해야 합니다. 만약 .git 폴더를 삭제한 후에 다시 git 을 사용하려면 git init 을 실행해서 설정을 처음부터 다시 시작해야 합니다.
한편, 이미 존재하는 프로젝트를 Git 저장소로 만들고 싶다면 해당 프로젝트 루트 디렉토리에서 git init을 실행하면 돼요.
shell
$ cd existing-project
$ git init
Initialized empty Git repository in /Users/codingmax/github/existing-project/.git/shell
$ cd existing-project
$ git init
Initialized empty Git repository in /Users/codingmax/github/existing-project/.git/










