본문 바로가기
Programming/Git

[git] git push 되돌리기 - reset

by guru_k 2021. 11. 25.
728x90
반응형

git commit을 잘못하여 되돌려야 할때가 있다. 이럴 경우 git reset 명령어를 사용해서 commit을 되돌릴 수 있다.

commit 되돌리기

먼저 git log 를 통해 commit 내용을 살펴보자.

$ git log
commit fe954047297922fdc380b974ebe83a401504ed6c (HEAD -> main, origin/main)
Author: test <test@gmail.com>
Date:   Thu Nov 25 19:17:39 2021 +0900

    third commit

commit 388ac6c6d77cad44065b51f1f5e2cd461660ebc8
Author: test <test@gmail.com>
Date:   Thu Nov 25 19:16:24 2021 +0900

    second commit

commit 070310e37ee130a36b068bd8656dd800c739be00
Author: test <test@gmail.com>
Date:   Thu Nov 25 18:25:28 2021 +0900

    first commit

총 세개의 commit이 존재하며 git reset HEAD^ 를 통해서 가장 최근 commit을 되돌린다. git reset 후 commit 이 사라졌으며 Local 환경에 unstaged 상태로 변경된 파일이 존재하는 것을 확인할 수 있다.

$ git reset HEAD^
Unstaged changes after reset:
M	README.md

$ git log
commit 388ac6c6d77cad44065b51f1f5e2cd461660ebc8 (HEAD -> main)
Author: test <test@gmail.com>
Date:   Thu Nov 25 19:16:24 2021 +0900

    second commit

commit 070310e37ee130a36b068bd8656dd800c739be00
Author: test <test@gmail.com>
Date:   Thu Nov 25 18:25:28 2021 +0900

    first commit

$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

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:   README.md

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

이후 필요한 내용을 변경한 이후 다시 commit / push를 진행하자.

$ git status
On branch main
Your branch is behind 'origin/main' by 3 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

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:   README.md

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

$ git add .

# 새로운 commit 메시지를 작성
$ git commit -m 'new third commit'
[main 0ea3cd6] new third commit
 1 file changed, 1 insertion(+)

# 해당 commit을 강제로 push
$ git push -f

이후에 commit log를 확인해보면 새로 생성한 commit인 new third commit이 덮어씌워진것을 확인할 수 있다.

$ git log
commit 0ea3cd6e0d2d338ac560cff6d64e304e12d06e2f (HEAD -> main, origin/main)
Author: test <test@gmail.com>
Date:   Thu Nov 25 19:25:15 2021 +0900

    new third commit

commit 388ac6c6d77cad44065b51f1f5e2cd461660ebc8
Author: test <test@gmail.com>
Date:   Thu Nov 25 19:16:24 2021 +0900

    second commit

commit 070310e37ee130a36b068bd8656dd800c739be00
Author: test <test@gmail.com>
Date:   Thu Nov 25 18:25:28 2021 +0900

    first commit

* 주의 할 점은 되돌아간 이후 Commit 정보가 모두 사라지기 때문에 주의해야 한다.

 

 

728x90
반응형

댓글