본문 바로가기
Git

Git] Git bash에서 최근 커밋 Squash하기

by Fastlane 2024. 4. 30.
728x90
반응형

squash는 Git command가 아니다. 다음 2가지 방법으로 commits을 squash할 수 있다. 

  • rebase 사용하기 : git rebase -i ...
  • -squash option과 함께 merge하기 : git merge -squash

시작하기 전에 Git alias slog를 만들자. 

$ git config --global alias.slog "log --graph --all --topo-order --pretty='format:%h %ai %s%d(%an)'"

$ git slog
* 7d737ab 2024-04-29 15:45:02 +0900 forth commit! (HEAD -> master)(test)
* 16ddbd4 2024-04-29 13:39:04 +0900 third commit!(test)
* 6c97b4e 2024-04-29 13:38:35 +0900 second commit!(test)
* 02020f9 2024-04-29 13:38:03 +0900 first commit!(test)

 

마지막 3 commits을 합쳐보자. 

rebase 사용하기

git rebase -i HEAD~3

실행하면, git은 Vim editor를 실행한다. 합쳐야 하는 3 commits이 pick command와 함께 리스트되어 있다. 

합치기 위해서 3, 4번째 commit의 pick command를 s로 바꾸고 저장한다. 

$ git slog
* 74aaf3c 2024-04-29 13:38:35 +0900 second commit! (HEAD -> master)(test)
* 02020f9 2024-04-29 13:38:03 +0900 first commit!(test)

$ git log -1
commit 74aaf3ca919f8ff3a6c5cd223528c30c50e94a3a (HEAD -> master)
Author: test
Date:   Mon Apr 29 13:38:35 2024 +0900

    second commit!

    third commit!

    forth commit!

 

하나로 합쳐진 것을 확인할 수 있다. git rebase -i HEAD~X는 최근 커밋들을 합치는데 사용하기 간단하다. 

하지만, 많은 수의 commit을 하나로 합치기 어렵고, 에러가 생기기 쉽다. 

 

X를 세기 어려운 경우, git rebase -i hash_onto 명령어를 사용할 수 있다. 

$ git slog
* 7d737ab 2024-04-29 15:45:02 +0900 forth commit! (HEAD -> master)(test)
* 16ddbd4 2024-04-29 13:39:04 +0900 third commit!(test)
* 6c97b4e 2024-04-29 13:38:35 +0900 second commit!(test)
* 02020f9 2024-04-29 13:38:03 +0900 first commit!(test)

$ git rebase -i 02020f9

-squash option과 함께 merge하기

rebase는 branch에서 commit-graph를 깔끔하기 하는데 효과적이다. 

feature branch에서 많은 commits을 만든 후, master라 불리는 main branch에 feature branch를 merge하려고 한다. 이때, master branch graph를 깔끔하게 유지하기 위해서, one feature에 one commit으로 관리하고 싶다. 

$ git slog
* 58827cd 2024-04-30 08:35:32 +0900 feature 3 commit! (HEAD -> feature)(test)
* 5146cec 2024-04-30 08:35:14 +0900 feature 2 commit!(test)
* d101cac 2024-04-30 08:35:00 +0900 feature 1 commit!(test)
* 7d737ab 2024-04-29 15:45:02 +0900 forth commit! (master)(test)
* 16ddbd4 2024-04-29 13:39:04 +0900 third commit!(test)
* 6c97b4e 2024-04-29 13:38:35 +0900 second commit!(test)
* 02020f9 2024-04-29 13:38:03 +0900 first commit!(test)

feature branch를 만들고 commit을 3개 추가하였다. 이 커밋을 하나로 합쳐서 master에 merge하자. 

$ git checkout master
Switched to branch 'master'

$ git merge --squash feature
Updating 7d737ab..58827cd
Fast-forward
Squash commit -- not updating HEAD
 sample.txt.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

일반 merge와 다르게, --squash option을 사용했기 때문에, git은 자동으로 merge commit을 만들지 않는다. 

대신, feature branch의 모든 변경사항을 local 변경사항으로 가져온다. 

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   sample.txt.txt

$ git commit -m 'squash merge'
[master b0bfd1b] squash merge
 1 file changed, 1 insertion(+), 1 deletion(-)

merge를 완료하기 위해, 변경사항을 commit한다. 

 

branch graph를 확인해보자. 

$ git slog
* b0bfd1b 2024-04-30 08:38:03 +0900 squash merge (HEAD -> master)(test)
| * 58827cd 2024-04-30 08:35:32 +0900 feature 3 commit! (feature)(test)
| * 5146cec 2024-04-30 08:35:14 +0900 feature 2 commit!(test)
| * d101cac 2024-04-30 08:35:00 +0900 feature 1 commit!(test)
|/
* 7d737ab 2024-04-29 15:45:02 +0900 forth commit!(test)
* 16ddbd4 2024-04-29 13:39:04 +0900 third commit!(test)
* 6c97b4e 2024-04-29 13:38:35 +0900 second commit!(test)
* 02020f9 2024-04-29 13:38:03 +0900 first commit!(test)

feature branch의 모든 변경사항을 master branch로 머지하였으며, master branch에 b0bfd1b 하나의 commit이 생겼다. 

반면에, feature branch에는 여전히 3 commits을 갖고있다. 

 

결론

feature -> develop 머지 시, Squash merge가 유용하다. feature branch에서 기능을 개발하기 위한 지저분한 커밋 내역을 하나의 커밋으로 묶어 develop에 병합하면서, develop에는 기능 단위로 커밋이 추가되도록 정리할 수 있다. 

또한, feature branch는 develop branch에 병합 후, 제거하므로 merge commit을 남길 필요가 없다. 

 

728x90
반응형

댓글