source

What does it mean to squash commits in git?

factcode 2023. 7. 18. 22:01
반응형

What does it mean to squash commits in git?

What does Squashing commits in git mean. How do I squash commits in Github?

You can think of Git as an advanced database of snapshots of your working directory(ies).

Git의 매우 좋은 특징 중 하나는 커밋 기록을 다시 작성할 수 있다는 것입니다.
이 작업을 수행하는 주된 이유는 이러한 기록의 대부분이 해당 기록을 생성한 개발자에게만 관련되므로 공유 저장소에 제출하기 전에 단순화하거나 더 좋게 만들어야 합니다.

커밋 스퀴즈는 관용적인 관점에서 해당 커밋에 소개된 변경 사항을 상위 항목으로 이동하여 두 개(또는 그 이상)가 아닌 하나의 커밋으로 마무리하는 것을 의미합니다.
이 프로세스를 여러 번 반복하면 커밋을 하나로 줄일 수 있습니다.

Visually, if you started your work at the commit tagged Start, you want this

Git commits squashing

새 커밋의 파란색이 약간 더 짙은 것을 알 수 있습니다.의도적인 행동입니다.

Git에서 스퀴싱은 Interactive Rebase라고 하는 특수한 형태의 Rebase를 사용하여 수행됩니다.
일련의 커밋을 분기 B로 재배치할 때 단순화하면 해당 커밋에 의해 도입된 모든 변경 사항을 원래 상위 항목이 아닌 B에서 시작하여 그대로 적용할 수 있습니다.

시각적 단서

enter image description here

파란색의 여러 가지 색조를 다시 한 번 주목하십시오.

대화형 기본 재배치를 사용하여 커밋의 기본 재배치 방법을 선택할 수 있습니다.이 명령을 실행하는 경우:

 git rebase -i branch

기반이 변경될 커밋이 나열된 파일이 표시됩니다.

 pick ae3...
 pick ef6...
 pick 1e0...
 pick 341...

커밋의 이름을 지정하지는 않았지만 이 네 가지는 시작부터 시작까지 커밋으로 사용됩니다.

이 목록의 좋은 점은 편집할 수 있다는 것입니다.
커밋을 생략하거나 커밋을 제거할 수 있습니다.
첫 번째 단어를 스쿼시로 바꾸기만 하면 됩니다.

 pick ae3...
 squash ef6...
 squash 1e0...
 squash 341...

편집기를 닫았는데 병합 충돌이 없으면 다음 기록이 나타납니다.

enter image description here

이 경우 다른 분기로 기본을 변경하지 않고 이전 커밋으로 되돌리려고 합니다.
첫 번째 예에서 보여진 것처럼 역사를 변환하기 위해서는 다음과 같은 것을 실행해야 합니다.

git rebase -i HEAD~4

change the "commands" to squash for all the commits apart from the first one, and then close your editor.


Note about altering history

Git에서 커밋은 편집되지 않습니다.제거하거나, 연결할 수 없게 하거나, 복제할 수 있지만 변경할 수는 없습니다.
기본값을 변경하면 실제로 새 커밋이 만들어집니다.
오래된 것들은 어떤 심판도 더 이상 도달할 수 없기 때문에, 역사에는 표시되지 않지만 여전히 존재합니다!

This is what you actually get for a rebase:

enter image description here

If you have already pushed them somewhere, rewriting the history will actually make a branch!

rebase 명령에는 몇 가지 놀라운 옵션이 있습니다.--interactive(또는)-i) 모드이며, 가장 널리 사용되는 것 중 하나는 커밋을 스쿼시하는 기능입니다.이렇게 하면 작은 커밋을 사용하여 더 큰 커밋으로 결합할 수 있습니다. 이는 하루의 작업을 마무리할 때나 변경 사항을 다르게 패키징하려는 경우에 유용할 수 있습니다.우리는 당신이 이것을 쉽게 할 수 있는 방법을 검토할 것입니다.

A word of caution: Only do this on commits that haven’t been pushed an external repository. If others have based work off of the commits that you’re going to delete, plenty of conflicts can occur. Just don’t rewrite your history if it’s been shared with others.

So let’s say you’ve just made a few small commits, and you want to make one larger commit out of them. Our repository’s history currently looks like this:

enter image description here

The last 4 commits would be much happier if they were wrapped up together, so let’s do just that through interactive rebasing:

$ git rebase -i HEAD~4

pick 01d1124 Adding license
pick 6340aaa Moving license into its own file
pick ebfd367 Jekyll has become self-aware.
pick 30e0ccb Changed the tagline in the binary, too.

# Rebase 60709da..30e0ccb onto 60709da
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

So, a few things have happened here. First of all, I told Git that I wanted to rebase using the last four commits from where the HEAD is with HEAD~4. Git has now put me into an editor with the above text in it, and a little explanation of what can be done. You have plenty of options available to you from this screen, but right now we’re just going to squash everything into one commit. So, changing the first four lines of the file to this will do the trick:

pick 01d1124 Adding license
squash 6340aaa Moving license into its own file
squash ebfd367 Jekyll has become self-aware.
squash 30e0ccb Changed the tagline in the binary, too.

Basically this tells Git to combine all four commits into the the first commit in the list. Once this is done and saved, another editor pops up with the following:

# This is a combination of 4 commits.
# The first commit's message is:
Adding license

# This is the 2nd commit message:

Moving license into its own file

# This is the 3rd commit message:

Jekyll has become self-aware.

# This is the 4th commit message:

Changed the tagline in the binary, too.

    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # Explicit paths specified without -i nor -o; assuming --only paths...
    # Not currently on any branch.
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #   new file:   LICENSE
    #   modified:   README.textile
    #   modified:   Rakefile
    #   modified:   bin/jekyll
    #

Since we’re combining so many commits, Git allows you to modify the new commit’s message based on the rest of the commits involved in the process. Edit the message as you see fit, then save and quit. Once that’s done, your commits have been successfully squashed!

Created commit 0fc4eea: Creating license file, and making jekyll self-aware.
 4 files changed, 27 insertions(+), 30 deletions(-)
  create mode 100644 LICENSE
    Successfully rebased and updated refs/heads/master.

And if we look at the history again… enter image description here

그래서, 이것은 지금까지 비교적 고통이 없었습니다.기본 재배치 중에 충돌이 발생하면 일반적으로 해결하기가 매우 쉬우며 Git은 가능한 한 많은 문제를 해결하도록 안내합니다.이 문제의 기본은 문제의 갈등을 해결하는 것입니다.git add파일, 그리고 나서.git rebase --continue프로세스를 다시 시작합니다.물론, 하는 것은.git rebase --abort당신이 원한다면 당신을 이전 상태로 되돌릴 것입니다.어떤 이유로 인해 기본 재배치에서 커밋이 손실된 경우 reflog를 사용하여 다시 가져올 수 있습니다.

Details can be found this link.

It means combining several commits into one. Have a look at :

Squash my last X commits together using Git

ReferenceURL : https://stackoverflow.com/questions/35703556/what-does-it-mean-to-squash-commits-in-git

반응형