본문 바로가기

Devops/Git

[Git] Git commit 내용 삭제하기

로컬 저장소에서 최신 Git commit 내용을 reset 하고 강제로 origin에 푸쉬하여 commit 내역을 삭제 할 수 있다.

skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a)
$ git log
commit 05eb1123c8a7e733fad86b9e82cec17d54863896 (HEAD -> git-test-a, origin/git-test-a)
Author: kindlove <skkkms@naver.com>
Date:   Thu Apr 24 09:41:28 2025 +0900

    Commit git-test-a #13

commit 05e407a0b593504b8d4d011037ac85a5ef0b624e
Author: kindlove <skkkms@naver.com>
Date:   Thu Apr 24 09:34:04 2025 +0900

    Commit git-test-a #12

commit 4c7463689fd4abce7caa1c8c757e7cd914f444e0
Author: kindlove <skkkms@naver.com>
Date:   Thu Apr 24 09:27:48 2025 +0900

    Commit git-test-a #11

 

 

git reset HEAD^ 로 로컬 브랜치에서 마지막 커밋 #13을 삭제한다. --hard 옵션을 추가하면 #13 롤백시 변경 파일을 Unstage하지 않는다.

skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a)
$ git reset HEAD^ --hard
HEAD is now at 05e407a Commit git-test-a #12

skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a)
$ git log
commit 05e407a0b593504b8d4d011037ac85a5ef0b624e (HEAD -> git-test-a)
Author: kindlove <skkkms@naver.com>
Date:   Thu Apr 24 09:34:04 2025 +0900

    Commit git-test-a #12

commit 4c7463689fd4abce7caa1c8c757e7cd914f444e0
Author: kindlove <skkkms@naver.com>
Date:   Thu Apr 24 09:27:48 2025 +0900

    Commit git-test-a #11

commit f90d053bc5a5035ed90dbf4ecf6b8c5e5ffbe565
Author: kindlove <skkkms@naver.com>
Date:   Thu Apr 24 09:27:13 2025 +0900

    Commit git-test-a #10

 

 

remote 브랜치에도 적용하기 위해 push한다.

$ git push origin git-test-a (그냥 push 하면 hea치위치가 안맞아서 안됨)
To github.com:skkkms/git-test.git
 ! [rejected]        git-test-a -> git-test-a (non-fast-forward)
error: failed to push some refs to 'github.com:skkkms/git-test.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a)
$ git push -f origin git-test-a (-f 강제 옵션을 붙여서 말그대로  강제로 remote의 커밋을 제거)
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To github.com:skkkms/git-test.git
 + 05eb112...05e407a git-test-a -> git-test-a (forced update)

 

최종 remote 브랜치의 log를 보면 #13 commit이 제거되었음을 볼 수 있다.

$ git log origin git-test-a
commit 05e407a0b593504b8d4d011037ac85a5ef0b624e (HEAD -> git-test-a, origin/git-test-a)
Author: kindlove <skkkms@naver.com>
Date:   Thu Apr 24 09:34:04 2025 +0900

    Commit git-test-a #12

commit 4c7463689fd4abce7caa1c8c757e7cd914f444e0
Author: kindlove <skkkms@naver.com>
Date:   Thu Apr 24 09:27:48 2025 +0900

    Commit git-test-a #11

commit f90d053bc5a5035ed90dbf4ecf6b8c5e5ffbe565
Author: kindlove <skkkms@naver.com>
Date:   Thu Apr 24 09:27:13 2025 +0900

    Commit git-test-a #10

 

-- The End --