push 전 commit을 수정하는 경우
1. 가장 최근의 commit을 수정하는 경우
git commit --amend
위 명령어를 입력하면 다음과 같이 뜬다
"commit한 메시지"
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Sep 4 15:33:01 2023 +0900
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# Changes to be committed:
# modified: ~
# modified: ~
# modified: ~
# modified: ~
# modified: ~
# modified: ~
# modified: ~
# modified: ~
# new file: ~
#
메시지 수정 후 esc, :wq를 순서대로 입력해서 수정하는 창을 닫으면 된다.
2. 오래된 commit을 수정하는 경우
command line에 git log를 입력하면 로그를 볼 수 있다.
commit ~ (HEAD -> main)
Author: ~
Date: Mon Sep 4 15:33:01 2023 +0900
About 디자인 변경
commit ~
Author: ~
Date: Sun Sep 3 19:49:19 2023 +0900
디자인 수정
commit ~
Author: ~
Date: Sat Sep 2 00:17:20 2023 +0900
반응형 수정
commit ~
Author: ~
Date: Fri Sep 1 23:31:20 2023 +0900
About 파트 디자인 변경
:
위에서 두 번째 commit을 수정해야 한다면 다음과 같이 입력하면 가장 최근의 commit 2개를 보여준다.
git rebase -i HEAD~2
# HEAD~n -> head에서 n번째 메시지 수정
pick e05b90d 디자인 수정
pick 5808bb7 About 디자인 변경
# Rebase 418234b..5808bb7 onto 418234b (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# create a merge commit using the original merge commit's
# message (or the oneline, if no original merge commit was
.git/rebase-merge/git-rebase-todo [unix] (15:59 04/09/2023) 1,1 Top
<eact/portfolio/.git/rebase-merge/git-rebase-todo" [unix] 33L, 1547B
수정하고 싶은 commit 옆의 pick을 reword로 바꾼 후, esc, :wq를 순서대로 입력하면, commit을 수정할 수 있는 창이 뜬다.
메시지를 수정한 후, 똑같이 esc, :wq를 입력하여 저장해주면 된다.
이미 push가 끝난 commit을 수정하는 경우
위에서 commit 메시지 수정이 끝났다면, 다음과 같이 입력한다.
git push "리모트 이름" "브랜치" --force
수정한 메시지를 force를 통해 강제로 push하는 방법이다.
force는 최대한 사용하지 않는 게 좋으니 commit message는 꼭! 한번 더 확인한 후 push하자.