Git

[Git] Squash 여러개의 커밋 합치기

쓰링 2024. 7. 24. 14:14
반응형

Git Squash

git squash는 여러 개의 커밋을 하나의 커밋으로 합치는 방법입니다.

여러 개의 중간 커밋들을 깔끔하게 정리하여 하나의 의미 있는 커밋으로 만들 수 있습니다.

단순히 squash 명령만으로는 작업을 수행할 수 없습니다. 보통 git rebase의 interactive 모드를 사용하여 squash 작업을 진행합니다.

Git Squash: 커밋 기록 깔끔하게 관리하기

 

1. Tool 이용(GitKranken)

원하는 Commit History를 선택하고 우클릭 후 Squash 2 commits를 선택합니다.

 

 

2. Command 명령어

$ git log --pretty=oneline
d442427eae836f15e94f5df0445c70081df79a3e Task 3/3
26395437be53e4e6e68f83aa98560ef93838aaa0 Task 2/3
7c6535580a038e9dcfaa72a98e04848812da9aee Task 1/3
2260a88777c247c31170ff6074d95569ac557afb Initial commit
$

위와 같이 Commit이 있을 때 아래와 같이 interactive rebase를 실행합니다.

$ git rebase -i HEAD~3

 

HEAD~3의 뒤 숫자는 합치고자 하는개수 입니다. (최근 3개의 커밋을 interactive rebase) 

 

pick 7c65355 Task 1/3
pick 2639543 Task 2/3
pick d442427 Task 3/3

# Rebase 2260a88..d442427 onto 2260a88
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# 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.
#
# Note that empty commits are commented out

상단에 있는 Command를 수정합니다.

pick 7c65355 Task 1/3
squash 2639543 Task 2/3
squash d442427 Task 3/3

 

squash는 다른 commit에 합쳐진다는 의미이며 pick 은 해당 commit을 사용한다는 의미입니다.

수정 후 :wq로 저장합니다.

pick 7c65355 Task 1/3
Rebasing (3/3)
# This is a combination of 3 commits.
# The first commit's message is:
Task 1/3

# This is the 2nd commit message:

Task 2/3

# This is the 3rd commit message:

Task 3/3

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 2260a88
# You are currently editing a commit while rebasing branch 'master' on '2260a88'.
#
# Changes to be committed:
#       modified:   README.md
#
~

 

 

Git Commit을 합치는 방법은 command보다는 툴을 사용하는 것이 쉬운 것 같습니다.

 

 

반응형