데브림의 블로그 포스팅 한 것들을 한 눈에 확인하고 싶다면 클릭!
👉 https://github.com/DevLimK1/tistory-map 👈
나의 소소한 기록이 누군가에게는 도움이 되기를 바라며...
파일들이 삭제되기전까지의 과정
(파일복구하는 방법만을 알고 싶다면 아래쪽으로 스크롤 하셔서 복구하는 과정만 참고하시면 되겠습니다~)
1. github repository에 작업한 프로젝트 파일을 push하기 위해 D드라이브에서 새로운 프로젝트 폴더를 만들고
git init 입력
2. git add .
3. git commit -m "upload"
4. git push origin master 를 했는데
-----------------------------git bash----------------------------------
Lim@DESKTOP /d/HealthInMoaPrj (master)
$ git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
---------------------------------------------------------------
위와 같은 에러 발생
5. remote repository 할 공간이 없다는 것을 깨닫고 github에서 repository를 new 해서 만들어 주고
아래와 같이 git remote add 실행
-----------------------------git bash----------------------------------------
Lim@DESKTOP /d/HealthInMoaPrj (master)
$ git remote add origin https://github.com/DevLimK1/HealthInMoaPrj.gitLim@DESKTOP /d/HealthInMoaPrj (master)
$ git remote
origin
---------------------------------------------------------------
6. git push origin master 를 입력했는데, 아래와 같은 에러메시지가 발생
-----------------------------git bash----------------------------------
Lim@DESKTOP /d/HealthInMoaPrj (master)
$ git push origin master
To https://github.com/DevLimK1/HealthInMoaPrj.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to https://github.com/DevLimK1/HealthInMoaPrj.git
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
---------------------------------------------------------------
5번에서 new해서 만든 github repository에서
README가 추가 되어있다. github repository와 현재 git이 있는 로컬폴더의 작업환경을
맞춰줘야한다. git pull을 해서 수정된 작업환경을 맞춰주고자 git pull 실행
7. git pull을 해서 원격저장소와 로컬저장소의 작업환경을 맞춰주고자 git pull 명령어 입력
-----------------------------git bash----------------------------------Lim@DESKTOP /d/HealthInMoaPrj (master)
$ git pull origin master From https://github.com/DevLimK1/HealthInMoaPrjbranch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories---------------------------------------------------------------
에러 당시에는 '수정된 원격 기록과 로컬에 있는 버전의 기록이 달라서 pull도 안되니 이전 커밋하기 전 버전으로 돌아가야겠구나' 라고 생각했다.
+)검색으로 알게된건데,
git pull origin 브런치명 --allow-unrelated-histories
위 명령어를 통해 git에서는 서로 관련 기록이 없는 두 프로젝트를 병합할 때 기본적으로 거부하는데,
거부하지 않고 허용해 주는 명령어라고 한다. 나중에 위와 같은 에러가 다시 발생한다면 참고해봐야겠다.
8. 이전 버전으로 돌아가기위해 git revert 실행하려고하는데, 이전 커밋할 때의 버전 주소를 알아야함으로 git log를 통해 버전주소를 확인
-----------------------------git bash----------------------------------
Lim@DESKTOP-UIJGJEH MINGW64 /d/HealthInMoaPrj (master)
$ git log --oneline
325a17b (HEAD -> master) upload HealthInMoaPrj, implement navbar and sidebar menu
---------------------------------------------------------------
9. git revert 버전주소 를 입력했더니, 로컬폴더에 있던 파일들이 삭제됐다...
-----------------------------git bash----------------------------------
Lim@DESKTOP-UIJGJEH MINGW64 /d/HealthInMoaPrj (master)
$ git revert 325a17b
Removing js/index.js
Removing index.html
Removing css/style.css
Removing css/reset.css
Removing css/global.css
[master 929e0a0] Revert "upload HealthInMoaPrj, implement navbar and sidebar menu" 12 files changed, 1152 deletions(-)
delete mode 100644 css/global.css
delete mode 100644 css/reset.css
delete mode 100644 css/style.css
delete mode 100644 index.html
delete mode 100644 js/index.js
---------------------------------------------------------------
복구 과정
1. git log --oneline 으로 log기록 확인
Lim@DESKTOP /d/HealthInMoaPrj (master)
$ git log --oneline
929e0a0 (HEAD -> master) Revert "upload HealthInMoaPrj, implement navbar and sidebar menu"
325a17b upload HealthInMoaPrj, implement navbar and sidebar menu
☞ 색칠한 revert된 버전주소를 기억하길 바란다.
2. 복구하는 방법은 2가지로 나뉜다.
- 커밋하지 않아서 원격저장소(github)에 push 되지 않은 경우
//단일 파일일 경우
git ls-files --deleted ( or git ls-files -d )
//여러개 파일일 경우
git ls-files --deleted | xargs git checkout --
- 커밋 후 원격저장소(github)에 push 된 경우 (필자의 경우)
//처음부터 HEAD까지 해당 하는 파일이 있는지 커밋 위치를 알려준다
git rev-list -n 1 HEAD -- <file_name>
Lim@DESKTOP /d/HealthInMoaPrj (master)
$ git rev-list -n 1 HEAD -- "index.html"
929e0a0b5265a6ab6552d1a109774aca82458245
☞ 삭제되었던 index.html 파일의 버전주소(아까 위에서 기억하라던 버전주소와 동일)가 나왔음을 알 수 있다.
3. 버전주소를 알았으니, 파일 복구를 해보자!!
//복구할 버전의 커밋 위치를 찾았으면, 해당 커밋 바로 이전 위치에 Checkout 해야하므로
//caret(^) 기호를 추가한다. 2가지 방법으로 작성가능하다.
//작성방법1)
Lim@DESKTOP /d/HealthInMoaPrj (master)
$ git checkout 929e0a0b^ -- index.html
//작성방법2)
Lim@DESKTOP /d/HealthInMoaPrj (master)
$ git checkout $(git rev-list -n 1 HEAD -- "index.html")^ -- "index.html"
//file_name 대신 file_path를 넣을경우 해당 로컬폴더에 이전에 삭제되었던 모든 파일이 복구 된다.
Lim@DESKTOP /d/HealthInMoaPrj (master)
$ git checkout $(git rev-list -n 1 HEAD -- "/d/HealthInMoaPrj")^ -- "/d/HealthInMoaPrj"
참고하면 좋을 자료
https://www.youtube.com/user/newlec1
(위 사진은 유튜브 뉴렉처 선생님 강의 자료)
긴 글 끝까지 읽어주셔서 감사합니다 : )
미흡한 글솜씨지만
최대한 쉽게 정보를 전달하려고 했는데, 도움이 되었으면 좋겠네요.
수정이 필요하거나 질문이 있으시면 댓글 남겨주세요~
도움이 되셨다면 공감(♥)버튼, 댓글은 작성자에게 큰 힘이 됩니다.
댓글