Programing

두 개의 비 연속 커밋을 어떻게 스쿼시합니까?

lottogame 2020. 6. 8. 07:50
반응형

두 개의 비 연속 커밋을 어떻게 스쿼시합니까?


git 내의 모든 rebasing 기능에 약간 익숙합니다. 내가 다음과 같은 커밋을했다고 가정 해 봅시다.

A -> B -> C -> D

이후 D에에 추가 된 새로운 코드에 따라 수정 사항이 포함되어 있으며 A이러한 커밋이 함께 속해 있음을 알고 있습니다. 어떻게 스쿼시 않는 AD함께하고 휴가 BC혼자?


git rebase --interactiveB보다 먼저 D를 실행 하고 재정렬 할 수 있으며 D는 A로 스쿼시 할 수 있습니다 .

Git이 편집기를 열면 다음과 같은 파일이 나타납니다.

pick aaaaaaa Commit A
pick bbbbbbb Commit B
pick ccccccc Commit C
pick ddddddd Commit D

# Rebase aaaaaaa..ddddddd onto 1234567 (4 command(s))
#
# 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

이제 다음과 같은 파일을 변경하십시오.

pick aaaaaaa Commit A
squash ddddddd Commit D
pick bbbbbbb Commit B
pick ccccccc Commit C

그리고 git은 이제 A와 D의 변경 사항을 하나의 커밋으로 병합하고 나중에 B와 C를 넣습니다. D의 커밋 메시지를 유지하지 않으려면 "fix"키워드를 사용할 수도 있습니다.


참고 : 결과 를 알지 못하면 다른 리포지토리 로 푸시 된 커밋을 변경 해서는 안됩니다 .

git log --oneline -4

D commit_message_for_D
C commit_message_for_C
B commit_message_for_B
A commit_message_for_A

git rebase --interactive

pick D commit_message_for_D
pick C commit_message_for_C
pick B commit_message_for_B
pick A commit_message_for_A

타입 i(삽입 모드에서 VIM 넣기)

다음과 같이 목록을 변경하십시오 (커밋 메시지를 제거하거나 포함하지 않아도 됨). 철자를 잘못 쓰지 마십시오 squash! :

pick C commit_message_for_C
pick B commit_message_for_B
pick A commit_message_for_A
squash D

입력 Esc한 후 ZZ(VIM 저장 후 종료)

# This is a combination of 2 commits.
# The first commit's message is:

commit_message_for_D

# This is the 2nd commit message:

commit_message_for_A

유형 i

텍스트를 새 커밋 메시지의 모양으로 변경하십시오. 커밋 A변경 사항에 대한 설명입니다 D.

new_commit_message_for_A_and_D

입력 Esc후,ZZ

git log --oneline -4

E new_commit_message_for_A_and_D
C commit_message_for_C
B commit_message_for_B

git show E

(You should see a diff showing a combination of changes from A and D)

You have now created a new commit E. Commits A and D are no longer in your history but are not gone. You can still recover them at this point and for a while by git rebase --hard D (git rebase --hard will destroy any local changes!).


For those using SourceTree:

Make sure you haven't already pushed the commits.

  1. Repository > Interactive Rebase...
  2. Drag D (the newer commit) to be directly above A (the older commit)
  3. Make sure commit D is highlighted
  4. Click Squash with previous

Interactive rebase works well until you have big feature branch with 20-30 commits and/or couple of merges from master or/and fixing conflicts while you was commiting in your branch. Even with finding my commits through history and replacing pick with squash doesn't worked here. So i was looking for another way and found this article. I did my changes to work this on separate branch:

git checkout master
git fetch
git pull
git merge branch-name
git reset origin/master
git branch -D branch-name
git checkout -b branch-name
git add --all
#Do some commit
git push -f --set-upstream origin branch-name

Before this I got my pull request with about ~30 commits with 2-3 merges from master + fixing conflicts. And after this I got clear PR with one commit.

P.S. here is bash script to do this steps in automode.


$ git checkout master

$ git log --oneline

D
C
B
A

$ git rebase --onto HEAD^^^ HEAD^

$ git log --oneline

D
A

참고URL : https://stackoverflow.com/questions/3921708/how-do-i-squash-two-non-consecutive-commits

반응형