Git, 분산 버전 관리 시스템 4장 Git 기초: 추가하고 커밋하기 본문

Programming

Git, 분산 버전 관리 시스템 4장 Git 기초: 추가하고 커밋하기

halatha 2010. 4. 20. 15:12
4.1 파일 추가하기

스테이징된 변경 사항: 저장소에 반영하려는 작업 트리의 변경 사항일 뿐, 저장소에 커밋하기 전에 커밋을 준비하는 위치

index.html의 Biography link를 About으로 변경

/mysite-remote$ git add -i    #    대화명 모드 실행
           staged     unstaged path
  1:    unchanged        +1/-1 index.html

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 1    #    상태 정보 출력
           staged     unstaged path
  1:    unchanged        +1/-1 index.html

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 2    #    staging할 수 있는 파일 목록 출력
           staged     unstaged path
  1:    unchanged        +1/-1 index.html
Update>> 1    #    대상 파일 번호 입력하면 staging 대상임을 확인
           staged     unstaged path
* 1:    unchanged        +1/-1 index.html
Update>>    #    엔터키를 입력해 메인 메뉴로 돌아감
updated one path

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help

What now> 1    #    상태 정보 확인 > stating된 변경 사항이 하나 있고, staging되지 않은 변경 사항 목록에는 아무 것도 없음
           staged     unstaged path
  1:        +1/-1      nothing index.html

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 3    #    변경 사항 취소
           staged     unstaged path
  1:        +1/-1      nothing index.html
Revert>> 1    #    파일 번호 선택
           staged     unstaged path
* 1:        +1/-1      nothing index.html
Revert>>    #    엔터를 입력해 메인 메뉴로 돌아옴
reverted one path

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 1    #    상태 정보 확인 > 다시 staging되지 않음
           staged     unstaged path
  1:    unchanged        +1/-1 index.html

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now>

What now> 5    #    패치 모드 실행
           staged     unstaged path
  1:    unchanged        +1/-1 index.html
Patch update>> 1
           staged     unstaged path
* 1:    unchanged        +1/-1 index.html
Patch update>>
diff --git a/index.html b/index.html
index e812d0a..ca86894 100644
--- a/index.html
+++ b/index.html
@@ -6,7 +6,7 @@
 <body>
     <h1>Hello World!</h1>
     <ul>
-        <li><a href="bio.html">Biography</a></li>
+        <li><a href="about.html">About</a></li>
     </ul>
 </body>
 </html>
Stage this hunk [y,n,q,a,d,/,e,?]?    #    y/n 변경 사항 staging 실행/실행 안 함, a 파일에 있는 나머지 모든 변경 사항 추가, d 나머지 모든 변경 사항 추가하지 않음

Stage this hunk [y,n,q,a,d,/,e,?]? n

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now> 7
Bye.

/mysite-remote$ git add -p    #    패치 모드를 바로 실행
diff --git a/index.html b/index.html
index e812d0a..ca86894 100644
--- a/index.html
+++ b/index.html
@@ -6,7 +6,7 @@
 <body>
     <h1>Hello World!</h1>
     <ul>
-        <li><a href="bio.html">Biography</a></li>
+        <li><a href="about.html">About</a></li>
     </ul>
 </body>
 </html>
Stage this hunk [y,n,q,a,d,/,e,?]? y    #    staging하고 commit할 준비

4.2 변경 사항 커밋하기

