Programing

Git 저장소의 처음 두 커밋을 결합 하시겠습니까?

lottogame 2020. 5. 12. 07:56
반응형

Git 저장소의 처음 두 커밋을 결합 하시겠습니까?


세 개의 커밋 A, BC를 포함하는 히스토리가 있다고 가정하십시오 .

A-B-C

두 커밋 AB 를 하나의 커밋 AB 에 결합하고 싶습니다 .

AB-C

나는 시도했다

git rebase -i A

다음 내용으로 편집기가 열립니다.

pick e97a17b B
pick asd314f C

나는 이것을 다음으로 바꾼다.

squash e97a17b B
pick asd314f C

그런 다음 Git 1.6.0.4는 다음과 같이 말합니다.

Cannot 'squash' without a previous commit

방법이 있습니까 아니면 불가능합니까?


git rebase -i --rootGit 버전 1.7.12부터 사용하십시오 .

대화식 리베이스 파일에서 커밋 B 의 두 번째 줄 스쿼시로 변경 하고 다른 줄은 pick으로 둡니다 .

pick f4202da A
squash bea708e B
pick a8c6abc C

이것은 두 커밋 AB 를 하나의 커밋 AB에 결합합니다 .

이 답변 에서 발견되었습니다 .


당신은 시도:

git rebase -i A

다음과 같이 계속 edit하지 않으면 다음과 같이 시작할 수 있습니다 squash.

edit e97a17b B
pick asd314f C

그런 다음 실행

git reset --soft HEAD^
git commit --amend
git rebase --continue

끝난.


A초기 커밋 이었지만 이제는 초기 커밋이되고 싶습니다 B. git commit은 전체 트리이며, 일반적으로 그들이 소개하는 diff로 설명되고 보더라도 diff가 아닙니다.

이 레시피는 A와 B, B와 C 사이에 여러 커밋이있는 경우에도 작동합니다.

# Go back to the last commit that we want
# to form the initial commit (detach HEAD)
git checkout <sha1_for_B>

# reset the branch pointer to the initial commit,
# but leaving the index and working tree intact.
git reset --soft <sha1_for_A>

# amend the initial tree using the tree from 'B'
git commit --amend

# temporarily tag this new initial commit
# (or you could remember the new commit sha1 manually)
git tag tmp

# go back to the original branch (assume master for this example)
git checkout master

# Replay all the commits after B onto the new initial commit
git rebase --onto tmp <sha1_for_B>

# remove the temporary tag
git tag -d tmp

대화식 리베이스의 경우 목록이 다음이되도록 A 전에 수행해야합니다.

pick A
pick B
pick C

될 :

pick A
squash B
pick C

A가 초기 커밋 인 경우 A보다 먼저 초기 커밋이 달라야합니다. Git이 차이점을 생각하면 (A와 B)와 (B와 C)의 차이에 대해 작동합니다. 따라서 스쿼시는 귀하의 모범에서 작동하지 않습니다.


In the case that you have hundreds or thousands of commits, using kostmo's answer of

git rebase -i --root

can be impractical and slow, just due to the large number of commits that the rebase script has to process twice, once to generate the interactive rebase editor list (where you select what action to take for each commit), and once to actually execute the re-application of commits.

Here is an alternative solution that will avoid the time cost of generating the interactive rebase editor list by not using an interactive rebase in the first place. In this way, it's similar to Charles Bailey's solution. You simply create an orphan branch from the second commit, and then rebase all the descendant commits on top of it:

git checkout --orphan orphan <second-commit-sha>
git commit -m "Enter a commit message for the new root commit"
git rebase --onto orphan <second-commit-sha> master

Documentation


In a related question, I managed to come up with a different approach to the need of squashing against the first commit, which is, well, to make it the second one.

If you're interested: git: how to insert a commit as the first, shifting all the others?


Git command for squad: git rebase -i HEAD~[number of commits]

Lets say you have below git commit history:


pick 5152061 feat: Added support for saving image. (A)
pick 39c5a04 Fix: bug fixes. (B)
pick 839c6b3 fix: conflict resolved. (C)

Now you want to squash A and B to AB, perform below steps:


pick 5152061 feat: Added support for saving image. (A)
s 39c5a04 Fix: bug fixes. (B)
pick 839c6b3 fix: conflict resolved. (C)

Note: for squashing commit we can use squash or s. The end result will be:
pick 5152061 feat: Added support for saving image. (AB)
pick 839c6b3 fix: conflict resolved. (C)


You have to perform a bit of command-line magic.

git checkout -b a A
git checkout B <files>
git commit --amend
git checkout master
git rebase a

That should leave you with a branch that has AB and C as commits.

참고URL : https://stackoverflow.com/questions/435646/combine-the-first-two-commits-of-a-git-repository

반응형