시작하기 앞서 주의사항... (저 자신도 다짐합니다...)
원격 저장소에 push하지 않은 commit은 수정이 쉽습니다.
push한 commit은 rebase 후에 강제로 push해야 하기 때문에 다른 사람과 함께 작업 중이었다면 문제가 생길 수 있습니다.
그러니 항상 push 전에 git log 명령어로 commit을 확인하시는 게 좋겠습니다ㅎㅎ
제일 최근 commit 수정
git commit --amend
명령어 실행 시 아래와 같은 에디터 창이 뜹니다.
커밋 메세지
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Thu Dec 14 15:13:27 2023 +0900
#
# On branch main
# Your branch is ahead of 'origin/main' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# modified: app/src/main/java/com/test/Test.Kt
#
가장 위에 적힌 메세지를 수정하고 저장한 뒤 창을 닫습니다.
vi 터미널 이용 시에는 esc 를 눌러 명령모드로 전환한 뒤 :wq 를 입력해 저장합니다.
명령어만 이용해서 커밋 메세지를 수정할 수도 있습니다.
git commit --amend -m "메세지 내용"
더 이전 commit 수정
git rebase -i HEAD~숫자
숫자는 수정하려는 커밋 순서(뒤에서부터 몇 번째인지)를 입력합니다.
예를 들어 "git rebase -i HEAD~3" 명령어 실행 시 아래와 같은 에디터 창이 뜹니다.
pick 02dc983 커밋 메세지3
pick 56c4589 커밋 메세지2
pick 95e3eab 커밋 메세지1
# Rebase fb62081..95e3eab onto fb62081 (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.
#
이때 수정하려는 커밋 메세지의 pick을 reword로 고칩니다.
pick 02dc983 커밋 메세지3
pick 56c4589 커밋 메세지2
reword 95e3eab 커밋 메세지1
~ 생략 ~
저장하고 창을 닫습니다.
vi 터미널 이용 시 esc 를 누르고 :wq 를 입력합니다.
이후 reword로 고쳐준 커밋이 새로운 에디터 창으로 뜹니다.
amend 옵션을 이용했을 때와 마찬가지로 가장 위의 메세지를 수정하고 저장한 뒤 창을 닫습니다.
vi 터미널 이용 시 메세지 수정 후 esc 를 누르고 :wq 를 입력해 저장합니다.
remote에 push한 commit 수정
위의 방법으로 커밋 메세지를 수정한 뒤 강제로 push합니다.
만약 가장 최근 메세지만 수정했다면 force-with-lease 옵션을 이용합니다.
git push --force-with-lease origin(원격 저장소) main(브랜치)
rebase 를 이용해 이전 메세지를 수정했다면 force 옵션을 이용합니다.
git push --force origin main
하지만 GitHub 공식 문서에서는 이런 force 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.
force push할 경우 레포지토리의 기록을 바꾸기 때문에, 해당 repository를 이미 clone했던 사람은 자신의 로컬 기록을 수동으로 고쳐야 할 수도 있다고 합니다.
참고
'Git' 카테고리의 다른 글
[GitHub] Issue 사용하기 (2) | 2024.01.22 |
---|---|
[Git] Write access to repository not granted (2) | 2024.01.10 |
[Git] 맥에서 GitHub 사용자 인증하기 (2) | 2023.12.22 |
[Git] LF will be replaced by CRLF 경고 해결하기 (1) | 2023.12.05 |