IT박스

Git 브랜치에서 특정 파일을 병합하는 방법

itboxs 2020. 6. 3. 21:47
반응형

Git 브랜치에서 특정 파일을 병합하는 방법


git 브랜치 branch1과 branch2가 2 개 있으며 branch2의 file.py를 branch1의 file.py에 병합하고 싶습니다.

본질적으로 나는 branch1의 file.py에 대해 작업하고 싶지만 merge 명령을 이용하고 싶습니다. 가장 좋은 방법은 무엇입니까?


컨텐츠가 branch1file.py 에서 더 이상 branch1에 적용되지 않는 경우 일부 변경 사항을 선택하고 다른 변경 사항 남겨야 합니다. 모든 권한을 얻으려면 스위치를 사용하여 대화식 병합을 수행하십시오 .--patch

$ git checkout --patch branch2 file.py

매뉴얼 페이지의 대화식 모드 섹션에서 git-add(1)사용할 키 대해 설명합니다.

y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

split 명령이 특히 유용합니다.


아닌 있지만 병합 자체로, 때로는 다른 분기에 다른 파일의 전체 내용이 필요하다. Jason Rudolph의 블로그 게시물 은 한 지점에서 다른 지점으로 파일을 복사하는 간단한 방법을 제공합니다. 다음과 같이 기술을 적용하십시오.

$ git checkout branch1 # ensure in branch1 is checked out and active
$ git checkout branch2 file.py

지금은 file.py지금이다 BRANCH1 .


모든 수정 사항이 있습니까 file.py에서 branch2자신의 커밋에서 다른 파일에 대한 수정과 분리? 그렇다면 간단히 다음과 cherry-pick같이 변경하면됩니다.

git checkout branch1
git cherry-pick <commit-with-changes-to-file.py>

그렇지 않으면, merge당신은뿐만 아니라 단지 만들 수 있습니다 ... 이상 개별 경로를 작동하지 않는 경우 git diff의 패치 file.py에서 변화 branch2하고 git apply그들에게를 branch1:

git checkout branch2
git diff <base-commit-before-changes-to-file.py> -- file.py > my.patch
git checkout branch1
git apply my.patch

다른 현재 답변은 merge 명령을 사용하는 것처럼 실제로 파일을 "병합"하지 않습니다. (최상의 경우 수동으로 diff를 선택해야합니다.) 실제로 공통 조상의 정보를 사용하여 병합을 활용하려면 git "고급 병합"섹션있는 절차를 따르십시오. 참조 설명서.

이 프로토콜의 경우 'path / to / file.txt'파일을 origin / master에서 HEAD로 병합하고 싶다고 가정합니다. 적절하게 수정하십시오. (저장소의 최상위 디렉토리에있을 필요는 없지만 도움이됩니다.)

# Find the merge base SHA1 (the common ancestor) for the two commits:
git merge-base HEAD origin/master

# Get the contents of the files at each stage
git show <merge-base SHA1>:path/to/file.txt > ./file.common.txt
git show HEAD:path/to/file.txt > ./file.ours.txt
git show origin/master:path/to/file.txt > ./file.theirs.txt

# You can pre-edit any of the files (e.g. run a formatter on it), if you want.

# Merge the files
git merge-file -p ./file.ours.txt ./file.common.txt ./file.theirs.txt > ./file.merged.txt

# Resolve merge conflicts in ./file.merged.txt
# Copy the merged version to the destination
# Clean up the intermediate files

git merge-file 은 포맷팅 등을 위해 모든 기본 병합 설정을 사용해야합니다.

Also note that if your "ours" is the working copy version and you don't want to be overly cautious, you can operate directly on the file:

git merge-base HEAD origin/master
git show <merge-base SHA1>:path/to/file.txt > ./file.common.txt
git show origin/master:path/to/file.txt > ./file.theirs.txt
git merge-file path/to/file.txt ./file.common.txt ./file.theirs.txt

You can stash and stash pop the file:

git checkout branch1
git checkout branch2 file.py
git stash
git checkout branch1
git stash pop

To merge only the changes from branch2's file.py, make the other changes go away.

git checkout -B wip branch2
git read-tree branch1
git checkout branch2 file.py
git commit -m'merging only file.py history from branch2 into branch1'
git checkout branch1
git merge wip

Merge will never even look at any other file. You might need to '-f' the checkouts if the trees are different enough.

Note that this will leave branch1 looking as if everything in branch2's history to that point has been merged, which may not be what you want. A better version of the first checkout above is probably

git checkout -B wip `git merge-base branch1 branch2`

in which case the commit message should probably also be

git commit -m"merging only $(git rev-parse branch2):file.py into branch1"

I am in same situation, I want to merge a file from a branch which has many commits on it on 2 branch. I tried many ways above and other I found on the internet and all failed (because commit history is complex) so I decide to do my way (the crazy way).

git merge <other-branch>
cp file-to-merge file-to-merge.example
git reset --hard HEAD (or HEAD^1 if no conflicts happen)
cp file-to-merge.example file-to-merge

참고URL : https://stackoverflow.com/questions/18115411/how-to-merge-specific-files-from-git-branches

반응형