들어가기 전에
git cherry-pick
명령어는 보통 같은 저장소의 커밋을 가져올 때 사용하지만, 다른 저장소의 커밋도 가져올 수 있습니다.
프로젝트 초기 세팅시 다른 프로젝트에서 세팅한 내용을 그대로 가져와야 하거나 특정 커밋을 가져와야 하는 경우가 있습니다. 리모트를 추가해서 체리픽 하는 방법을 설명합니다.
repository 설명
- app-a : 가져올 다른 레포지토리
- 레포지토리 주소 : https://github.com/2circumflex/app-a.git
- app-b : 현재 레포지토리
- 레포지토리 주소 : https://github.com/2circumflex/app-b.git
1. remote 추가
app-b의 remote 목록입니다.
git remote -v
origin git@github.com:2circumflex/app-b.git (fetch)
origin git@github.com:2circumflex/app-b.git (push)
여기에 app-a를 remote로 추가합니다.
git remote add <remote-name> <repository-url>
(remote-name은 임의로 지정할 수 있습니다.
git remote add app-a git@github.com:2circumflex/app-a.git
2circumflex은 제 깃허브 아이디입니다. 본인 아이디로 변경해주세요.
다시 app-b의 remote 목록을 확인해봅니다.
app-a가 추가된 것을 확인할 수 있습니다.
git remote -v
app-a git@github.com:2circumflex/app-a.git (fetch)
app-a git@github.com:2circumflex/app-a.git (push)
origin git@github.com:2circumflex/app-b.git (fetch)
origin git@github.com:2circumflex/app-b.git (push)
2. fetch
app-a의 commit 정보를 가져오기 위해서는 app-a의 remote를 fetch해야 합니다.
git fetch <remote-name>
git fetch app-a
remote: Enumerating objects: 34, done.
remote: Counting objects: 100% (34/34), done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 34 (delta 12), reused 31 (delta 9), pack-reused 0 (from 0)
Unpacking objects: 100% (34/34), 147.22 KiB | 375.00 KiB/s, done.
From github.com:2circumflex/app-a
* [new branch] main -> app-a/main
3. commit 로그 확인
app-a의 commit 목록을 확인합니다.
git log <remote-name>/<branch-name>
git log app-a/main
973dcb0 13:26:15 Roy 커밋 e
8970071 13:21:53 Roy 커밋 d
1d293ba 13:13:06 Roy 커밋 c
4ff5bf0 13:04:44 Roy 커밋 b
3a84645 12:58:04 Roy 커밋 a
4. cherry-pick
가져올 커밋을 아래 명령어로 가져옵니다.
git cherry-pick <commit-hash>
git cherry-pick 3a84645
여러개의 커밋을 한번에 cherry-pick 하는 방법
여러개의 커밋을 가져오기
git cherry-pick <commit-hash> <commit-hash> <commit-hash>
git cherry-pick 3a84645 4ff5bf0 1d293ba
커밋을 범위로 가져오기 1
git cherry-pick <start-hash>..<end-hash>
이 명령어는 start-hash를 제외하고 end-hash까지 가져옵니다.
git cherry-pick 4ff5bf0..973dcb0
// 4ff5bf0 : 커밋 b
// 973dcb0 : 커밋 e
커밋 c 부터 커밋 e 까지 가져옵니다.
커밋을 범위로 가져오기 2
git cherry-pick <start-hash>^..<end-hash>
start-hash를 포함해서 end-hash까지 가져옵니다.
^
기호는 부모 커밋을 참조합니다.
Git의 상대 참조 포스트를 참고해주세요.
git cherry-pick 4ff5bf0^..973dcb0
// 4ff5bf0 : 커밋 b
// 973dcb0 : 커밋 e
커밋 b 부터 커밋 e 까지 가져옵니다.
start-hash가 최초 커밋이면 참조할 부모 커밋이 없어 에러가 발생합니다.