git commit -m "message"
git commit > log message 작성을 위한 editor 실행
    1. GIT_EDITOR 환경변수
    2. Git의 core.editor 구성변수
    3. VISUAL 환경변수
    4. EDITOR 환경변수
    5. 설정된 환경변수가 없으면 vi 이용
    editor 이용시 -v option을 추가하면 commit하려는 변경 사항과 저장소의 차이점을 editor에 표시(#으로 시작하는 줄은 포함하지 않음)

1. commit하려는 파일 지정: 변경의 일부분만 추가하는 경우 -p option 사용
git add [-p] [file name]
git commit -m "log message"

2. 작업 트리의 가장 최근 버전으로 commit
git commit -m "log message" -a

3. 하나 이상의 파일 지정: 지정한 파일에 대해 가장 최근 버전을 commit
git commit -m "log message" [file name]

1. staging한 시점의 변경 사항을 commit > staging 영역은 일종의 buffer
2. 3. commit하는 시점의 변경 사항을 commit

* alias 사용
git config --global alias.ci "commit"

4.3 변경된 내용 살펴보기

현재 상태 보기

/mysite$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   index.html
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       mysite-1.0.tar.gz
#       mysite-1.0.zip

/mysite$ vi index.html    #    about link 뒤에 contact link 추가

/mysite$ git status    #    첫 번째는 staging, 두 번째는 staging 전 변경 사항
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   index.html
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   index.html
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       mysite-1.0.tar.gz
#       mysite-1.0.zip

차이점 살펴보기

/mysite$ git diff    #    작업 트리의 영역과 staging 영역간의 비교
diff --git a/index.html b/index.html
index ffadc4c..a57feaf 100644
--- a/index.html
+++ b/index.html
@@ -7,6 +7,7 @@
        <h1>Hello World!</h1>
        <ul>
                <li><a href="about.html">About</a></li>
+               <li><a href="contact.html">Contact</a></li>
        </ul>
 </body>
 </html>

/mysite$ git diff --cached    #    staging 영역과 저장소의 차이점 확인. staging되지 않은 변경 사항은 표시 하지 않음
diff --git a/index.html b/index.html
index 35d304c..ffadc4c 100644
--- a/index.html
+++ b/index.html
@@ -6,7 +6,7 @@
 <body>
        <h1>Hello World!</h1>
        <ul>
-               <li><a href="bio.html">Biography</a></li>
+               <li><a href="about.html">About</a></li>
        </ul>
 </body>
 </html>

-는 Biography 영역이 삭제된 것을 의미

/mysite$ git diff HEAD    #    작업 트리, staging된 변경 사항, 저장소의 모든 차이점 비교
diff --git a/index.html b/index.html
index 35d304c..a57feaf 100644
--- a/index.html
+++ b/index.html
@@ -6,7 +6,8 @@
 <body>
        <h1>Hello World!</h1>
        <ul>
-               <li><a href="bio.html">Biography</a></li>
+               <li><a href="about.html">About</a></li>
+               <li><a href="contact.html">Contact</a></li>
        </ul>
 </body>
 </html>

HEAD는 현재 작업중인 브랜치에서 가장 최신의 commit을 나타내는 keyword

/mysite$ git commit -a -m "Changes biography link and add contact link" \
> -m "About is shorter, so easier to process" \
> -m "We need to provide contact info"
[master 38c1612] Changes biography link and add contact link
 1 files changed, 2 insertions(+), 1 deletions(-)
/mysite$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       mysite-1.0.tar.gz
#       mysite-1.0.zip
nothing added to commit but untracked files present (use "git add" to track)

4.4 파일 관리하기

파일 이름을 변경하고 파일 이동하기

/mysite$ git mv index.html hello.html
/mysite$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       renamed:    index.html -> hello.html

/mysite$ git commit -m "rename to more appropriate name"
[master a529f56] rename to more appropriate name
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename index.html => hello.html (100%)

파일 복사하기: 6.5절 참조

파일 무시하기

이전으로 돌아가 vi로 index.html을 연 경우

/mysite-remote$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   index.html
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .index.html.swp

.swp를 추가할 필요는 없으므로 해당 파일을 .gitignore에 추가하면 저장소에서 사라짐

/git-1.7.0.5$ vi .gitignore

.*.swp 추가

지역 저장소에서만 해당 파일을 무시하도록 설정하는 경우 .git/info/exclude에 추가

/mysite$ vi .git/info/exclude
Comments