Programming/Git

[Git] Squash Commit / Merge : 여러 개의 Commit 합치기

728x90

 

 

이미 Commit 했는 데 추가적인 수정사항이 있거나 요건이 추가되어

여러 개의 Commit으로 이력이 너무 지저분해진 경우,

Squash하여 하나의 Commit으로 이력을 깔끔하게 관리할 수 있습니다 😋

 

 

 

 

0. Squash

 

여러 개의 Commit을 하나의 Commit 이력으로 만드는데 사용하는 방법

 

가능하다면 원격 저장소에 push하지 않은 작업만 squash하기를 추천합니다 (push / pull 과정이 번거로워요)

 

여러 Commit을 Squash 할 수 있는 두 가지 방법을 소개하겠습니다

    1. Rebase를 통한 Squash

    2. Squash 옵션을 사용한 Merge

 

 

 

 

1. Rebase를 통한 Squash

 

다음처럼 3개의 commit을 확인해두었습니다 ( First ~ Third Commit : 합칠거임 😉 )

choco@LAPTOP-K5UE8036 MINGW32 /d/7.Git Repositories/TEST-PROJECT (master)
$ git log
commit dd66ce8437c4e38be502f46a0e5e0dfffb0976b5 (HEAD -> master)
Author: syunWin <tus302@naver.com>
Date:   Sun Sep 25 18:08:15 2022 +0900

    Third Commit

commit 669adb073747ed71b90429b22629662a5750a788
Author: syunWin <tus302@naver.com>
Date:   Sun Sep 25 18:07:41 2022 +0900

    Second Commit

commit 0822d211b54e712094367a68d430ec05fa829fb4
Author: syunWin <tus302@naver.com>
Date:   Sun Sep 25 18:07:06 2022 +0900

    First Commit

 

 

 

1.1 git rebase

다음 rebase 명령어를 통해 3개의 Commit을 Squash합니다

git rebase -i HEAD~3

-i 옵션은 —interactive의 약어로 rebase 명령어를 대화형으로 실행한다는 의미입니다

 

 

 

1.2 Squash에 대한 Commit 세부정보 수정

rebase 실행 후, 다음과 같이 기본 편집기가 호출됩니다

pick 명령으로 여러 commit을 보여줍니다

pick 0822d21 First Commit
pick 669adb0 Second Commit
pick dd66ce8 Third Commit

# Rebase e66e72f..dd66ce8 onto e66e72f (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 [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# 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.
#

 

다음과 같이 첫번째 commit만을 pick으로 유지하고

나머지 commit들의 명령을  squash  또는  s 로 변경한 후, 저장하고 종료합니다

pick 0822d21 First Commit
squash 669adb0 Second Commit
squash dd66ce8 Third Commit

# Rebase e66e72f..dd66ce8 onto e66e72f (3 commands)

 

 

 

2.3 Commit Message 수정

commit 세부정보를 수정/저장하고 난 후, 자동으로 커밋 메세지를 수정하기 위한 편집기가 실행됩니다

편집기가 실행된 후 초기 모습은 다음과 같습니다

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

First Commit

# This is the commit message #2:

Second Commit

# This is the commit message #3:

Third Commit

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Sep 25 18:07:06 2022 +0900
#
# interactive rebase in progress; onto e66e72f
# Last commands done (3 commands done):
#    squash 669adb0 Second Commit
#    squash dd66ce8 Third Commit
# No commands remaining.
# You are currently rebasing branch 'master' on 'e66e72f'.
#
# Changes to be committed:
#       new file:   temp.txt
#

 

다음과 같이 첫번째 라인에 Commit Message로 사용될 문장을 작성하고 저장합니다

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

First Commit

# This is the commit message #2:

Second Commit

# This is the commit message #3:

Third Commit

 

log를 확인하면 First~Third Commit이 하나의 Commit으로 합쳐진 것을 확인할 수 있습니다

choco@LAPTOP-K5UE8036 MINGW32 /d/7.Git Repositories/TEST-PROJECT (master)
$ git log
commit e2508c236699991e695124fdf08a9f75d762043c (HEAD -> master)
Author: syunWin <tus302@naver.com>
Date:   Sun Sep 25 18:07:06 2022 +0900

    Squash of 3 commits

    First Commit

    Second Commit

    Third Commit

commit e66e72f8abfbbb1cabb77594a1374b184c75082d (origin/master, origin/HEAD)
Merge: 395a316 8a06d1a
Author: Sihyun Lee <tus302@naver.com>
Date:   Sat Sep 24 16:12:28 2022 +0900

    Merge pull request #5 from syun32/4-blog-update-test

    'blog-update-test' into master

 

 

 

2.4 push

이제 변경사항을 원격 저장소에 push하여 마무리합니다

git push

 

choco@LAPTOP-K5UE8036 MINGW32 /d/7.Git Repositories/TEST-PROJECT (master)
$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 323 bytes | 323.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To <https://github.com/syun32/TEST-PROJECT.git>
   e66e72f..e2508c2  master -> master

 

 

 

 


2. Squash 옵션을 사용한 Merge

1에서의 3개의 commit을 이번에는 git merge -squash를 사용하여 squash 하겠습니다

 

2.1 git checkout

Merge할 target branch로 checkout 합니다

$ git checkout prd

 

 

 

2.2 merge —squash

다음 명령어와 같이 —squash 옵션과 함께 merge합니다

git merge --squash master

 

3개의 Commit에 대한 변경사항이 합쳐져서 staged 되어있습니다

$ git merge --squash master
Automatic merge went well; stopped before committing as requested
Squash commit -- not updating HEAD

choco@LAPTOP-K5UE8036 MINGW32 /d/7.Git Repositories/TEST-PROJECT (prd)
$ git status
On branch prd
Your branch is up to date with 'origin/prd'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   temp.txt

 

 

 

2.3 commit

commit message와 함께 squash한 내용을 commit합니다

git commit -m "커밋 메세지"

 

choco@LAPTOP-K5UE8036 MINGW32 /d/7.Git Repositories/TEST-PROJECT (prd)
$ git commit -m "Squash of 3 commits"
[prd 3b2dff2] Squash of 3 commits
 1 file changed, 3 insertions(+)
 create mode 100644 temp.txt

 

 

 

2.4 git push

원격 저장소에 commit한 내용을 push 합니다

git push

 

choco@LAPTOP-K5UE8036 MINGW32 /d/7.Git Repositories/TEST-PROJECT (prd)
$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 293.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To <https://github.com/syun32/TEST-PROJECT.git>
   450cb75..3b2dff2  prd -> prd

 

 

 

 

 

 


References

📌 https://www.delftstack.com/ko/howto/git/git-squash-commits/

📌 https://im-developer.tistory.com/182

 

 

 

 

728x90