Git에서 변경 사항 실행 취소

John Wachira 2024년2월15일
  1. git log 명령을 사용하여 Git에서 커밋 기록 확인
  2. git clean 명령을 사용하여 Git의 변경 사항 실행 취소
  3. git revert 명령을 사용하여 Git에서 변경 사항 실행 취소
  4. git reset 명령을 사용하여 Git에서 변경 사항 실행 취소
Git에서 변경 사항 실행 취소

이 자습서에서는 로컬 및 원격 저장소에 대한 변경 사항을 실행 취소하는 다양한 명령을 다룹니다. 이러한 명령에는 다음이 포함됩니다.

  1. git clean
  2. git reset
  3. git revert

git log 명령을 사용하여 Git에서 커밋 기록 확인

먼저 커밋 기록을 확인하는 방법을 살펴보겠습니다. git log 명령을 사용하여 이를 확인합니다.

pc@JOHN MINGW64 ~/Git (main)
$ git log
commit 195e5c362975354d62ebc469da2e3cd276c7da03 (HEAD -> main)
Author: John <wachirajohnie11@gmail.com>
Date:   Mon Feb 21 12:11:12 2022 +0300
commit 7b19db4b35c1ca15e5ecb8df1f805d44aad62e5f
Author: John <wachirajohnie11@gmail.com>
Date:   Mon Feb 21 10:09:31 2022 +0300
   first commit

이 명령은 작업 분기의 기록만 표시합니다. 모든 분기를 확인하려면 아래 명령어를 사용하세요.

git log --branches=*

git checkout 명령을 사용하여 아래와 같이 특정 커밋을 봅니다.

Git checkout

이제 프로젝트의 현재 상태를 변경하지 않고 파일에 액세스할 수 있습니다. git checkout main 명령을 사용하여 현재 프로젝트 작업 공간으로 돌아갑니다.

git clean 명령을 사용하여 Git의 변경 사항 실행 취소

git clean 명령을 사용하여 추적되지 않은 파일의 변경 사항을 취소합니다. git clean 명령은 실행 취소할 수 없습니다.

  1. git clean -n 옵션은 tryout 명령으로 작동합니다. 추적되지 않은 파일만 표시하지만 삭제하지는 않습니다.

예시:

```bash
pc@JOHN MINGW64 ~/Git (main)
$ git clean -n
Would remove Hello world.txt
Would remove Test1.txt
```
  1. git clean --force 명령은 작업 디렉토리에서 추적되지 않은 모든 파일과 폴더를 삭제합니다. .gitignore 명령을 사용하여 예외를 throw할 수 있습니다.

예시:

```bash
pc@JOHN MINGW64 ~/Git (main)
$ git clean --force
Removing Hello world.txt
Removing Test1.txt
```

파일이 디렉토리에 있는지 확인합시다.

```bash
pc@JOHN MINGW64 ~/Git (main)
$ git status
On branch main
nothing to commit, working tree clean
```

출력에 깨끗한 분기가 표시됩니다. 명령이 파일을 성공적으로 삭제했습니다.

git revert 명령을 사용하여 Git에서 변경 사항 실행 취소

git revert 명령은 커밋으로 인한 변경 사항을 적용하고 덮어씁니다. 우리는 그것을 사용하여 지점으로 돌아가서 변경합니다.

명령을 사용하여 첫 번째 커밋을 변경해 보겠습니다.

git revert first commit
#[first commit [7b19db4] Revert 'first commit'
#1 file changed, 1 deletion(-)

커밋 참조를 전달해야 합니다.

git reset 명령을 사용하여 Git에서 변경 사항 실행 취소

마지막으로 다룰 명령은 git reset입니다. 이 명령은 HEAD에서 작동합니다.

  1. git reset --hard 옵션은 지정된 커밋을 변경합니다. 그러나 스테이징 인덱스 및 작업 디렉터리의 모든 변경 사항은 변경 사항과 일치합니다. 모든 준비된 커밋은 프로세스에서 손실됩니다.

    예시:

    $ git reset --hard
    HEAD is now at 78129a6 Revert "$first commit"
    

    명령은 HEAD로 하드 리셋을 실행합니다.

  2. git reset --mixed 명령은 스테이징 인덱스의 변경 사항을 작업 디렉토리에 저장합니다.

    예시:

    $ git reset --mixed
    Unstaged changes after reset:
    M       .bash_history
    M       text.txt.txt
    M       text.txt.txt.bak
    

    명령은 실행 취소된 변경 사항을 출력에서 ​​작업 디렉토리에 저장했습니다. 지점의 상태를 확인합시다.

    $ git status
    On branch main
    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:   .bash_history
    		modified:   text.txt.txt
    		modified:   text.txt.txt.bak
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    		.bash_history.bak
    no changes added to commit (use "git add" and/or "git commit -a")
    
  3. git reset --soft 명령은 커밋 기록만 변경합니다. 기본 포인터는 항상 HEAD입니다.

    예시:

    pc@JOHN MINGW64 ~/Git (main)
    $ git reset --soft
    pc@JOHN MINGW64 ~/Git (main)
    $ git status
    On branch main
    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:   .bash_history
    		modified:   text.txt.txt
    		modified:   text.txt.txt.bak
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    		.bash_history.bak
    no changes added to commit (use "git add" and/or "git commit -a")
    

    위의 출력에서 ​​작업 디렉토리와 스테이징 인덱스는 변경되지 않았습니다. 커밋의 해시를 사용하여 특정 커밋을 재설정합니다.

작가: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

관련 문장 - Git Reset