본문 바로가기

개발/협업

git 특정파일 변경사항 되돌리기

개발하다보면 특정파일의 변경사항을 가장 마지막의 commit 버전으로 되돌려야하는 경우가 생깁니다.


0. 현재 상황

아래는 현재의 변경사항이 일어난 파일 목록입니다.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

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:   Dockerfile
	modified:   docker-compose.yml

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

	docker-compose.yml.bak

no changes added to commit (use "git add" and/or "git commit -a")

위를 통해 현재 Dockerfile과 docker-compose.yml 에 변경사항이 생겼고, 아직 commit 되지 않은 상태임을 알 수 있습니다.


1. 수정하기

Git cli 환경에서, Dockerfile 의 변경사항을 가장 최근에 commit한 버전의 파일로 원래대로 되돌려 보겠습니다.

$ git checkout -- Dockerfile

위와 같이 입력 후 엔터를 쳤을때 아무런 오류나 메세지 없이 끝났다면 정상적으로 되돌려진 것 입니다. 


2. 확인하기

변경사항이 사라졌는지 명령어를 통해 다시 한번 확인 해보겠습니다.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

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:   docker-compose.yml

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

	docker-compose.yml.bak

no changes added to commit (use "git add" and/or "git commit -a")

아까와는 다르게 modified: Dockerfile 이 사라진것을 알 수 있습니다.



번외

모든 변경사항을 버리기

주의: 위 작업은 돌이킬 수 없습니다.

$ git checkout -- {파일명}

git checkout 명령어를 일일이 전부 입력하는 것이 아닌, 하나의 명령어로 모든 파일의 변경사항을 초기화 시키고 싶다면?

.$ git reset --hard

가장 마지막 commit 된 버전으로 모든 변경사항이 초기화됩니다.