본문 바로가기
Git

Git] Command Line으로 실수를 되돌리는 17가지 방법![2] - reflog, cherry-pick

by Fastlane 2022. 6. 21.
728x90
반응형

목차

  • 삭제된 커밋 복구하기
  • 삭제된 브랜치 복구하기
  • 새로운 Branch로 commit 이동 (원격저장소로 push된 경우 사용하지 않는다!)
  • 다른 Branch로 commit 이동 (원격저장소로 push된 경우 사용하지 않는다!)

9. 삭제된 커밋 복구하기 : 

$ git reflog

$ git branch <branchname> <commit hash>

$ git log
commit 1956ced7e1717708ccd4e52a5c00d6afbd99b1be (HEAD -> main)
Date:   Tue Jun 21 10:41:08 2022 +0900

    Revert "주석 수정"

    This reverts commit 96060d5d80fe5f9e7d5df2df1ca83a5d7488d4e0.

commit f7a09f9f7bcf288e9a1b4db62ca79a96428d7681
Date:   Tue Jun 21 10:26:00 2022 +0900

    test commit!

commit 96060d5d80fe5f9e7d5df2df1ca83a5d7488d4e0
Date:   Tue Jun 21 10:15:33 2022 +0900

    주석 수정

commit 5c862ffef246deaa76d5e096ed41d8b97165a7c7 (origin/main)
Merge: 53a881c 2c3fc96
Date:   Tue Jun 21 09:53:49 2022 +0900

    Merge branch 'main' of https://github.com/BigExecution/SimpleAPI

과거 특정 commit 상태로 돌아간다. 

$ git reset --hard 5c862ffef246deaa76d5e096ed41d8b97165a7c7
HEAD is now at 5c862ff Merge branch 'main' of https://github.com/BigExecution/SimpleAPI

$ git log
commit 5c862ffef246deaa76d5e096ed41d8b97165a7c7 (HEAD -> main, origin/main)
Merge: 53a881c 2c3fc96
Date:   Tue Jun 21 09:53:49 2022 +0900

    Merge branch 'main' of https://github.com/BigExecution/SimpleAPI

reflog 명령어로 작업내역을 확인한다. 

$ git reflog
5c862ff (HEAD -> main, origin/main) HEAD@{0}: reset: moving to 5c862ffef246deaa76d5e096ed41d8b97165a7c7
1956ced HEAD@{1}: revert: Revert "주석 수정"
f7a09f9 HEAD@{2}: commit: test commit!

reset 이전 상태(1956ced)로 돌아간다. 

hello라는 branch를 생성하고 commit hash를 입력한다. 

$ git branch hello 1956ced

$ git checkout hello
Switched to branch 'hello'

$ git log
commit 1956ced7e1717708ccd4e52a5c00d6afbd99b1be (HEAD -> hello)
Date:   Tue Jun 21 10:41:08 2022 +0900

    Revert "주석 수정"

    This reverts commit 96060d5d80fe5f9e7d5df2df1ca83a5d7488d4e0.

commit f7a09f9f7bcf288e9a1b4db62ca79a96428d7681
Date:   Tue Jun 21 10:26:00 2022 +0900

    test commit!

commit 96060d5d80fe5f9e7d5df2df1ca83a5d7488d4e0
Date:   Tue Jun 21 10:15:33 2022 +0900

    주석 수정

commit 5c862ffef246deaa76d5e096ed41d8b97165a7c7 (origin/main, main)
Merge: 53a881c 2c3fc96
Date:   Tue Jun 21 09:53:49 2022 +0900

    Merge branch 'main' of https://github.com/BigExecution/SimpleAPI

10. 삭제된 브랜치 복구하기 :

$ git reflog

$ git branch <branchname> <commit hash>

main branch로 변경한 다음, hello branch를 삭제한다. 

$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

$ git branch -d hello
error: The branch 'hello' is not fully merged.
If you are sure you want to delete it, run 'git branch -D hello'.

checkout 이전으로 돌아갈 hash(1956ced)를 복사한다. 

$ git reflog
5c862ff (HEAD -> main, origin/main) HEAD@{0}: checkout: moving from hello to main
1956ced HEAD@{1}: checkout: moving from main to hello

hello branch에 hash를 지정하여 branch를 복구한다. 

$ git branch hello 1956ced

$ git checkout hello
Switched to branch 'hello'

11. 새로운 Branch로 commit 이동

$git branch login

$git reset HEAD~1 --hard

main branch의 마지막 commit을 login branch로 옮겨보자. 

$ git log
commit f047ac9cec05c248a40dd5da71e306cf501da997 (HEAD -> main)
Date:   Tue Jun 21 15:14:34 2022 +0900

    start new 'login' featuer

commit 5c862ffef246deaa76d5e096ed41d8b97165a7c7 (origin/main)
Merge: 53a881c 2c3fc96
Date:   Tue Jun 21 09:53:49 2022 +0900

    Merge branch 'main' of https://github.com/BigExecution/SimpleAPI

login branch를 생성한다. 

$ git branch login

원하는 commit으로 되돌아간다. $ git reset HEAD~1 --hard 로 해도 동일하게 변경된다. 

$ git reset --hard 5c862ffef246deaa76d5e096ed41d8b97165a7c7
HEAD is now at 5c862ff Merge branch 'main' of https://github.com/BigExecution/SimpleAPI

12. 다른 Branch로 commit 이동

$ git checkout <branch name>

$ git cherry-pick <SHA>

$ git checkout master

$ git reset --hard HEAD~1

m

main의 commit log를 확인해보자. 

$ git log
commit c6c625e3d7071b50a1af3ffcbc040260899694c1 (HEAD -> main)
Date:   Tue Jun 21 16:04:27 2022 +0900

    login new page

commit 5c862ffef246deaa76d5e096ed41d8b97165a7c7 (origin/main)
Merge: 53a881c 2c3fc96
Date:   Tue Jun 21 09:53:49 2022 +0900

    Merge branch 'main' of https://github.com/BigExecution/SimpleAPI

마지막 commit을 login branch로 옮겨보자. 

admin@DESKTOP-LFHA7HJ MINGW64 ~/source/repos/SimpleAPISln/src/SimpleAPI (main)
$ git checkout login
Switched to branch 'login'

$ git cherry-pick c6c625e3d7071b50a1af3ffcbc040260899694c1
[login 9e8ee93] login new page
 Date: Tue Jun 21 16:04:27 2022 +0900
 1 file changed, 2 insertions(+)

$ git log
commit 9e8ee930a1a9c5cfc9c24baf0bb470f0f198a3ed (HEAD -> login)
Date:   Tue Jun 21 16:04:27 2022 +0900

    login new page

commit 8724eca83010fc2160aa7a4c8c5b92b5bd09a494
Date:   Tue Jun 21 16:09:21 2022 +0900

    add new page to login branch

commit 5c862ffef246deaa76d5e096ed41d8b97165a7c7 (origin/main)
Merge: 53a881c 2c3fc96
Date:   Tue Jun 21 09:53:49 2022 +0900

    Merge branch 'main' of https://github.com/BigExecution/SimpleAPI

main branch의 마지막 commit을 삭제하자. 

$ git checkout main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

$ git reset --hard HEAD~1
HEAD is now at 5c862ff Merge branch 'main' of https://github.com/BigExecution/SimpleAPI

 

728x90
반응형

댓글