[2장]되돌리기

한 번 되돌리면 복구할 수 없기에 주의 해야 한다. Git을 사용하면 우리가 저지른 실수는 대부분 복구할 수 있지만 되돌린 것은 복구할 수 없다.

기본

커밋 메시지만 수정하고 싶다면 --amend 명령을 사용하면 된다.

$ git commit --amend

이 명령은 Staging Area를 사용하여 커밋한다. 만약 마지막으로 커밋하고 나서 수정한 것이 전혀 없다면(커밋하자마자 바로 이 명령을 실행하는 경우) 조금 전에 한 커밋과 모든 것이 같다.

편집기가 실행되면 이전 커밋 메시지가 자동으로 포함된다. 메지시를 수정하지 않고 그대로 커밋해도 기존의 커밋을 덮어쓴다.

커밋을 했는데 Stage 하는 것을 깜빡하고 빠트린 파일이 있다면 아래와 같이 고칠 수 있다.

  • 테스트를 위해 파일을 하나 만든다. 그리고 기존 파일만 추가하고 커밋

      $ echo '되돌리기 테스' > test2.txt
      $ git add README.md
      $ git commit -m 'initial commit'
      [master (root-commit) 1121179] initial commit
       2 files changed, 2 insertions(+)
       create mode 100644 README.md
       create mode 100644 test.txt
    
      $ git status
      On branch master
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              progit_v2.1.1.pdf
              test2.txt
    
      nothing added to commit but untracked files present (use "git add" to track)
  • 방금 만든 파일을 넣어서 커밋을 다시 추가한다.

      $ git add test2.txt
      $ git commit --amend
      [master 3eab3dc] initial commit
       Date: Tue Mar 10 09:35:49 2020 +0900
       3 files changed, 3 insertions(+)
       create mode 100644 README.md
       create mode 100644 test.txt
       create mode 100644 test2.txt

    vi 에디터가 뜨고 방금 커밋한 메시지가 포함되어 나타났다.

      initial commit
    
      # Please enter the commit message for your changes. Lines starting
      # with '#' will be ignored, and an empty message aborts the commit.
      #
      # Date:      Tue Mar 10 09:35:49 2020 +0900
      #
      # On branch master
      #
      # Initial commit
      #
      # Changes to be committed:
      #       new file:   README.md
      #       new file:   test.txt
      #       new file:   test2.txt
      #
      # Untracked files:
      #       progit_v2.1.1.pdf
      #
  • 로그를 통해 커밋이 몇 번 되었는지 확인할 수 있다. (1번만 되는게 맞다)👍

      $ git log
      commit 3eab3dc3f4cfcbbf2ede630eb3a3bace12a1e83f (HEAD -> master)
      Author: 사용자아이디 <사용자@이메일>
      Date:   Tue Mar 10 09:35:49 2020 +0900
    
          initial commit

파일 상태를 Unstage로 변경하기

Staging Area와 Working Directory 사이를 왔다 갔다 할 수 있는 방법 소개

두 영역의 상태를 확인할 때마다 변경된 상태를 되돌리는 방법을 알려준다.

예를 들어 새로운 파일을 생성(CONTRIBUTE.md)해서 add 했고, 파일명을 수정하다가 오타가 났다고 가정을 하자.

$ echo '테스트' > CONTRIBUTING.md
$ git add CONTRIBUTING.md
$ git mv README.md README

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   CONTRIBUTING.md
        renamed:    README.md -> REDAMe

위 예제에서 보듯이 Changes to be committed 밑에 git restore --staged <file>... 메시지가 보인다. 이 명령으로 Unstaged 상태로 변경할 수 있다.

CONTRIBUTE.md 파일을 Unstaged 상태로 변경해보자.

$ git restore --staged CONTRIBUTING.md

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    README.md -> REDAMe

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        CONTRIBUTING.md

정상적으로 CONTRIBUTE.md 파일이 Unstaged가 되어있다.

책에서는 git reset 이라고 되어있다. 실행해보면

$ git reset HEAD REDAMe

izen@DESKTOP-2MMGPOJ MINGW64 /d/Projects/TIL (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        CONTRIBUTING.md
        REDAMe

같은 명령어인가❓❓

  • restore 레퍼런스를 보면 워킹트리 파일을 복원한다고 되어 있고,
  • reset 레퍼런스를 보면 현재 HEAD를 지정된 상태로 재설정 한다고 되어있다. (참고로 git reset 명령은 매우 위험하다. --hard 옵션과 함께 사용하면 더욱 더 위험하다. 하지만 위 예제처럼 옵션없이 사용하면 워킹 디렉토리의 파일은 건드리지 않는다.) - 이 부분은 나중에 책에서 다시 자세히 다룬다고 하니 그 때 다시 확인하자.

아직은 차이를 모르겠다.😭 일단은 현재 메시지창에 나오는 restore를 사용하는 걸로...😁

Modified 파일 되돌리기

최근 커밋된 버전 아니면 처음 Clone 했을 때처럼 워킹 디렉토리에 처음 Checkout 한 그 내용으로 되돌리는 방법은 없을까? git status 명령을 확인해 보면 친절히 알려준다. 아래 예제에서 Unstaged 부분을 보자.

먼저 CONTRIBUTE.md파일 을 수정하고, status 를 확인하자.

$ echo 'test2' >> CONTRIBUTE.md
$ git status
On branch master
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:   CONTRIBUTE.md

위의 메시지는 수정한 파일을 되돌리는 방법을 꽤 정확하게 알려준다. 알려주는 대로 한 번 해보자.

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

정상적으로 복원되었다.

하지만 책에서는 checkout 이라는 명령어를 사용했다.

  • 책에서 나온 내용 복붙

    git status 내용 확인

      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

    checkout 실행

      $ git checkout -- CONTRIBUTING.md
      $ git status
      On branch master
      Changes to be committed:
       (use "git reset HEAD <file>..." to unstage)
       renamed: README.md -> README

    git checkout -- [file] 명령은 꽤 위험한 명령이라는 것을 알아야 한다. 원래 파일로 덮어썼기 때문에 수정한 내용은 전부 사라진다. 수정한 내용이 진짜 마음에 들지 않을 때만
    사용하자.

✨✨변경한 내용을 쉽게 버릴수는 없고 당장은 되돌려야만 하는 상황이라면 Stash와 Branch를 사용하라. 이것은 추후 다시 배울 것이다.

Git으로 커밋한 모든 것은 언제나 복구할 수 있다. 삭제한 브랜치에 있었던 것도, —amend 옵션으로 다시 커밋한 것도 복구할 수 있다. 하지만 커밋하지 않고 읽어버린 것은 절대로 되돌릴 수 없다는 것을 명심하라❗❗❗

+ Recent posts