- 깃 저장소 만들기
- 저장소를 만들고 싶은 디렉토리에서 깃 초기화를 하면
- 그 때부터 해당 디렉토리에 있는 파일들을 버전 관리할 수 있음
$ mkdir git-test
$ cd git-test
$ ls -alrt
drwxr-xr-x 1 test 197609 0 4월 13 13:04 ../
drwxr-xr-x 1 test 197609 0 4월 13 13:05 ./
git 초기화하기 : git init
.git/ 디렉토리가 생성됨
$ git init
Initialized empty Git repository in [깃 저장소 경로]
$ ls -alrt
drwxr-xr-x 1 test 197609 0 4월 13 13:04 ../
drwxr-xr-x 1 test 197609 0 4월 13 13:05 ./
drwxr-xr-x 1 test 197609 0 4월 13 13:05 .git/ #해당 디렉토리가 깃을 사용하면서 버전이 저장될 저장소
버전 관리에서 제외하기 : .gitignore파일
버전 관리 중인 디렉토리 안에서 버전 관리를 하지 않을 파일이나 디렉토리는
.gitignore파일을 만들어 그 안에 파일 또는 디렉토리 이름이나 파일 확장자명 지정
2. 버전 만들기
버전 : 수정된 내용이 쌓이면서 번호를 붙여서 이전 상태와 구별된 것
스테이지 & 커밋 이해
깃이 어떻게 파일이름을 그대로 유지한 채 수정 내용을 기록하는지????
작업트리 : 파일 수정, 저장 등 작업을 하는 곳
스테이지(stage) : 버전으로 만들 파일이 대기하는 곳
저장소(repository) : 스테이지에 대기하던 파일을 버전으로 만들어 저장하는 곳
-
작업 트리에서 문서 수정, 저장
-
수정한 파일 중, 버전으로 만들고 싶은 것을 스테이지에 저장
-
스테이지에 저장된 파일을 저장소로 커밋(commit)하기
문서 수정하기
$ git status #git 상태 확인
On branch master #현재 master 브랜치에 있음
No commits yet #아직 커밋할 파일이 없음
nothing to commit (create/copy files and use "git add" to track) #현재 커밋할 파일이 없음
$ vi hello.txt
1
$ ls
hello.txt
$ git status
On branch master
No commits yet
Untracked files: #한 번도 버전관리하지 않은 파일
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
파일을 스테이징(staging)하기 : git add
깃에게 버전 만들 준비를 하라고 알려주는 것
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
$ git status
On branch master
No commits yet
Changes to be committed: #new file(hello.txt)를 커밋할 것
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
스테이지에 올라온 파일을 커밋하기 : git commit
파일이 스테이지에 있으면 버전을 만들 수 있음(커밋, commit)
커밋할 때는 어떤 변경사항이 있는지 메시지를 기록해두기!!
$ git commit -m "message1"
*** Please tell me who you are.
Run
$ git config --global user.email "test@example.com"
$ git config --global user.name "test"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'test@DESKTOP-E6sefdf.(none)')
$ git config --global user.email "계정명@github.com"
config 수정 후, 내용 확인하기
$ git config --list
$ git commit -m "message1"
[master (root-commit) 4ef0867] message1
1 file changed, 1 insertion(+)
create mode 100644 hello.txt
$ git status
On branch master
nothing to commit, working tree clean #버전으로 만들 파일이 없고, 작업트리도 수정사항이 없음
$ git log
commit 4ef0867adfd78883dfbc34fe0a0320c7d0475a9a (HEAD -> master)
Author: unknown <계정명@github.com> #만든 사람
Date: Mon Apr 13 17:32:41 2020 +0900 #만든 날짜
message1 #커밋 메시지
커밋한 메시지 수정하기
가장 최근의 커밋 메시지를 수정하려면
$ git commit --amend #vi 편집기가 실행됨
message3
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Tue Apr 14 15:48:23 2020 +0900
#
# On branch master
# Changes to be committed:
# modified: hello.txt
# new file: hello2.txt
#
스테이징과 커밋을 한번에 : git commit -am
이 방법은 한번이라도 커밋한 적이 있는 파일을 다시 커밋할 경우에만 사용 가능
$ vi hello.txt
1
2
$ git commit -am "message2"
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master 5bca567] message2
1 file changed, 1 insertion(+)
$ git log
commit 5bca567d5e4086c9c366b7e8ed625cd0f67eb586 (HEAD -> master)
Author: unknown <계정명@github.com>
Date: Tue Apr 14 12:00:38 2020 +0900
message2
commit 4ef0867adfd78883dfbc34fe0a0320c7d0475a9a
Author: unknown <계정명@github.com>
Date: Mon Apr 13 17:32:41 2020 +0900
message1
3. 커밋 내용 확인하기
버전 관리를 위해선 지금까지 어떤 버전을 만들었는지 알아야 함
각 버전마다 어떤 차이가 있는지 파악 필요
커밋 기록 자세히 보기 : git log
git log결과 정보들을 커밋 로그라고 함
--oneline 옵션 : 한 출에 한 커밋씩 나타내 주기 때문에, 간략히 확인 시 편리
변경 사항 확인하기 : git diff
작업트리에 있는 파일과 스테이지에 있는 파일,
스테이지에 있는 파일과 저장소에 있는 최신 커밋을 비교해서
수정한 파일을 커밋하기 전에 최종 검토가 가능
$ vi hello.txt
1
two
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
diff --git a/hello.txt b/hello.txt
index 1191247..0b66db0 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,2 +1,2 @@
1
-2 #최신버전과 비교시, 2가 삭제됨
+two #파일에 two내용이 추가됨
수정한 내용을 버리려면 git checkout 명령어 사용
4. 버전 만드는 단계마다 파일 상태 알아보기
tracked파일과 untracked파일
작업트리에 있는 파일은 크게 tracked상태와 untracked상태로 나뉨
$ vi hello.txt
1
2
3
$ vi hello2.txt
a
b
c
d
$ git status
On branch master
Changes not staged for commit: #변경된 파일이 아직 스테이징되지 않음
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt #변경된 파일명
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello2.txt
no changes added to commit (use "git add" and/or "git commit -a")
깃은 한번이라도 커밋을 한 파일의 수정여부를 계속 추적함
깃이 추적하고 있다는 의미에서 tracked파일 이라고 부름
Untracked는 한 번도 깃에서 버전 관리를 하지 않아서 추적하지 않는다는 의미
$ git add hello.txt #hello.txt를 스테이지에 올리기
$ git add hello2.txt #hello2.txt를 스테이지에 올리기
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
new file: hello2.txt
tracked파일과 Untracked파일 모두 스테이지에 올라옴
$ git commit -m "message3"
[master cb2f110] message3
2 files changed, 5 insertions(+)
create mode 100644 hello2.txt
$ git log
commit cb2f110b166ca50688739dec75e60faeb3604d78 (HEAD -> master)
Author: unknown <계정명@github.com>
Date: Tue Apr 14 15:48:23 2020 +0900
message3
commit 5bca567d5e4086c9c366b7e8ed625cd0f67eb586
Author: unknown <계정명@github.com>
Date: Tue Apr 14 12:00:38 2020 +0900
message2
commit 4ef0867adfd78883dfbc34fe0a0320c7d0475a9a
Author: unknown <계정명@github.com>
Date: Mon Apr 13 17:32:41 2020 +0900
message1
각 커밋에 어떤 파일들이 관련된 것인지 확인 불가
$ git log --stat
commit cb2f110b166ca50688739dec75e60faeb3604d78 (HEAD -> master)
Author: unknown <계정명@github.com>
Date: Tue Apr 14 15:48:23 2020 +0900
message3
hello.txt | 1 +
hello2.txt | 4 ++++
2 files changed, 5 insertions(+)
commit 5bca567d5e4086c9c366b7e8ed625cd0f67eb586
Author: unknown <계정명@github.com>
Date: Tue Apr 14 12:00:38 2020 +0900
message2
hello.txt | 1 +
1 file changed, 1 insertion(+)
commit 4ef0867adfd78883dfbc34fe0a0320c7d0475a9a
Author: unknown <계정명@github.com>
Date: Mon Apr 13 17:32:41 2020 +0900
message1
hello.txt | 1 +
1 file changed, 1 insertion(+)
가장 최근의 커밋부터 순서대로 커밋 메시지와 관련 파일이 나열됨
enter를 치면 다음 로그 화면으로 이동, q를 누르면 로그 화면에서 빠져나옴
unmodified, modified, staged 상태
tracked된 파일이 있는 조건에서 이전 명령어 결과에서 확인하자면....
$ git status
On branch master
nothing to commit, working tree clean #unmodified상태(수정되지 않은 상태)
*** 파일 수정 후 ***
$ git status
On branch master
Changes not staged for commit: #modified상태(수정만 된 상태)
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
*** git add 실행 후 ***
$ git status
On branch master
Changes to be committed: #staged상태(commit할 변경 사항이 있는 상태)
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
'git & github' 카테고리의 다른 글
원격 저장소와 깃허브 (0) | 2020.10.31 |
---|---|
브랜치 관리 (0) | 2020.10.31 |
브랜치 병합 (0) | 2020.10.31 |
깃과 브랜치 (0) | 2020.10.31 |
Git으로 버전 관리하기 2 (0) | 2020.10.31 |