깃허브(2): 커밋

리린·2021년 8월 26일
0

github

목록 보기
5/8

커밋이란

  • 변경된 부분만 시간순서에 따라서 관리해주는 것
  • 시간적 변화도 함께 저장

git init : 저장소를 깃으로 초기화

  • .git/폴더 생성
  • 성공 메시지
$ mkdir gitstudy04 -------- 새 폴더 만들기
$ cd gitstudy04 -------- 만든 폴더로 이동
$ git init -------- 저장소를 깃으로 초기화
Initialized empty Git repository in E:/gitstudy04/.git/

infoh@hojin MINGW64 /e/gitstudy04 (master)
$ code index.htm -------- VS Code를 사용하여 파일 작성

git status : 상태 확인

  • 메시지:
infoh@hojin MINGW64 /e/gitstudy04 (master)
$ git status -------- 상태 확인
On branch master
No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        index.htm -------- 새로운 파일이 등록된 것을 확인
nothing added to commit but untracked files present (use "git add" to track)

git add (파일이름): 워킹 디렉토리 파일을 스테이지 영역에 등록

  • git은 add 명령어를 기준으로 이전과 이후 단계를 구분

  • git add . : 전체 파일을 스테이지 영역에 등록하기

  • 단, 빈 폴더는 등록되지 않는다.

  • 메시지

$ git add index.htm -------- 스테이지에 등록

git rm --cached (파일이름) : 파일 등록 취소

  • rm: remove. 삭제하다
  • cached: 스테이지 영역에서만 등록한 파일을 삭제하는 옵션

git reset HEAD index.htm : 이미 커밋한 파일을 등록 취소

  1. 커밋한 파일을 취소했을 경우
  • 삭제 혹은 변경된 것으로 간주한다
  • 메시지:
