기타 메모

이미 push한 commit 메시지를 수정하기

Jonchann 2023. 10. 17. 15:35

요즘 commit 메시지를 영어로 쓰려고 하는데 push 해놓고 보니까 문법이 틀렸다. 이럴 때 어떻게 메시지를 어떻게 수정하는지 찾아봤으니 메모를 남겨놓는다.

먼저 몇 번째 commit 메시지를 수정해야 하는지 확인한다.

# 이미 작업 중인 브런치라고 가정하고
$ git log
commit {commit_id1} (HEAD -> feature/some-branch, origin/feature/some-branch)
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 15:02:20 2023 +0900

    correct message 1

commit {commit_id2}
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 15:00:48 2023 +0900

    correct message 2

commit {commit_id3}
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 14:38:19 2023 +0900

    correct message 3

commit {commit_id4}
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 12:18:38 2023 +0900

    wrong message 1

commit {commit_id5}
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 12:18:08 2023 +0900

    wrong message 2

위와 같은 로그가 출력되었을 때, 위에서부터 4번째와 5번째 commit 메시지를 고쳐야 하니 HEAD~5까지를 rebase하면 된다.


rebase란, 1) 특정 commit을 새로 만들거나 로그를 삭제할 때 2) 서로 다른 브런치의 commit을 연결할 때 사용하는 기능이다.
2번은 merge와 뭐가 다르냐 할 수 있겠지만 다른 브런치의 로그로써 잇느냐 작업하는 브런치의 로그로써 잇느냐 하는 점이 다르다.
자세한 내용: https://www.atlassian.com/git/tutorials/merging-vs-rebasing

 

Merging vs. Rebasing | Atlassian Git Tutorial

Compare git rebase with the related git merge command and identify all of the potential opportunities to incorporate rebasing into the typical Git workflow

www.atlassian.com

 

rebase의 관리 페이지를 붙여놓겠다. 로그가 깨끗해지지만 익숙하지 않은 사람이 했다간 commit 이력이 꼬일 수 있으니 조심해야한다.(그래서 난 잘 안 쓴다.)

       Assume the following history exists and the current branch is "topic":

                     A---B---C topic
                    /
               D---E---F---G master


       From this point, the result of either of the following commands:

           git rebase master
           git rebase master topic

       would be:

                             A'--B'--C' topic
                            /
               D---E---F---G master

git rebase를 -i(--interactive)옵션(rebase를 할 수 있도록 commit 리스트를 출력하는 옵션)으로 실행하면 아래와 같이 commit 메시지 리스트가 표시된다.

   -i, --interactive
       Make a list of the commits which are about to be rebased. Let the user edit that list before rebasing. This mode can also be used to split commits (see SPLITTING COMMITS below).

       The commit list format can be changed by setting the configuration option rebase.instructionFormat. A customized instruction format will automatically have the long commit hash prepended to
       the format.

       See also INCOMPATIBLE OPTIONS below.

지금 하고 싶은 것은 commit 메시지를 고치는 것이기 때문에 아래에 출력된 대로 pickreword로 고친다.

$ git rebase -i HEAD~5
pick {commit_id5} wrong message 2
pick {commit_id4} wrong message 1
pick {commit_id3} correct message 3
pick {commit_id2} correct message 2
pick {commit_id1} correct message 1

# Rebase {commit_id6}..{commit_id1} onto {commit_id6} (5 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

예를 들면 아래와 같이.

reword {commit_id5} wrong message 2
reword {commit_id4} wrong message 1
pick {commit_id3} correct message 3
pick {commit_id2} correct message 2
pick {commit_id1} correct message 1

그러면 reword대상만 commit 파일을 다시 표시해주니 insert모드로 고치고 저장하면 된다.

다 끝났으면 아래 커맨드로 다시 push 해서 commit을 고친다.

$ git push --force origin feature/some-branch
$ git log
commit {commit_id1} (HEAD -> feature/some-branch, origin/feature/some-branch)
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 15:02:20 2023 +0900

    correct message 1

commit {commit_id2}
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 15:00:48 2023 +0900

    correct message 2

commit {commit_id3}
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 14:38:19 2023 +0900

    correct message 3

commit {commit_id4}
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 12:18:38 2023 +0900

    correct message 4

commit {commit_id5}
Author: github_id <someone@gmail.com>
Date:   Tue Oct 17 12:18:08 2023 +0900

    correct message 5