오픈소스 기여를 마치면 하나의 커밋으로 스쿼시 해달라고 한다. 지저분한 커밋 기록을 push 에서 정리하기 위함이다.
지금까지 여러개의 커밋으로 올라간 기록을 하나의 커밋으로 합치는 작업이다.
먼저 git log
명령어로 로그를 확인한다.
git log
의 옵션은 다음 링크에서 확인하자.
git log --pretty="[%h] %s - %an %cd"
[16fe114927] 1차 수정 제안 적용 - hs.lucas.lee Mon Sep 18 17:24:34 2023 +0900
[36f379d4ee] [ko] change-runtime-containerd.md into korean - hs.lucas.lee Wed Jul 26 17:17:01 2023 +0900
[8e3b962db9] add an original source - hs.lucas.lee Fri Jul 14 16:43:59 2023 +0900
[33403f784c] Merge pull request #41767 from byeongjaeSon/outdated-16-21 - Kubernetes Prow Robot Tue Jun 27 17:38:30 2023 -0700
[1b195d62ae] [ko] Update outdated files in dev-1.27-ko.1 [M16-21] - 손병재 Wed Jun 28 09:27:02 2023 +0900
위 커밋 중에서 상단 3개의 커밋을 하나의 커밋으로 합치려고 한다. (hs.lucas.lee의 작업 내용)
다음과 같이 rebase 명령어를 사용한다.
git rebase -i HEAD~3
'HEAD~3'은 가장 마지막 커밋에서 세번째 커밋까지를 말한다.
위와 같이 명령어를 입력하면 미리 지정된 default 에디터가 실행된다.
pick 8e3b962db9 add an original source
pick 36f379d4ee [ko] change-runtime-containerd.md into korean
pick 16fe114927 1차 수정 제안 적용
# Rebase 33403f784c..16fe114927 onto 33403f784c (3 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
# specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
# to this position in the new commits. The <ref> is
# updated at the end of the rebase
#
# 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.
#
여기서 가장 상단 커밋(첫번째 커밋)을 기준으로 아래는 모두 s(quash) 로 변경한다.
가장 상단의 커밋은 r(eword)를 사용한다. 이는 해당 커밋을 사용하되 커밋 메시지를 편집하는 옵션이다.
다른 옵션이 필요한 경우도 있을 수 있으니, 꼭 도움 설명을 읽어보자.
필자는 아래와 같이 수정하였다.
r 8e3b962db9 add an original source
s 36f379d4ee [ko] change-runtime-containerd.md into korean
s 16fe114927 1차 수정 제안 적용
저장하고 에디터를 종료하면 다시 아래와 같이 커밋 메시지를 어떻게 편집할 것인지 새로운 에디터를 띄워준다.
add an original source
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Fri Jul 14 16:43:59 2023 +0900
#
# interactive rebase in progress; onto 33403f784c
# Last command done (1 command done):
# reword 8e3b962db9 add an original source
# Next commands to do (2 remaining commands):
# squash 36f379d4ee [ko] change-runtime-containerd.md into korean
# squash 16fe114927 1차 수정 제안 적용
# You are currently editing a commit while rebasing branch 'dev-1.27-ko.1-change-runtime-containerd' on '33403f784c'.
#
# Changes to be committed:
# new file: content/ko/docs/tasks/administer-cluster/migrating-from-dockershim/change-runtime-containerd.md
#
필자의 경우 "[ko] change-runtime-containerd.md into korean"로 변경하고 저장한 뒤 종료하였다.
마지막으로 여러 커밋을 스쿼시 하는 경우이므로 커밋 메시지를 다시 하나로 작성한다.
git 에서 명령어 수행 로그는 아래와 같이 남았다.
$ git rebase -i HEAD~3
[detached HEAD 96343d0288] [ko] change-runtime-containerd.md into korean
Date: Fri Jul 14 16:43:59 2023 +0900
1 file changed, 170 insertions(+)
create mode 100644 content/ko/docs/tasks/administer-cluster/migrating-from-dockershim/change-runtime-containerd.md
[detached HEAD 57757b23bd] [ko] change-runtime-containerd.md into korean
Date: Fri Jul 14 16:43:59 2023 +0900
1 file changed, 170 insertions(+)
create mode 100644 content/ko/docs/tasks/administer-cluster/migrating-from-dockershim/change-runtime-containerd.md
Successfully rebased and updated refs/heads/dev-1.27-ko.1-change-runtime-containerd.
마지막으로 해당 커밋이 원격지와 다르므로 pull 을 받거나 fetch 를 하면 conflict 를 해결해야 하는 상황에 놓일 수 있다.
또한, 일반 push 를 시도하면 문제가 있다고 안된다.
원격지에 강제로 push 해서 깨끗한 커밋 기록을 만들자.
(!위험! 잘 알고 사용하자)
git push -f
# 혹은 명시적으로
git push origin {해당 브랜치 명}
그리고 로그를 다시 확인해보자
$ git log --pretty="[%h] %s - %an %cd"
[57757b23bd] [ko] change-runtime-containerd.md into korean - hs.lucas.lee Wed Sep 20 13:59:54 2023 +0900
[33403f784c] Merge pull request #41767 from byeongjaeSon/outdated-16-21 - Kubernetes Prow Robot Tue Jun 27 17:38:30 2023 -0700
[1b195d62ae] [ko] Update outdated files in dev-1.27-ko.1 [M16-21] - 손병재 Wed Jun 28 09:27:02 2023 +0900
하나로 합쳐진 커밋을 확인할 수 있다.