Git squash

git-squash

Önceki makalemde versiyon geçmişindeki bir commit’i nasıl düzelteceğimizi anlatmıştım. Bu makalede rebase ile squash nasıl yapılır onu anlatacağım.

Yukardaki resim yapacağımız işlemin özeti gibi. Feature branchında yeni özellik geliştiriyorsunuz, master’a merge edeceksiniz ama versiyon geçmişinizin daha düzenli kalmasını istiyorsunuz diyelim. Feature içerisindeki bütün commitleri tek bir commit hâline getirerek bunu yapabilirsiniz.

Feature branch’nde aşağıdaki gibi bir versiyon geçmişi olsun. Feature branch’ine ben 4. commit’ten checkout ettim yani son üç commiti squash edeceğim.

c409ccf (HEAD -> feature) seventh
7d22efe sixth
ba0a4e4 fifth
cd9b86f fourth
54edd5b third
826eb40 second
c5c75a1 first

Şu komutu çalıştırarak işleme başlayalım.

git rebase -i HEAD~3

Karşınıza şu şekilde bir editör belirecek.

pick ba0a4e4 fifth
pick 7d22efe sixth
pick c409ccf seventh

# Rebase cd9b86f..c409ccf onto cd9b86f (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
...

Squash işlemi için 3 tane commit seçtik. Burda mantık bir tane commit’i bırakıp diğerlerini o commit’in üzerine squash etmektir. Yani ilk satırdaki commit pick olarak kalacak, diğer ikisini squash olarak işaretleyip kaydedeceğiz.

pick ba0a4e4 fifth
squash 7d22efe sixth
squash c409ccf seventh

# Rebase cd9b86f..c409ccf onto cd9b86f (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

Ardından tekrar editör açılacak, squash edilmiş commit mesajını girmeniz istenecek. İstediğiniz commit mesajını yazıp kaydedin.

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

squashed into fifth commit

# This is the commit message #2:

#sixth

# This is the commit message #3:

#seventh

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#

İşlem başarılı. Görüldüğü üzere şuan Feature branch’imde sadece tek bir commit var. Bunu artık master’a merge edip diğer feature’larımı geliştirmeye devam edebilirim.

nurettin@pc:~/PycharmProjects/gitAmend$ git tree
* 75c71fe (HEAD -> feature) squashed into fifth commit
* cd9b86f fourth
* 54edd5b third
* 826eb40 second
* c5c75a1 first
Mor renkli dal feature branch’ine karşılık geliyor
nurettin@pc:~/PycharmProjects/gitAmend$ git rebase -i HEAD~3
[detached HEAD 75c71fe] squashed into fifth commit
 Date: Tue Aug 16 22:05:00 2022 +0300
 1 file changed, 4 insertions(+), 1 deletion(-)
Successfully rebased and updated refs/heads/feature.

Sonraki makalede görüşmek üzere 🙂