git

[git] git squash - 여러 개의 커밋 하나로 묶기 🚀

728x90

하나의 작업을 여러 번에 걸쳐서 만들다 보면 여러 개의 커밋이 발생할 수 있습니다.

 

작업 완료 후 같은 작업을 한 커밋을 하나로 합쳐서 깔끔하게 정리하고 싶다면 Squash를 사용해야 합니다.

 

오늘도 역시 Interactive Rebase를 사용해서 Squash 하는 방법을 포스팅하려고 합니다.

 

 

아래처럼 같은 화면을 여러 번에 걸쳐서 만들 때가 있습니다.

이 커밋들을 하나로 합칠 때 Interactive Rebase를 사용합니다.

* [2021-06-17] [6b9da64] | main (3/3) {{dongmin}}  (HEAD -> master)
* [2021-06-17] [16f9739] | main (2/3) {{dongmin}}
* [2021-06-17] [be62605] | main (1/3) {{dongmin}}
* [2021-06-17] [2be4c42] | Initial commit {{dongmin}}

 

main 1 ~ 3까지 묶어야 하기 때문에 main 1의 직전 커밋을 지정해야 합니다.

(최근 3개의 커밋을 불러온다 라고 생각하시면 좀 더 쉽게 느껴지실 거예요 ^^)

git rebase -i HEAD~3

 

명령어를 입력하면 아래와 같은 화면을 볼 수 있습니다.

pick be62605 main (1/3)
pick 16f9739 main (2/3)
pick 6b9da64 main (3/3)

# Rebase 2be4c42..6b9da64 onto 2be4c42 (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.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#

 

가장 위의 3줄을 아래와 같이 바꾸고 저장합니다.

(가장 위의 커밋을 제외한 나머지 커밋들을 "s" or "squash"로 바꿔줍니다.)

pick be62605 main (1/3)
s 16f9739 main (2/3)
s 6b9da64 main (3/3)

 

저장하고 에디터를 닫으면 아래와 같이 커밋 메세지를 수정할 수 있는 화면이 생깁니다.

# This is a combination of 3 commits.
# This is the 1st commit message:

main (1/3)

# This is the commit message #2:

main (2/3)

# This is the commit message #3:

main (3/3)

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Jun 17 17:41:46 2021 +0900
#
# interactive rebase in progress; onto 2be4c42
# Last commands done (3 commands done):
#    squash 16f9739 main (2/3)
#    squash 6b9da64 main (3/3)
# No commands remaining.
# You are currently rebasing branch 'master' on '2be4c42'.
#
# Changes to be committed:
#	new file:   b
#	new file:   c
#	new file:   main.html
#

마지막으로 자신이 원하는 커밋 메세지를 작성해서 Squash 작업을 마무리하면 됩니다.

* [2021-06-17] [49a0dc1] | Add main (1~3) {{dongmin}}  (HEAD -> master)
* [2021-06-17] [2be4c42] | Initial commit {{dongmin}}

 

※ rebase를 하면 수정 작업을 하는 커밋부터 그 이후의 커밋들이 전부 변경되기 때문에(commit history가 변경됨)

혼자 작업하는 브랜치가 아닌 다른 개발자들과 함께 사용하는 브랜치에서는 되도록이면 사용하지 않는 걸 추천드립니다!

728x90

'git' 카테고리의 다른 글

[git] git에서 commit을 분리하는 방법 🚀  (0) 2021.06.16