Git cherry pick illo

Git Cherry Pick

git cherry-pick은 임의의 Git 커밋을 참조로 선택하고 현재 작업 중인 HEAD에 추가할 수 있는 강력한 명령입니다. Cherry pick은 브랜치에서 커밋을 선택하여 다른 브랜치에 적용하는 행위입니다. git cherry pick은 변경 사항을 실행 취소에 유용할 수 있습니다. 예를 들어, 실수로 잘못된 브랜치에 커밋을 수행했다면 올바른 브랜치로 전환하고 해당 브랜치가 속해야 하는 커밋을 cherry-pick할 수 있습니다.

git cherry pick을 사용하는 경우

git cherry pick은 유용한 도구지만 항상 모범 사례는 아닙니다. cherry pick은 중복 커밋을 유발할 수 있으며, cherry pick이 작동하는 많은 시나리오에서 대신에 기존 병합을 선호합니다. 그러나 git cherry pick은 몇 가지 시나리오에서 편리한 도구입니다.

팀 공동 작업

동일한 코드 또는 그와 관련하여 작업하는 개별 팀원이 있는 경우가 팀에 종종 있습니다. 새로운 제품 기능에 백엔드 및 프런트엔드 컴포넌트가 있을 수 있습니다. 두 제품 섹터 간에 일부 공유 코드가 있을 수 있습니다. 백엔드 개발자는 프런트엔드도 활용해야 하는 데이터 구조를 만들 수도 있습니다. 프런트엔드 개발자는 git cherry-pick을 사용하여 이 가상의 데이터 구조를 만든 커밋을 선택할 수 있습니다. 이 선택을 통해 프런트엔드 개발자는 프로젝트 측면에서 작업을 계속 진행할 수 있습니다.

버그 핫픽스

When a bug is discovered it is important to deliver a fix to end users as quickly as possible. For an example scenario,say a developer has started work on a new feature. During that new feature development they identify a pre-existing bug. The developer creates an explicit commit patching this bug. This new patch commit can be cherry-picked directly to the main branch to fix the bug before it effects more users.

변경 사항 실행 취소 및 손실된 커밋 복원

Sometimes a feature branch may go stale and not get merged into main. Sometimes a pull request might get closed without merging. Git never loses those commits and through commands like git log and git reflog they can be found and cherry picked back to life.

git cherry pick 사용 방법

git cherry-pick 사용 방법을 보여주기 위해 다음과 같은 브랜치 상태의 리포지토리가 있다고 가정하겠습니다.

    a - b - c - d   Main
         \
           e - f - g Feature

git cherry pick 사용은 간단하며 다음과 같이 실행할 수 있습니다.

git cherry-pick commitSha

In this example commitSha is a commit reference. You can find a commit reference by using git log. In this example we have constructed lets say we wanted to use commit `f` in main. First we ensure that we are working on the main branch.

git checkout main

이어서 다음 명령으로 cherry-pick을 실행합니다.

git cherry-pick f

실행하면 Git 기록이 다음과 같이 표시됩니다.

    a - b - c - d - f   Main
         \
           e - f - g Feature

The f commit has been successfully picked into the main branch

git cherry pick의 예제

git cherry pick에 일부 실행 옵션을 전달할 수도 있습니다.

-edit

-edit 옵션을 전달하면 Git에서 cherry pick 작업을 적용하기 전에 커밋 메시지를 표시합니다.

--no-commit

--no-commit 옵션은 cherry pick을 실행하지만 새 커밋을 만드는 대신 대상 커밋의 콘텐츠를 현재 브랜치의 작업 디렉터리로 이동합니다.

--signoff

--signoff 옵션은 cherry-pick 커밋 메시지 끝에 '냐' 서명줄을 추가합니다.

이 유용한 옵션 외에도 git cherry-pick에서는 다양한 병합 전략 옵션을 사용할 수 있습니다. git merge 전략 설명서에서 옵션에 대해 자세히 알아보세요.

또한 git cherry-pick은 병합 충돌을 해결하기 위한 옵션 입력도 수락하며, 여기에는 --abort --continue--quit 옵션이 포함됩니다. 이 옵션은 git mergegit rebase 명령과 관련하여 더 자세히 다룹니다.

요약

Cherry pick은 몇 가지 시나리오에서 매우 유용하게 쓰이는 강력하고 편리한 명령입니다. git merge 또는 git rebase를 대신하여 Cherry pick을 오용해서는 안 됩니다. cherry pick에 대한 커밋을 찾으려면 git log 명령이 필요합니다.