infoh@hojin MINGW64 /e/gitstudy04 (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        deleted:   index.htm
Untracked files:
  (use "git add <file>..." to include in what will be committed)
index.htm -------- 스테이지 삭제

git mv (파일이름) (새 파일이름): 파일 이름 변경하기

  • 메시지:
infoh@hojin MINGW64 /e/gitstudy04 (master)
$ git mv index.htm home.htm -------- 파일 이름 변경   
$ git status
On branch master
No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   home.htm -------- 변경된 파일 이름
  • 이름 변경 뒤에는 rm 과 add 명령어를 사용해야 한다.

커밋 이론

  1. HEAD
  • HEAD는 최종(부모) 커밋을 가리킨다.
  1. 스냅샷
  • HEAD를 기준으로 사진을 찍듯 저장
  1. status
  • status에 변경이 있어야 커밋이 가능하다.
  • 커밋하기 전에 늘 status를 확인하는 습관을 가지자.
  1. 커밋
  • HEAD와 스테이지 영역 간 차이를 비교하여 새로운 객체를 생성
  • 생성된 객체를 깃 저장소에 기록

git commit: 커밋 메시지를 작성할 수 있는 명령어

  1. git commit -a: git add . + git commit

  2. vi 에디터를 이용한 메시지

  3. 기본적인 커밋 메시지

  • 새로운 내용 입력: esc 누른 후 i 누르기
  • 저장과 종료: esc 누른 후 :wq 입력하기 ( :를 할 때는 shift와 ; 키를 같이 눌러야 한다 )
  • 종료: esc 누른 후 : + q 누르기
  • git commit 명령어들
  • https://blockdmask.tistory.com/25
  1. 커밋메시지 작성 요령
  • 요약 내용과 상세 내용을 분리하여 기록하기
  • 첫째 줄에는 제목, 중간 빈 줄로 구분(선택) 다음 줄에 상세 내용
  • 제목은 명령어
  • 첫 단어는 대문자
  • 관련 텍스트
  1. 커밋메시지 완료한 다음 상태확인
  • git status: 상태 확인
  1. git checkout --(수정파일이름)
  • 바로 이전 commit으로 되돌리기
  1. git commit -m "커밋메시지" : 간단한 커밋메시지로 커밋하기

  2. git commit --allow-empty-message-m ""

  • 빈칸 커밋도 가능하나 권장하지 않는다.
  1. git commit --amend:
    마지막으로 작성한 커밋 메시지를 수정
  1. git commit -am "커밋메시지" : git add . + git commit -m"커밋메시지"
  • 가장 많이 사용한다

git log: 로그 기록 확인

  • 소스트리로 확인하는 것을 추천한다
  1. 메시지
$ git log
commit 976a6b7bb40c2af75d4d84ce290295ff353a19a5 (HEAD -> master)
Author: nahyunbak <nahyunbak@naver.com>
Date:   Thu Aug 26 21:21:57 2021 +0900

    Add initial frontend code

    landing pages

nahyu@nahyeon MINGW64 ~/Downloads/webd-master/실습/writingapp/frontend (master)
  1. 배경지식
  • 깃을 처음 생성하면 자동으로 master 브랜치 1개를 생성한다
  • 커밋은 master 브랜치 안에 기록된다
  • 소스트리의 master 브랜치를 선택하면 커밋 내역을 볼 수 있다.
    (즉 깃 bash 와 소스트리를 같이 이용하기를 권장한다)
  • 커밋 아이디는 SHA1 이라는 해시 알고리즘으로 만든 것이다. (40자리의 hexa 값)
  • 단축키를 사용한다 (앞쪽 7자리 숫자)
  1. git log --pretty-short: 간략 로그( ㅊ첫 번째 줄의 커밋 메시지만 출력)
infoh@hojin MINGW64 /e/gitstudy04 (master)
$ git log --pretty=short -------- 로그 확인  
commit aa92947d350db27b604d1351930d4f809f96886e (HEAD -> master)
Author: hojin <infohojin@gmail.com>

commit aa1dd51a8883b2ea9a54209a00f434a2da01ee85
Author: hojin <infohojin@gmail.com>
    hello git world 추가

commit e2bce41380691b0a34aeab7db889a6c30fed8287
Author: hojin <infohojin@gmail.com>
    인덱스 페이지 레이아웃
  1. git show 커밋id: 커밋의 상세정보 확인하고 싶을 때
$ git show 976a6b7bb40c2af75d4d84ce290295ff353a19a5
commit 976a6b7bb40c2af75d4d84ce290295ff353a19a5
Author: nahyunbak <nahyunbak@naver.com>
Date:   Thu Aug 26 21:21:57 2021 +0900

    Add initial frontend code

    landing pages

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4d29575
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,23 @@
:
  1. log 명령어의 옵션
  • -p 옵션: diff(수정한 라인 비교) 를 같이 포함하여 출력
  • --stat 옵션: 히스토리를 출력
  • --pretty-oneline 옵션: 각 커밋을 한 줄로 표시

git diff : 직접적으로 비교하기

  1. git diff: 워킹 디렉터리 vs 스테이지 영역
  • 아직 add 명령어로 파일을 추가하지 않은 경우, 워킹 디렉터리와 스테이지 영역 간 변경 사항을 비교할 수 있다
infoh@hojin MINGW64 /e/gitstudy04 (master)
$ git diff  -------- 스테이지 vs 워킹 디렉터리 비교
diff --git a/index.htm b/index.htm
index f5097d9..56af0de 100644
--- a/index.htm
+++ b/index.htm
@@ -7,5 +7,6 @@
 </head>
 <body>
     <h1>hello GIT world!</h1>
+    <h2>깃을 이용하면 소스의 버전 관리를 쉽게 할 수 있습니다.</h2> -------- 추가  
 </body>
 </html>
\ No newline at end of file
  1. git diff head: 워킹 디렉터리 vs 가장 마지막 커밋
infoh@hojin MINGW64 /e/gitstudy04 (master)
$ git diff head --------   
diff --git a/index.htm b/index.htm
index f5097d9..56af0de 100644
--- a/index.htm
+++ b/index.htm
@@ -7,5 +7,6 @@
 </head>
 <body>
     <h1>hello GIT world!</h1>
+    <h2>깃을 이용하면 소스의 버전 관리를 쉽게 할 수 있습니다.</h2>
 </body>
 </html>
\ No newline at end of file
  1. git commit -v :
  • vi 에디터에서 diff 내용을 추가할 수 있다.

소스트리

( 그래프, 커밋메시지, 작성자, 날짜, 커밋아이디 )

주의사항:

  1. 디렉토리 안에 .git 폴더가 있으면 지우자.

  2. 프론트엔드와 백엔드는 별도로 관리하는 게 맞다.

  3. git log의 시간 기록은 믿을 수 없다. 작업 중인 컴퓨터가 다른 지역의 시간 등으로 잘못 설정되어 있을 수 있기 때문이다.

profile
개발자지망생

0개의 댓글