[git] 커밋 메세지 수정하기 (changing commit message)

May·2020년 9월 17일
60

git

목록 보기
1/2

1. 아직 커밋이 local 에 있을 때

아직 로컬에서 commit 을 하고 push 는 하지 않아 remote 에 올라가지 않은 상태일 경우 다음과 같이 하면 됩니다.

1.1. 가장 최근의 commit 수정

git commit --amend

위와 같이 amend 를 이용하면 가장 마지막에 commit 한 내용을 수정할 수 있습니다.

git commit --amend 를 사용하고 커밋을 수정할 수 있는 창이 뜨면, 수정을 완료한 후 esc -> :wq(저장 + 창 닫기) 를 해주면 됩니다.


1.2. 더 오래된 commit 수정 or 한 번에 여러 commit 수정

커맨드 라인에 git log 를 쳐 보세요.

로그에서 여태 자신이 한 커밋을 쭉 확인 후 어떤 커밋을 수정할 것인지 확인합니다.

만일 위에서부터 세 번째 커밋을 수정해야 한다면

git rebase -i HEAD~3

위 커맨드를 사용하면 현재 작업중인 브랜치의 가장 최근 commit 3개를 보여주게 됩니다.

(3이 들어간 자리에 체크하길 원하는 commit 의 갯수를 집어넣으시면 됩니다!)

그럼 다음과 같은 창이 뜹니다.


pick e499d89 Delete CNAME
pick 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.

# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

이젠 수정하고 싶은 커밋 옆의 pick 이라는 문구를 reword 로 바꿔 주면 됩니다.

두 번째 커밋과 세 번째 커밋을 수정해보도록 하겠습니다.

pick e499d89 Delete CNAME
reword 0c39034 Better README
reword f7fde4a Change the commit message but push the same commit.

esc -> :wq 를 통해 커밋 리스트를 저장을 해주고 나면, 두 개의 커밋을 각각 수정할 수 있는 창이 순서대로 띄워집니다.

원하는대로 커밋을 수정하시고, :wq 를 통해 저장해주세요.

수정이 잘 되었는지 git log 를 통해 확인합니다.


2. 이미 커밋을 push 해 remote 에 올린 상황일 때

커밋이 이미 remote 에 적용된 상황이라면, force 를 통해 수정된 커밋을 강제로 push 해주어야 합니다.

github 공식 문서에 따르면 force pushing 을 최대한 사용하지 않아야 한다고 하네요. push 된 커밋의 로그를 갖고 있던 다른 팀원들이 로그를 수동으로 수정해줘야 하기 때문이라고 합니다...

로컬에서 commit 을 할 때도 중요하지만 remote 에 push 하기 전에는 정말 두 번 세 번 다시 확인합시다!!!

We strongly discourage force pushing, since this changes the history of your repository. If you force push, people who have already cloned your repository will have to manually fix their local history. For more information, see "Recovering from upstream rebase" in the Git manual.

방법 자체는 간단합니다. local 에서 commit 메세지를 수정한 후, 아래 커맨드를 실행하면 됩니다.

git push --force 브랜치이름

출처
Github Docs: changing a commit message

profile
쉽다는 설명도 저는 어려워요.

3개의 댓글

comment-user-thumbnail
2022년 12월 2일

쉽다는 설명도 저는 어려웠기에 많이 헤맸습니다... 정말 쉬운 설명을 마침내 찾았네요. 감사합니다.

답글 달기
comment-user-thumbnail
2023년 8월 31일

감사합니다!!

혹시 pick -> reword로 변경 안되시는 분들은 i 입력하고나면 수정될거에요!

답글 달기