Development Tip

git에서 하위 디렉토리를 어떻게 병합합니까?

yourdevel 2020. 10. 24. 11:56
반응형

git에서 하위 디렉토리를 어떻게 병합합니까?


로컬 git 브랜치에서 원격 git 브랜치로 하위 디렉토리의 변경 사항 만 병합 할 수 있습니까? 아니면 "모두 또는 아무것도"입니까?

예를 들면 다음과 같습니다.

branch-a
 - content-1
 - dir-1
   - content-2

branch-b
 - content-1
 - dir-1
   - `content-2

branch-a dir-1의 내용과 branch-b dir-1의 내용 만 병합하고 싶습니다.


" 선택 파일을 git-merge와 어떻게 병합합니까? "라는 질문에 대한 대안으로 방금 git read-tree를 기반으로 전체 하위 디렉토리를 병합하는 데 더 적합 할 수있는 이 GitHub 스레드찾았 습니다 .

  • 내 저장소 => cookbooks
    내 저장소 대상 디렉토리 =>cookbooks/cassandra

  • 원격 저장소 => infochimps
    병합하려는 원격 저장소 소스 cookbooks/cassandra=>infochimps/cookbooks/cassandra

병합하는 데 사용한 명령은 다음과 같습니다.

  • 저장소를 추가하고 가져옵니다.
git remote add -f infochimps git : //github.com/infochimps/cluster_chef.git
  • 병합 수행
git merge -s ours --no-commit infochimps / master
  • 만 병합 infochimps/cookbooks/cassandra으로cassandra
git read-tree --prefix = cassandra / -u infochimps / master : cookbooks / cassandra
  • 변경 커밋
 git commit -m 'merging in infochimps cassandra'

추가

기이합니다. [편집]read-tree단계는 다음과 같이 실패 할 수 있습니다.

error: Entry 'infochimps/cookbooks/cassandra/README' overlaps with 'cookbooks/cassandra/README'. Cannot bind.

... 두 파일이 동일한 경우에도 . 도움이 될 수 있습니다.

git rm -r cassandra
git read-tree --prefix=cassandra/ -u infochimps/master:cookbooks/cassandra

그러나 물론, 이것이 원하는 것을 수동으로 확인하십시오.


내 예를 들어, 자신의 업스트림 버전을 반영하고 (로컬 전용 인 경우에는 아님) 최신 코드로 가져 오는 분기 '소스'및 분기 '대상'이 있다고 가정합니다. 'source'브랜치에만 존재하는 newFeature라는 저장소의 하위 디렉토리를 원한다고 가정 해 보겠습니다.

git checkout destination
git checkout source newFeature/
git commit -am "Merged the new feature from source to destination branch."
git pull --rebase
git push

내가 본 다른 모든 것보다 훨씬 덜 복잡하며 이것은 여기 에서 찾을 수 있습니다 .

이것은 '실제 병합'이 아니므로 대상 브랜치에 newFeature에 대한 커밋 정보가없고 해당 하위 디렉터리의 파일에 대한 수정 사항 만 있습니다. 그러나 아마도 나중에 전체 분기를 병합하거나 폐기 할 것이기 때문에 문제가되지 않을 수 있습니다.


Eclipse 포럼 스레드 에서 이것을 얻었 으며 매력처럼 작동했습니다.

git checkout source-branch
git checkout target-branch <directories-or-files-you-do-**NOT**-want> 
git commit
git checkout target-branch
git merge source-branch

create git repo에는 branch-a와 branch-b가 모두 포함됩니다.

git checkout branch-a
git diff branch-b dir-1 > a.diff
patch -R -p1 < a.diff

git cherry-pick원하는 커밋을 선택하고 이러한 커밋 만 병합하는 데 사용 합니다. 여기서 핵심 트릭은 이러한 커밋을 쉬운 방법으로 가져 오는 것입니다 (따라서 수동으로 git 로그를 확인하고 직접 입력하여 확인할 필요가 없습니다). 방법은 다음과 같습니다. 다음과 git log같이 커밋의 sha1 ID를 인쇄하는 데 사용 합니다.

git log ^<commit-a> <commit-b> --pretty=format:"%h" --reverse -- <subdir>

commit-a는 병합 할 분기의 시작 지점 직전의 커밋이고 commit-b는 병합 할 분기의 마지막 커밋입니다. --reverse는 나중에 체리 선택을 위해 이러한 커밋을 역순으로 인쇄합니다.

그런 다음 다음과 같이하십시오.

git cherry-pick $(git log ^<commit-a> <commit-b> --pretty=format:"%h" --reverse -- <subdir>)

간단하고 안정적인 두 단계!

참고 URL : https://stackoverflow.com/questions/1214906/how-do-i-merge-a-sub-directory-in-git

반응형