your programing

여러 커밋을 선택하는 방법

lovepro 2020. 9. 29. 08:20
반응형

여러 커밋을 선택하는 방법


두 개의 지점이 있습니다. 커밋은 a다른있는 동안, 하나의 머리 b, c, d, ef위에 a. 나는 이동하려면 c, d, ef커밋하지 않고 첫 번째 분기로 b. 벚꽃이 그것을 선택 사용하기 쉬운 : 체크 아웃 첫 번째 지점을 하나씩 체리 - 선택 c에를 f먼저에 두 번째 지점을 리베이스. 그러나 모든 체리 - 선택할 수있는 방법이있다 c- f하나 개의 명령은?

다음은 시나리오에 대한 시각적 설명입니다 ( JJD에게 감사 드립니다 ).

여기에 이미지 설명 입력


Git 1.7.2는 다양한 커밋을 선택하는 기능을 도입했습니다. 로부터 릴리스 노트 :

git cherry-pick "은 커밋 범위 (예 :"cherry-pick A..B "및"cherry-pick --stdin ")를 선택하는 방법을 배웠으므로"git revert "도 마찬가지입니다.이 기능은 더 좋은 순서 제어를 지원하지 않습니다." rebase [-i] "가 있습니다.


중요한 댓글 포함 (각 저자에 대한 크레딧)

참고 1 : "cherry-pick A..B"양식에서 A는 B보다 이전이어야합니다. 순서가 잘못되면 명령이 자동으로 실패합니다. – 데미안

참고 2 : 또한 A를 선택하지 않고 A 이후부터 B까지 모든 것을 선택합니다. – JB Rainsberger

참고 3 : A를 포함하려면 git cherry-pick A ^ .. B – sschaef를 입력하십시오.


이를 수행하는 가장 간단한 방법은 onto옵션을 사용하는 것 rebase입니다. 가정에서 분기되는 현재 완료가 amybranch로라고하며이 이동할하는 지점입니다 c- f에.

# checkout mybranch
git checkout mybranch

# reset it to f (currently includes a)
git reset --hard f

# rebase every commit after b and transplant it onto a
git rebase --onto a b

또는 요청한 한 줄짜리 :

git rebase --onto a b f

git rebase의 직렬 조합을 사용하여 git branch커밋 그룹을 다른 분기에 적용 할 수 있습니다 . wolfc이미 게시 한 것처럼 첫 번째 명령은 실제로 커밋을 복사합니다. 그러나 그룹의 최상위 커밋에 분기 이름을 추가 할 때까지 변경 사항이 표시되지 않습니다.

새 탭에서 사진을여세요 ...

워크 플로우

명령을 텍스트 형식으로 요약하려면 :

  1. 다음 명령을 사용하여 gitk 를 독립적 인 프로세스로 엽니 다 gitk --all &..
  2. 을 실행 git rebase --onto a b f합니다.
  3. Press F5 in gitk. Nothing changes. But no HEAD is marked.
  4. Run git branch selection
  5. Press F5 in gitk. The new branch with its commits appears.

This should clarify things:

  • Commit a is the new root destination of the group.
  • Commit b is the commit before the first commit of the group (exclusive).
  • Commit f is the last commit of the group (inclusive).

Afterwards, you could use git checkout feature && git reset --hard b to delete the commits c till f from the feature branch.

In addition to this answer, I wrote a blog post which describes the commands in another scenario which should help to generally use it.


To apply J. B. Rainsberger and sschaef's comments to specifically answer the question... To use a cherry-pick range on this example:

git checkout a
git cherry-pick b..f

or

git checkout a
git cherry-pick c^..f

If you have selective revisions to merge, say A, C, F, J from A,B,C,D,E,F,G,H,I,J commits, simply use below command:

git cherry-pick A C F J


git rev-list --reverse b..f | xargs -n 1 git cherry-pick

To cherry pick from a commit id up to the tip of the branch, you can use:

git cherry-pick commit_id^..branch_name


git format-patch --full-index --binary --stdout range... | git am -3

Actually, the simplest way to do it could be to:

  1. record the merge-base between the two branches: MERGE_BASE=$(git merge-base branch-a branch-b)
  2. fast-forward or rebase the older branch onto the newer branch
  3. rebase the resulting branch onto itself, starting at the merge base from step 1, and manually remove commits that are not desired:

    git rebase ${SAVED_MERGE_BASE} -i
    

    Alternatively, if there are only a few new commits, skip step 1, and simply use

    git rebase HEAD^^^^^^^ -i
    

    in the first step, using enough ^ to move past the merge-base.

You will see something like this in the interactive rebase:

pick 3139276 commit a
pick c1b421d commit b
pick 7204ee5 commit c
pick 6ae9419 commit d
pick 0152077 commit e
pick 2656623 commit f

그런 다음 b 행 (및 원하는 다른 행)을 제거하십시오.


다음은 체리 선택에 대한 소스 및 대상 분기와 커밋 수를 스크립트에 알려 주기만하면 연속으로 여러 커밋을 체리로 선택할 수있는 스크립트입니다.

https://gist.github.com/nickboldt/99ac1dc4eb4c9ff003a1effef2eb2d81

브랜치에서 마스터로 체리 피킹하려면 (현재 브랜치를 소스로 사용) :

./gcpl.sh -m

6.19.x 브랜치에서 마스터로 최신 커밋 5 개를 선택하려면 :

./gcpl.sh -c 5 -s 6.19.x -t master

참고 URL : https://stackoverflow.com/questions/1670970/how-to-cherry-pick-multiple-commits

반응형