git 파일을 스테이징 영역 버전으로 되돌리려면 어떻게합니까?


80

라는 파일이 있다고 가정 해 보겠습니다 a.txt. 스테이징 영역에 추가 한 다음 수정합니다. 추가했을 때의 상태로 되돌리려면 어떻게해야합니까?

답변:


77
  • Git 2.23 이전 : git checkout a.txt
  • Git 2.23부터 : git restore a.txt

을 입력하면 Git이이를 알려줍니다 git status.

Git 2.23 이전 :

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   a
#
# 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:   a
#

Git 2.23부터 :

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   a

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   a

3
@Daenyth 게시하기 전에 확인한 결과, 다른 상태 (스테이지 vs.

1
@Daenyth- 'git checkout branch-name path'또는 'git checkout HEAD path'를 생각하고 있습니다.
William Pursell

@ 윌리엄 : 감사합니다! 이제 훨씬 더 의미가 있습니다.
Daenyth

새 파일에 대해서는 작동하지 않으므로 객체가 필요하므로 스테이징에서 실제로 체크 아웃하지 않습니다. 스테이징에서 어떻게 체크 아웃합니까? 편집-- 상태가 말하는 것처럼 작동했습니다 .
Rudie 2014 년

30

git checkout -- a.txt

이 페이지의 다른 답변에는가 없으므로 --혼란이 발생했습니다.

다음을 입력 할 때 Git이 알려주는 내용입니다 git status.

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   a
#
# 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:   a
#

4
이전에 인용 된 내용을 게시하는 대신 차이점을 알려주는 것이 더 낫습니다.
Bachsau

2

준비된 파일 준비 해제

다음 두 섹션에서는 스테이징 영역 및 작업 디렉토리 변경 작업 방법을 보여줍니다. 좋은 점은이 두 영역의 상태를 확인하는 데 사용하는 명령이 변경 사항을 취소하는 방법을 상기시켜 준다는 것입니다. 예를 들어, 두 개의 파일을 변경했고이를 두 개의 개별 변경 사항으로 커밋하려고하지만 실수로 git add *를 입력하고 둘 다 스테이징했다고 가정 해 보겠습니다. 둘 중 하나를 어떻게 해제 할 수 있습니까? git status 명령은 다음을 알려줍니다.

$ git add *
$ git status

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

renamed:    README.md -> README
modified:   CONTRIBUTING.md

“Changes to be commit”텍스트 바로 아래에 use git reset HEAD ... to unstage라고 표시되어 있습니다. 따라서이 조언을 사용하여 CONTRIBUTING.md 파일을 언 스테이징 해 보겠습니다.

$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M   CONTRIBUTING.md

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

renamed:    README.md -> 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 파일이 수정되었지만 다시 한 번 준비 해제되었습니다.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.