Git의 기초 - 수정하고 저장소에 저장

2020. 3. 1. 16:25Git Study

수정하고 저장소에 저장

만질 수 있는 git 저장소 생성 후, working directory에 checkout도 했다. 여기서는 파일을 수정하고 파일의 스냅샷을 commit 해본다.
working directory의 모든 파일은 track(관리대상)Untracked(아직관리하지않음) 으로 나뉜다. 여기서 다시 tracked file은 Unmodified(수정하지 않음)modified(수정함) 그리고 staged(commit으로 저장소에 기록할) 상태 중 하나이다. 말하자면 git이 파악(track)하고 있는 파일이란 것이다.

그리고 나머지 파일은 모두 Untracked 파일이 되겠다. Untracked file은 working directory file 중, 스냅샷에도, staging area에도 포함되지 않은 파일이다. 처음 저장소를 Clone하면 모든 파일은 Tracked이면서 Unmodified 상태이다. 파일을 checkout하고 나서 아무것도 수정하지 않았기 때문이다.
마지막 commit 이후, 아직 아무것도 수정하지 않은 상태에서 어떤 파일을 수정하면 git은 그 파일을 modified 상태로 인식한다. 실제로 커밋하기 위해서는 이 수정한 파일을 staged 상태로 만든 후, stgaed 상태의 파일을 커밋해야 한다.

file status

보통 git status명령을 사용한다. Clone한 직후, 바로 이 명령을 실행하면 아래와 같은 메시지를 볼 수 있다.

  $ git status
  On branch master
  Your branch is up-to-date with 'origin/master'.
  nothing to commit, working directory clean.

위의 메시지는 Tracked 파일은 하나도 수정되지 않았다는 것이다. Untracked file은 아직 없어서 목록에 나타나지 않는다. 만약 여기서 README파일을 만들면, 새로 만든 파일이기에 git status를 실행하면 'Untracked files'에 들어있다.

$ echo 'My Project' > README
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    README

nothing added to commit but untracked files present (use "git add" to track)

Git은 Untracked file을 아직 스냅샷(commit)에 넣어지지 않은 파일이라고 보기 때문에, 파일이 tracked status가 되기 전까지는 절대 그 파일을 commit하지 않는다.

Modified 상태의 파일을 Stage하기

이미 tracked 상태인 파일을 수정한다고 하자. CONTRIBUTING.md라는 파일을 수정하고 나서, git status로 살펴보면 아래와 같이 나온다.

  $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   README

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:   CONTRIBUTING.md

CONTRIBUTING.md파일은 "Changes not staged for commit"에 있다. 이것은 수정한 파일이 Tracked 상태이지만, 아직 staged 상태는 아니라는 것이다. git add명령어를 사용하면 되는데, 이 명령은 파일을 새로 추적할 때도 사용되고, 수정된 파일을 staged 상태로 만들때나, Merge할 때 충돌난 상태의 파일을 Resolve 상태로 만들때도 사용한다.

add의 의미는 프로젝트에 파일을 추가한다기보다, 다음 commit에 추가한다고 생각하는게 좋다.

이제 git status를 통해 확인하면 둘 모두 staged 상태이므로 다음 commit에 포함된다. 그런데 여기서 더 수정해야 함을 알게되어 CONTRIBUTING.md를 수정하고, 다시 git status를 확인하면...

$ vim CONTRIBUTING.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   README
    modified:   CONTRIBUTING.md

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:   CONTRIBUTING.md

?!?!?!!?? CONTRIBUTING.mdStaged 이면서 Unstage로 나온다. 이런 일이 가능한 이유를 설명해보자.
git add명령은 Git으로 하여금, 파일을 바로 staged 상태가 만든다. 지금 이 시점에서 commit 명령을 실행하면, current가 아닌 마지막으로 git add를 실행했을 때의 버전이 commit된다. 그러므로 재차 git add를 해주어, 최신 버전의 staged 상태로 만들어줘야 한다.

$ git add CONTRIBUTING.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   README
    modified:   CONTRIBUTING.md

git status가 보여주는 내용이 많다고 생각되면 git status -s 혹은 git status --short를 하면 짤막하게 보여준다. 다음을 살펴보자.

 $ git status -s
 M README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt

아직 Untracked인 상태에서는 ?? 표시가 붙는다. Staged 상태로 추가한 파일 중, 새로 생성한 파일 앞에는 A가, 수정한 파일 앞에는 M 표시가 붙는다. 이 표시는 두칸에 걸쳐서 보여지는데 왼쪽은 staging area상태이고 오른쪽은 working tree에서의 상태이다. 만약 MM이라는 표시가 붙었다면, 변경하고 staged 상태로 추가한 후, 또 내용을 변경하여 staged이면서 Unstaged인 상태이다.