ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [정리] cherry-pick
    IT/Git 2024. 4. 1. 23:39

    출처 - unsplash.com

     

    Git 브랜치 전략을 고민하면서 얘기를 나누던 중, "cherry-pick" 기능에 대해 알게 되어 내용을 정리해본다.

     

    출처 - https://blog.devops.dev/git-cherry-pick-601b9e0d7f7b

     

    위의 그림과 같이 'cherry-pick' 이란,

    특정 브랜치에 커밋된 항목을 다른 브랜치로 커밋이 필요한 경우 사용하는 기능이다.

     

    언제 사용하지?

    1. 버그 수정 시

    - feature branch에서 수정한 버그를 master(main) brach로 바로 반영할 필요가 있는 경우

     

    2. 커밋을 잘못한 경우

    - 수정한 소스를 다른 브랜치로 잘못 커밋한 경우 올바른 브랜치에서 해당 소스를 가져옴

     

    3. 특정 feature만 다른 브랜치로 배포할 경우

    - 하나의 브랜치에 커밋된 feature 중에서 "전체가 아닌" 특정 feature만 다른 브랜치로 반영이 필요한 경우 

     

    출처 - https://blog.devops.dev/git-cherry-pick-601b9e0d7f7b

     

    Git Cherry-pick

    Cherry picking means to choose a commit from one branch and apply it onto another branch.

    blog.devops.dev

     

    사실 우리가 고민한 부분도 위의 3번 케이스에 해당하는 경우였고, 

    내부적으로 개발자가 cherry-picking 하는 방법으로 방향으로 잡았다.

     

    그러면 cherry-pick 실습을 해보자.

     

    1. Git 저장소 생성 (특정 디렉토리에서 아래 명령어 수행)

    $ git init

     

    2. 최초 파일 생성 및 add, commit

    $ echo "hello" > init.txt
    $ git add init.txt
    $ git commit -m "init"

     

    3. 로그 확인

    $ git log
    commit 3e6138a17214e55330d045c747c8dbf7267eecdd (HEAD -> master)

     

    4. 자, 이제 cherry-pick 기능 확인을 위해서 새로운 브랜치 ch 를 만들자.

    - 기존 master 브랜치에서 새로운 ch 브랜치로 변경됨을 확인 가능

    $ git checkout -b ch
    Switched to a new branch 'ch'

     

     

    새로운 ch 브랜치에 01.txt, 02.txt, 03.txt 파일을 생성하고, 이를 master 브랜치에서 02.txt 파일만 cherry-pick 해볼꺼임.

     

    5. ch 브랜치에서 01.txt 파일 생성, add&commit 수행

    $ echo "1" > 01.txt
    $ git add * && git commit -m 01
    [ch e102903] 01
     1 file changed, 1 insertion(+)
     create mode 100644 01.txt

     

    6. 동일하게 02.txt, 03.txt 파일 생성, add&commit 수행

    $ echo "2" > 02.txt
    $ git add * && git commit -m 02
    warning: in the working copy of '02.txt', LF will be replaced by CRLF the next time Git touches it
    [ch 3b523f5] 02
     1 file changed, 1 insertion(+)
     create mode 100644 02.txt
     $
     $
     $ echo "3" > 03.txt
     $ git add * && git commit -m 03
    warning: in the working copy of '03.txt', LF will be replaced by CRLF the next time Git touches it
    [ch a2320d4] 03
     1 file changed, 1 insertion(+)
     create mode 100644 03.txt

     

    7. 커밋 로그 확인

    - 최초 init부터 01~03.txt 파일을 커밋했다면 총 4개의 커밋 로그 확인 가능

    $ git log
    commit a2320d4ed2250467807c1bb7a2b5c418add01a84 (HEAD -> ch)
    Author: 
    Date:   Mon Apr 1 22:42:18 2024 +0900
    
        03
    
    commit 3b523f5e9cd3acb6a184d2ad6475f4e0b9deceae
    Author: 
    Date:   Mon Apr 1 22:41:33 2024 +0900
    
        02
    
    commit e102903c5c11a00ab72c5340d4a529831b85a4e9
    Author: 
    Date:   Mon Apr 1 22:40:47 2024 +0900
    
        01
    
    commit 3e6138a17214e55330d045c747c8dbf7267eecdd (master)
    Author: 
    Date:   Mon Apr 1 22:34:22 2024 +0900
    
        init

     

    자, 이제 중간 커밋인 02.txt 를 master 브랜치로 커밋해보자.

     

    8. ch -> master 로 브랜치 변경

    $ git checkout master
    Switched to branch 'master'

     

    9. cherry-pick 수행

    - 사용법: git cherry-pick <commit 해시코드>

    - commit 해시코드는 최소 4자리 이상 기재하여 식별하는데, 중복 방지를 위해 일반적으로 7자리를 사용함

    - 위의 경우, 3b523f5

    $ git cherry-pick 3b523f5
    [master 4a63885] 02
     Date: Mon Apr 1 22:41:33 2024 +0900
     1 file changed, 1 insertion(+)
     create mode 100644 02.txt

     

    10. 로그 확인

    - commit 해시코드값이 ch 브랜치의 값과 다르다

    - 이는 ch 브랜치에 있는 '내용만 같은' 02 를 master 브랜치에 "새로" 커밋했다는 의미

    $ git log
    commit 4a6388589ac7db10dc975af9af87f355c6d67cdf (HEAD -> master)
    Author: 
    Date:   Mon Apr 1 22:41:33 2024 +0900
    
        02
        
    commit 3e6138a17214e55330d045c747c8dbf7267eecdd
    Author: 
    Date:   Mon Apr 1 22:34:22 2024 +0900
    
        init

     

     

    결국 cherry-pick 이라는 것이 다른 브랜치에 있는 리소스를 현 브랜치에 '강제로' 커밋하겠다는 의미로 보인다.

     

    관련해서 rebase, merge, revert 의 개념을 다시 정리할 필요가 있다.

     

     

    'IT > Git' 카테고리의 다른 글

    [정리] rebase  (0) 2024.04.02
    불필요한 파일 제외하고 push하자! - .gitignore  (0) 2020.11.18
    Git, Github  (0) 2020.11.16

    댓글

Designed by Tistory.