개발개발/ETC

[Git] Staged 된 파일 삭제

kyeoneee 2021. 3. 20. 16:24
반응형

개요

모든 파일을 add하여 stage 상태로 변경하고 -m 옵션을 이용해 커밋하여 불필요한 파일까지 커밋하는 경험이 자주 있었다.
이런 경우 불필요한 파일을 Staging Area에서 삭제하여 필요한 변경 내역만 커밋하는 방법을 정리하려 한다.

용어 정리

Git의 파일 상태 관리

Git은 init된 순간부터 디렉토리의 모든 파일을 아래의 상태들로 분류하여 관리한다.

  • Tracked : 관리 대상 파일, 한번이라도 스냅샷에 포함되면 tracked 파일이 됨
    • Staged : 커밋으로 저장소에 기록할 상태
    • Unmodified : 마지막 스냅샷 이후로 변경이 없는 상태
    • Modified : 마지막 스냅샷으로부터 변경 내역이 있지만 staged되지 않은 상태
  • Untracked : 관리 대상에 포함되지 않는 파일들

Staged 된 파일 삭제

먼저, 현재 상태를 살펴본다.
git status 명령어를 통해 아래와 같은 결과가 나왔을 때, -m 옵션과 함께 commit 하게 되면 Changed to be commit 에 있는 모든 파일이 커밋된다. 그런데 PracticeSpringJpaApplication.java 파일만 제외하고 커밋하고 싶다면 해당 파일을 staged 상태에서 없애주면 된다.

$ git status                                                 
On branch main
Changes to be committed:  # 변경 이후 staged에 올라간 상태
  (use "git restore --staged <file>..." to unstage)
        modified:   src/main/java/com/practicespringjpa/PracticeSpringJpaApplication.java
        new file:   src/test/java/com/practicespringjpa/chapter3/entity/Chap3MemberTest.java
        new file:   src/test/java/com/practicespringjpa/chapter3/repository/Chap3MemberJpaRepositoryTest.java
        new file:   src/test/java/com/practicespringjpa/chapter3/repository/Chap3MemberRepositoryTest.java
        deleted:    src/test/java/com/practicespringjpa/entity/MemberTest.java
        deleted:    src/test/java/com/practicespringjpa/repository/MemberJpaRepositoryTest.java
        deleted:    src/test/java/com/practicespringjpa/repository/MemberRepositoryTest.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        src/main/java/com/practicespringjpa/chapter4/

staged 상태에서 없애주기 위해 git reset HEAD -- path/to/file 명령어를 사용하면 된다.

아래와 같이 staged 상태에서 PracticeSpringJpaApplication.java 파일을 위 명령어로 삭제해주면 Modified 상태임을 표시하는 Changes not staged for commit: 파일 내역으로 옮겨진 것을 확인할 수 있다.

$ git reset HEAD -- src/main/java/com/practicespringjpa/PracticeSpringJpaApplication.java                                                  
Unstaged changes after reset:
M       src/main/java/com/practicespringjpa/PracticeSpringJpaApplication.java
$ git status                                                                                    
On branch main
Changes to be committed:   # 변경 이후 staged에 올라간 상태
  (use "git restore --staged <file>..." to unstage)
        new file:   src/test/java/com/practicespringjpa/chapter3/entity/Chap3MemberTest.java
        new file:   src/test/java/com/practicespringjpa/chapter3/repository/Chap3MemberJpaRepositoryTest.java
        new file:   src/test/java/com/practicespringjpa/chapter3/repository/Chap3MemberRepositoryTest.java
        deleted:    src/test/java/com/practicespringjpa/entity/MemberTest.java
        deleted:    src/test/java/com/practicespringjpa/repository/MemberJpaRepositoryTest.java
        deleted:    src/test/java/com/practicespringjpa/repository/MemberRepositoryTest.java

Changes not staged for commit:  # 변경은 있지만 staged에 올라가지 않은 상태 (Modified)
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   src/main/java/com/practicespringjpa/PracticeSpringJpaApplication.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        src/main/java/com/practicespringjpa/chapter4/
반응형