Skip to main content

git squash - 여러개의 커밋로그를 하나로 묶기

About 1 minGitArticle(s)blogmeetup.nhncloud.comgitgit-squash

git squash - 여러개의 커밋로그를 하나로 묶기 관련

Git > Article(s)

Article(s)

git squash - 여러개의 커밋로그를 하나로 묶기| NHN Cloud Meetup
git squash - 여러개의 커밋로그를 하나로 묶기

정확히 이야기해서 git squash 라는 명령어는 없습니다. interactive rebase를 하는데 필요한 명령어 중 하나지요.

때때로 아래처럼 하나의 작업을 여러번에 걸쳐서 커밋을 할 때가 있습니다.

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

여기서 Task 1/3~3/3 을 하나의 commit으로 묶어버리고 싶은 경우가 많죠.

이럴때 사용하는것이 interactive rebase 입니다.

git rebase -i HEAD~3

(최근 3개의 커밋을 interactive rebase 한다는 뜻입니다)

환경에 따라 다르겠지만 보통은 vi에디터가 뜨면서 다음과 같은 메세지를 볼 수 있습니다.

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

위의 세줄을 다음과 같이 바꾸어 준 후 저장(:wq)를 누르면

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

다른 vi창이 뜨면서 커밋 메세지를 rewrite 할 수 있습니다.

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 log --pretty=oneline
#
# 9833ca676c5a24361c1cc36fb173746328dfac3a Task 1/3 ~ 3/3
# 2260a88777c247c31170ff6074d95569ac557afb Initial commit

참고

가능하면 이미 push하지 않은 작업만 squash 하는것을 추천합니다. push와 pull 작업이 살짝 번거로워 집니다

git push -f / git fetch &&; git merge --squash


이찬희 (MarkiiimarK)
Never Stop Learning.