2013년 9월 5일 목요일

[Git] 인터렉티브 git add

Git의 특성 상 수정하거나 변경된 파일은 Staging이라는 절차를 거쳐야만 커밋이 가능하다. 그런데 스테이징 할 파일이 여러개이지만 전부가 아닌 경우 하나하나 일일이 입력하는 건 매우 귀찮고 시간 손해가 많은 일이다.

물론 외부 툴을 이용해서 쉽게 하는 방법도 있겠지만 여기서는 쉘(터미널/콘솔) 상에서 외부 도구 없이 Interactive Git Add 기능을 설명한다.

현재 상황을 가정해보자. Git 저장소 상태인 특정 디렉토리의 내용이다.
$ ls
file1.txt   file2.txt   readme.txt
이 디렉토리에는 readme.txt라는 이미 추가된 파일이 있고, file1.txt와 file2.txt는 추가되지 않은(untracked) 파일이다.
$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# file1.txt
# file2.txt
nothing added to commit but untracked files present (use "git add" to track)
여기서 file1.txt 파일 만을 저장소의 관리하에 두려면 git add file1.txt 라는 명령을 이용하면 되겠지만 (-_-) 여기서는 인터랙티브 기능을 일부러 사용해 보겠다.

git add -i 라는 명령을 입력한다.
$ git add -i
           staged     unstaged path


*** Commands ***
  1: status   2: update   3: revert   4: add untracked
  5: patch   6: diff   7: quit   8: help
What now>
평소에는 보기 힘든 화면이 나타난다. 아래에 커맨드라고 표시된 부분은 세부 모드로 들어갈 명령어를 알려주는 부분이다. 숫자를 누르거나 각 영문의 첫 글자를 입력하면 해당 모드로 진입한다. 별도로 설명하진 않겠지만, 1 혹은 s를 눌러서 보이는 화면은 진입시에 위에 보이는 내용과 동일한 내용을 다시 표시해 준다.

여기서는 Untracked File을 스테이징 하려는 것이니 4 혹은 a 를 입력한다. 그러면 저장소에 새로운 파일을 추가하기 위한 모드로 바뀌며 여기서 원하는 파일의 번호를 입력하면 된다. 여기서는 1번(file1.txt) 만을 입력한다.
What now> a
  1: file1.txt
  2: file2.txt
Add untracked>> 1
* 1: file1.txt
  2: file2.txt
별표 표시로 추가(add)가 되었음을 알려준다. (참고로 여러개를 한번에 입력하려면 쉼표(콤마)를 구분자로 한번에 입력해 주면 된다. 예를 들어 1,2,3 이런 식으로)

제대로 확인하기 위해 일부러 인터랙티브 모드를 종료하고 쉘에서 git status를 입력해 봐야겠다.
Add untracked>> ^D
added one path

*** Commands ***
  1: status   2: update   3: revert   4: add untracked
  5: patch   6: diff   7: quit   8: help
What now> ^D
Bye.
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
# new file:   file1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# file2.txt
^D 라는 문자가 보이는데 쉘 이용자라면 다들 알 만한 Ctrl-D 키를 입력하면 표시되는 문자다. 참고로 인터랙티브 모드에서 커맨드 모드로 진입했을 때 그 이전 단계로 돌아가고 싶다면 Ctrl-D를 누르면 된다. 물론 첫 화면에서 Ctrl-D를 누르면 인터랙티브 모드가 종료된다.

이제 추가가 되었으니 커밋을 하고 다시 스테이터스를 확인한다.
$ git commit -m "Add file1.txt"
[master 0fb4670] Add file1.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# file2.txt
nothing added to commit but untracked files present (use "git add" to track)
file1.txt가 저장소에 추가되고 커밋되었으니 남은 file2.txt만 덩그러니 보인다. 이제 다시 시험을 위해 file1.txt를 수정하고 status를 보자.
$ vi file1.txt (vim으로 파일을 수정하는 단계)
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   file1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# file2.txt
no changes added to commit (use "git add" and/or "git commit -a")
file1.txt의 수정 사항을 반영하기 위해 다시 인터랙티브 모드를 이용해보자.
$ git add -i
           staged     unstaged path
  1:    unchanged        +1/-0 file1.txt

*** Commands ***
  1: status   2: update   3: revert   4: add untracked
  5: patch   6: diff   7: quit   8: help
What now> u
           staged     unstaged path
  1:    unchanged        +1/-0 file1.txt
Update>> 1
           staged     unstaged path
* 1:    unchanged        +1/-0 file1.txt
Update>> ^D
updated one path

*** Commands ***
  1: status   2: update   3: revert   4: add untracked
  5: patch   6: diff   7: quit   8: help
What now> ^D
Bye.
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   file1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# file2.txt
이미 추가된(tracked) 파일의 경우 인터랙티브 화면에서 u 혹은 2키를 눌러 update 모드로 진입해서 해당 파일의 번호를 누름으로써 스테이징이 완료된다. 물론 여기서는 생략했지만 남은 일은 commit을 하는 것이다.

이 외에도 인터랙티브 모드에선 많은 기능이 제공되고 있으니 원하는 모드 번호나 명령어를 눌러서 테스트 해 보자.

댓글 없음 :