MEDIUMTEXT

git에서 다른 repository에 있는 commit을 cherry-pick 해오는 방법

tweak 2024. 9. 28. 00:12
반응형

개발을 하다보면 기본이 되는 repository에서 fork를 떠서 개발을 하게 되는 경우가 있는데, 이럴 경우 서로 다른  repository에서 commit을 가져와야 하는 경우가 생길 수 있다. 서로 다른 repository에 있는 commit을 cherry-pick 해오는 방법을 알아본다.

 

먼저 현재 test_a라고 하는 repository를 clone하여 main브랜치에 있다고 가정하자.

그리고 test_a_2에 cherry-pick 대상이 되는 commit이 존재 하고 있다고 가정하고, 그 commit의 commit id는 12345abc라고 가정한다.

 

먼저 현재 remote는 test_a로 설정되어 있을 것이다.

$ git remote -v
origin	https://github.com/xxx/test_a.git (fetch)
origin	https://github.com/xxx/test_a.git (push)

 

여기에 cherry-pick 하고 싶은 commit이 존재하는 test_a_2를 origin_2라는 이름의 remote로 추가한다.

$ git remote add origin_2 https://github.com/xxx/test_a_2.git

 

이제 remote가 2개가 연결된 상태가 되었다.

$ git remote -v
origin	https://github.com/xxx/test_a.git (fetch)
origin	https://github.com/xxx/test_a.git (push)
origin_2	https://github.com/xxx/test_a_2.git (fetch)
origin_2	https://github.com/xxx/test_a_2.git (push)

 

 

새로 추가한 origin_2 remote를 fetch 한다.

$ git fetch origin_2
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 881 bytes | 293.00 KiB/s, done.
From https://github.com/xxx/test_a_2
 * [new branch]      main       -> origin_2/main

 

이제 origin_2 remote에 있는 12345abc commit을 cherry-pick한다.

$ git cherry-pick 12345abc
[main 2304aa6] Update hello
 Author: tweak <xxx@gmail.com>
 Date: Fri Feb 24 23:33:26 2024 +0900
 1 file changed, 1 insertion(+), 1 deletion(-)

cherry-pick이 되면서 새로운 commit id가 따졌다.

 

이제 불필요한 origin_2 remote를 삭제 한다.

$ git remote rm origin_2
$ git remote -v
origin	https://github.com/xxx/test_a.git (fetch)
origin	https://github.com/xxx/test_a.git (push)
반응형