Development Tip

다른 git 저장소에서 커밋을 체리로 선택할 수 있습니까?

yourdevel 2020. 9. 30. 11:35
반응형

다른 git 저장소에서 커밋을 체리로 선택할 수 있습니까?


나는 첫 번째 것을 알지 못하는 다른 git 저장소에서 커밋이 필요한 git 저장소로 작업하고 있습니다.

일반적으로 나는 HEAD@{x}reflog에서를 사용하여 체리를 선택합니다 . 그러나 .git이것은이 reflog 항목 (다른 물리적 디렉토리)에 대해 전혀 알지 못하기 때문에 어떻게 체리를 선택하거나 할 수 있습니까?

나는 git-svn. 내 첫 분기 사용 git-svntrunk서브 버전의 repo의, 다음 분기 사용되는 git-svn서브 버전 분기에.


다른 저장소를 원격으로 추가 한 다음 변경 사항을 가져와야합니다. 거기에서 커밋을 볼 수 있으며 체리를 선택할 수 있습니다.

그렇게 :

git remote add other https://example.link/repository.git
git fetch other

이제 간단하게 할 수있는 모든 정보가 git cherry-pick있습니다.

리모컨 작업에 대한 자세한 정보 : https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes


주어진대로 대답은 format-patch를 사용하는 것이지만 질문은 다른 폴더에서 체리를 선택하는 방법 이었으므로 다음과 같은 코드가 있습니다.

$ git --git-dir=../<some_other_repo>/.git \
format-patch -k -1 --stdout <commit SHA> | \
git am -3 -k

( @cong ma의 설명 )

git format-patch명령은 some_other_repoSHA에 의해 지정된의 커밋 에서 패치를 생성합니다 ( -1하나의 커밋 만 해당). 이 패치는로 파이프되어 git am패치를 로컬로 적용합니다 ( -3패치가 제대로 적용되지 않을 경우 3 방향 병합을 시도 함을 의미 함). 그것이 설명되기를 바랍니다.


다음은 remote-fetch-merge의 예입니다.

cd /home/you/projectA
git remote add projectB /home/you/projectB
git fetch projectB

그런 다음 다음을 수행 할 수 있습니다.

git cherry-pick <first_commit>..<last_commit>

또는 전체 분기를 병합 할 수도 있습니다.

git merge projectB/master

할 수 있지만 두 단계가 필요합니다. 방법은 다음과 같습니다.

git fetch <remote-git-url> <branch> && git cherry-pick FETCH_HEAD

<remote-git-url>체리를 선택하려는 저장소의 URL 또는 경로로 바꿉니다 .

<branch>원격 저장소에서 선택하려는 브랜치 또는 태그 이름으로 바꿉니다 .

FETCH_HEAD브랜치에서 git SHA로 바꿀 수 있습니다 .

업데이트 됨 : @pkalinow의 피드백에 따라 수정되었습니다.


다음은 원격 추가, 분기 가져 오기, 커밋 체리 선택 단계입니다.

# Cloning our fork
$ git clone git@github.com:ifad/rest-client.git

# Adding (as "endel") the repo from we want to cherry-pick
$ git remote add endel git://github.com/endel/rest-client.git

# Fetch their branches
$ git fetch endel

# List their commits
$ git log endel/master

# Cherry-pick the commit we need
$ git cherry-pick 97fedac

출처 : https://coderwall.com/p/sgpksw


See How to create and apply a patch with Git. (From the wording of your question, I assumed that this other repository is for an entirely different codebase. If it's a repository for the same code base, you should add it as a remote as suggested by @CharlesB. Even if it is for another code base, I guess you could still add it as a remote, but you might not want to get the entire branch into your repository...)


You can do it in one line as following. Hope you are in git repository which need the cherry-picked change and you have checked out to correct branch.

git fetch ssh://git@stash.mycompany.com:7999/repo_to_get_it_from.git branchToPickFrom && git cherry-pick 02a197e9533
# 

git fetch [branch URL] [Branch to cherry-pick from] && git cherry-pick [commit ID]


Yes. Fetch the repository and then cherry-pick from the remote branch.


Assuming A is the repo you want to cherry-pick from, and B is the one you want to cherry-pick to, you can do this by adding </path/to/repo/A/>/.git/objects to </path/to/repo/B>/.git/objects/info/alternates. Create this alternates files if it does not exist.

This will make repo B access all git objects from repo A, and will make cherry-pick work for you.


My situation was that I have a bare repo that the team pushes to, and a clone of that sitting right next to it. This set of lines in a Makefile work correctly for me:

git reset --hard
git remote update --prune
git pull --rebase --all
git cherry-pick -n remotes/origin/$(BRANCH)

By keeping the master of the bare repo up to date, we are able to cherry-pick a proposed change published to the bare repo. We also have a (more complicated) way to cherry-pick multiple braches for consolidated review and testing.

If "knows nothing" means "can't be used as a remote", then this doesn't help, but this SO question came up as I was googling around to come up with this workflow so I thought I'd contribute back.

참고URL : https://stackoverflow.com/questions/5120038/is-it-possible-to-cherry-pick-a-commit-from-another-git-repository

반응형