Git commit 정리하기 (rebase)

hannni·2022년 9월 26일
0

Git

목록 보기
3/11

📌 rebase란?

지정한 커밋을 브랜치를 바꾸어 새로 작성하거나, 일괄 정리하거나 로그를 깨끗하게 만들어주는 커맨드입니다.
한마디로 지정 커밋을 재작성해 로그를 정리하기 위한 커맨드 입니다.


✏️ 사용법

git rebase -i HEAD~[n]
git rebase -i [COMMIT ID]

rebase를 통해 현재 커밋을 원하는 시점의 커밋에 포함시킬 수 있습니다. HEAD~나 커밋 아이디로 원하는 커밋을 재작성합니다.
명령어를 입력하면 선택한 시점 이후부터 HEAD(현재)까지의 커밋 내용이 표시됩니다. 편집기로 이동해 원하는 작업을 선택할 수 있습니다.

pick 99ea0b0 a
pick d6d5a3c b
pick 565634b c

변경 전 a,b,c 순 커밋 메시지입니다. 여기서 c를 a에 포함시켜 보겠습니다.

pick 99ea0b0 a
fixup 565634b c
pick d6d5a3c b

커밋c를 a의 뒤로 옮기고 pick을 fixup으로 수정해 c를 a에 포함시켰습니다.

# 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.

알파벳(f) 또는 단어 전부(fixup)를 입력해 각각의 기능을 사용할 수 있습니다.
이 중 fixup을 사용해 메시지를 수정해보았습니다.

이 때, 편집하지 않고 자동으로 커밋을 포함시킬 수도 있습니다.

git rebase -i HEAD~[n] --autosquash

--autosquash 옵션을 사용하면 편집기를 열어 직접 작성하지 않고, 자동으로 fixup 수정이 됩니다.

pick 99ea0b0 a
fixup 5989dda fixup! a
pick d6d5a3c b

이 옵션은 이렇게 fixup! [커밋] 이라는 메시지가 자동으로 남겨집니다.

➕직전 커밋 수정과 포함

git commit --amend

현재 커밋을 직전 커밋을 수정하거나 현재 커밋을 직전 커밋에 바로 포함 시킬 수 있습니다.

0개의 댓글