티스토리 뷰

https://stackoverflow.com/questions/215718/how-do-i-reset-or-revert-a-file-to-a-specific-revision

 

How do I reset or revert a file to a specific revision?

How do I revert a modified file to its previous revision at a specific commit hash (which I determined via git log and git diff)?

stackoverflow.com

https://stackoverflow.com/questions/949314/how-do-i-get-the-hash-for-the-current-commit-in-git

 

How do I get the hash for the current commit in Git?

How do I get the hash of the current commit in Git?

stackoverflow.com

 

작업하면서 git reset --hard 명령어를 자주쓴다.

 

그런데 git reset --hard 명령어의 경우 프로젝트의 모든 파일에 대한 수정내역을 reset or revert 시켜버린다.

 

특정 파일만 reset 할 수 있는 방법이 존재한다!(VSCode git extension 쓰면 클릭한번으로 될일이다!)

 

1. 먼저 복구하고자 하는 버전의 commit id를 불러와야한다.

 

내 경우 가장 최근 commit id가 복구하고자 하는 버전이였다. 이 경우 아래의 명령어를 입력하면 commit id를 cli 상으로 출력할 수 있다.

git rev-parse HEAD

 

아래 처럼 출력이 됐다.

 

(torch1131) PS C:\Users\yonghye\Documents\Workspace\Crowd-Counting> git rev-parse HEAD
982d08d2bd1adc651f27c8192b6093288776a223

 

982d08d2bd1adc651f27c8192b6093288776a223 이게 commit id가 되는거다.

 

2. 특정 파일만 reset 하기

 

특정 파일만 reset 하는 명령어는 아래와 같다.

git checkout commit-id -- file1/to/restore file2/to/restore

 

예시 1.) 

 

여기서 commit-id 는 내 경우로 치면 과정 1. 에서 출력한 982d08d2bd1adc651f27c8192b6093288776a223 가 되는 것이다.

 

그리고 내 경우에는 복구할 파일이 1개 였다.  이 파일이 example.py 라고 하면 아래와 같이 명령어를 입력하면 된다.

git checkout 982d08d2bd1adc651f27c8192b6093288776a223 -- example.py

 

예시 2.)

 

commit-id: 100

reset할 파일들: example.py, pkg/build.py

git checkout 100 -- example.py pkg/build.py

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함