본문 바로가기
Project/Git, GitHub

이미 push한 commit message 수정하기

by minhi 2025. 4. 7.

commit message를 feat: Add LifestyleQuestionScreen으로 작성했어야 했는데

 

feat: Add LifestyleCompleteScreen으로 작성하고 실수로 push까지 해버렸다.

 

이렇게 이미 push한 commit message를 수정하는 방법을 알아보자.

 

최근 n개의 commit 불러오기

 

최근 n개의 commit은 아래의 명령어로 불러올 수 있다.

git rebase -i HEAD~n

 

내가 수정하고자 하는 commit은 마지막에서 3번째이므로 n 자리에 3을 넣었다.

1
2
3
PS C:\Users\minha\Desktop\DementiaForecast\src\assets\images> git rebase -i HEAD~3
error: cannot rebase: You have unstaged changes.
error: Please commit or stash them.
cs

 

그런데 unstaged changes를 commit하거나 stash*하라고 한다.

 

나는 commit message만 수정하면 되므로 stash를 하기로 했다.

 

이때 stash란 commit되지 않은 변경 사항들을 임시저장하는 것으로, working directory를 깨끗이 비운다.

 

commit되지 않은 변경 사항을 임시저장하고, working directory로 되돌리기

 

commit되지 않은 변경 사항은 아래의 명령어로 임시저장할 수 있다.

git stash push -m "message"

 

이때 message에는 어떤 변경사항을 저장한 것인지에 대한 설명을 작성하면 된다.

 

이렇게 stash한 변경 사항은 추후에 working directory로 되돌려 작업을 이어나가야 할 것이다.

 

stash한 변경 사항은 아래의 명령어로 working directory로 되돌릴 수 있다.

git stash pop

 

이제 stash 후 commit message를 수정하고 다시 working directory로 되돌려놓자.

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
PS C:\Users\minha\Desktop\DementiaForecast\src\assets\images> git stash push -"temp changes"
Saved working directory and index state On feature/front-end: temp changes       
PS C:\Users\minha\Desktop\DementiaForecast\src\assets\images> git rebase -i HEAD~3
[detached HEAD 702a347] feat: Add LifestyleQuestionScreen
 Date: Mon Apr 7 18:02:37 2025 +0900
 1 file changed, 211 insertions(+)
 create mode 100644 src/screens/lifestyle/LifestyleQuestionScreen.tsx
Successfully rebased and updated refs/heads/feature/front-end.
PS C:\Users\minha\Desktop\DementiaForecast\src\assets\images> git stash pop
On branch feature/front-end
Your branch and 'origin/feature/front-end' have diverged,
and have 3 and 3 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
 
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:   ../../screens/auth/LoginScreen.tsx
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        ../fonts/
        ../../redux/
        ../../screens/auth/EmailLoginScreen.tsx
        ../../screens/auth/EmailSignUpScreen.tsx
        ../../screens/biometric/
 
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (3f9b32ee157f4a09596ae27cf20951788974f4df)
cs

 

위에는 나와있지 않지만 git rebase -i HEAD~3을 실행시키면 최근 3개의 commit이 표시되는데, 

 

3개의 commit 중 수정하고자 하는 commit에 한해 pick을 reword로 수정하고 ESC, :wq를 입력한다.

 

그러면 해당 commit의 commit message를 수정할 수 있는 창이 열리고, 수정하면 된다!

 

이때 vim을 사용하게 되므로 자주 쓰이는 명령어는 알아두도록 하자.

동작 명령어
입력 모드 전환 i
입력 모드 종료 ESC
저장 후 종료 :wq
저장하지 않고 종료 :q!
저장만 하기 :w
종료만 하기 :q