Git 커밋 수정하기 치트 시트
1. 마지막 커밋 수정하기
1.1 커밋 메시지만 수정하기
- 가장 최근 커밋의 메시지만 변경할 때 사용합니다.
git commit --amend -m "새로운 커밋 메시지"
- 에디터를 열어서 메시지를 수정하려면
-m
옵션 없이 사용합니다.
git commit --amend
에디터 관련 팁
에디터가 vim으로 열린다면 i
를 눌러 입력 모드로 전환하고, 수정 후 Esc
를 누른 다음 :wq
를 입력하여 저장하고 종료합니다.
1.2 파일 추가하면서 커밋 수정하기
- 빠뜨린 파일이나 추가 변경사항을 마지막 커밋에 포함시킬 때 사용합니다.
# 빠뜨린 파일 스테이징
git add forgotten-file.txt
# 마지막 커밋에 포함시키기 (메시지 수정)
git commit --amend
# 또는 메시지 변경 없이
git commit --amend --no-edit
주의사항
git commit --amend
는 기존 커밋을 완전히 새로운 커밋으로 교체합니다. 이미 원격 저장소에 푸시된 커밋을 수정한 경우 강제 푸시(git push --force
)가 필요할 수 있습니다.
2. 이전 커밋들 수정하기(rebase)
- 이전 커밋을 수정하려면
rebase -i
명령어를 사용합니다.- 최신 커밋을 수정할 때는 git commit --amend를 사용할 수 있습니다.
- rebase를 사용하면 커밋 히스토리가 변경되므로, 이미 푸시된 커밋을 수정할 때는 팀원들과 협의가 필요합니다
2.1 백업 브랜치 생성
- 중요한 작업 전에는 항상 백업을 만들어두는 것이 좋습니다.
# 수정 전 백업
git branch backup-branch
# 수정 작업 수행
git rebase -i HEAD~3
# 문제 발생시 복구
git reset --hard backup-branch
2.2 최근 n개의 커밋 수정하기
- 여러 커밋을 한 번에 수정하거나 정리할 때 사용합니다.
# 최근 3개 커밋을 인터랙티브하게 수정
git rebase -i HEAD~3
# 이후 에디터가 열리면 수정할 커밋을 선택합니다.
pick 7ee1762 4
pick 9f1a8db 5
pick 464b89c 6
# Rebase f9f3985..464b89c onto f9f3985 (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 <commit> = like "squash", but discard this commit's log message
# 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.
#
# These lines can be re-ordered; they are executed from top to bottom.
- 위 명령어를 실행하면 에디터가 열리고, 최근 3개의 커밋 목록이 표시됩니다.
- 각 커밋 앞에 있는 명령어를 변경하여 원하는 작업을 수행할 수 있습니다.
# 이후 에디터가 열리면 수정할 커밋을 선택합니다.
pick 7ee1762 4
pick 9f1a8db 5
r 464b89c 6
# Rebase f9f3985..464b89c onto f9f3985 (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 <commit> = like "squash", but discard this commit's log message
# 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.
#
# These lines can be re-ordered; they are executed from top to bottom.
- 464b89c 커밋의 메시지를 수정하려면
reword
명령어를 사용합니다. 줄여서r
로 표시할 수 있습니다. - 에디터를 저장하고 종료하면, Git이 해당 커밋을 수정하기 위해 멈춥니다.
666
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Fri Jun 13 00:00:31 2025 +0900
#
# interactive rebase in progress; onto f9f3985
# Last commands done (3 commands done):
# pick 9f1a8db 5
# reword 464b89c 6
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on 'f9f3985'.
#
# Changes to be committed:
# modified: REAM.md
#
# Untracked files:
# asdfsadf
#
"~/Desktop/project/study/git-demo/.git/COMMIT_EDITMSG" 20L, 603B
- 이제 에디터가 열리면 커밋 메시지를 수정할 수 있습니다.
2.2.1 에디터에서 나타나는 옵션들
에디터에서 각 커밋 앞의 명령어를 변경하여 다양한 작업을 수행할 수 있습니다.
# 예시 화면:
# pick a1b2c3d 첫 번째 커밋
# pick e4f5g6h 두 번째 커밋
# pick i7j8k9l 세 번째 커밋
사용 가능한 명령어:
pick (p)
: 커밋을 그대로 유지reword (r)
: 커밋 메시지만 변경edit (e)
: 커밋을 수정하기 위해 멈춤squash (s)
: 이전 커밋과 합치기 (메시지 결합)fixup (f)
: 이전 커밋과 합치되 메시지는 버리기drop (d)
: 커밋 삭제
3. reset 명령어 사용하기
3.1 reset 옵션별 차이점
- Git reset은 세 가지 주요 옵션이 있으며, 각각 다른 영역에 영향을 미칩니다.
- reset 명령으로 삭제된 커밋을 복구하고 싶다면
git reflog
를 사용해야 합니다.
3.1.1 git reset --soft
- HEAD만 이동시키고 스테이징 영역과 작업 디렉토리는 그대로 유지합니다.
- 커밋만 취소하고 변경사항은 스테이징된 상태로 보존됩니다.
git reset --soft HEAD~1