파일 상태를 Staged 에서 Unstaged로 변경하기
Staging Area는 커밋대상 파일들의 목록으로 담아두는 공간인데, git add 명령을 사용하여 등록합니다. 그런데 다음 커밋에 반영하려던 파일을 실수로 git add 해버리는 경우가 있을 수 있습니다. 이상태에서 git commit을 해버리면 그대로 해당 파일이 Git 저장소에 반영되어 버릴 것입니다.

이런 경우 Staging Area에 등록했던 파일을 워킹디렉터리로 되돌리는 방법이 있는데, git reset HEAD <파일> 명령을 사용하는 것입니다.

다음의 예제를 보면 aa 파일과 bb 파일이 있는데 aa파일은 다음 커밋에 반영하고 이번 커밋에는 bb파일만 반영하려 했습니다만 git add *  명령을 통해 실수로 두 개의 파일 모두를 Staging Area에 등록해 버렸습니다. git status로 상태를 보니 역시나 두 파일 모두 커밋 대상으로 Staged 되었습니다.
$ git add *

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   aa
        modified:   bb


그리고 친절하게도 도움말 아래 되돌리는 방법을 설명해 두었네요. aa 파일은 다음 커밋에 반영할 것이므로 워킹디렉터리로 되돌려 봅니다.
git reset HEAD aa 명령으로 aa파일을 워킹 Staging Area에서 다시 빼내었습니다.
$ git reset HEAD aa
Unstaged changes after reset:
M       aa


$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   bb

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:   aa


git reset 명령어는 매우 위험한데, --hard 옵션과 사용하면 더욱 위험합니다. 그러나 옵션 없이 사용하는 경우 워킹 디렉터리의 작업 파일은 변경되지 않습니다. git reset 명령어는 다음에 더욱 자세히 알아보도록 하겠습니다.


출처 - https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things

블로그 이미지

도로락

IT, 프로그래밍, 컴퓨터 활용 정보 등을 위한 블로그

,