IT

특정 파일에서 충돌하는 병합을 위해 항상 내 로컬 버전을 선택하도록 git에 지시하는 방법은 무엇입니까?

lottoking 2020. 8. 28. 19:32
반응형

특정 파일에서 충돌하는 병합을 위해 항상 내 로컬 버전을 선택하도록 git에 지시하는 방법은 무엇입니까?


내가 git 저장소를 통해 다른 사람과 공동 작업하고 외부 변경 사항을 절대 받아들이고 싶지 않은 특정 파일이 가정 해 보겠습니다.

git pull 할 때마다 충돌하는 병합에 대해 불평하지 않습니다. 이 파일을 병합 할 때 항상 내 로컬 버전을 선택하고 싶습니다.


구성 파일의 특정 인스턴스에서 Ron의 대답에 동의합니다 .
구성은 작업 공간에 대해 "비공개"에 대해 (은행 " .gitignore파일 에서 선언 됨"에서와 같이 "무시 "됨)합니다. 토큰 화 된 값
이 포함 된 구성 파일 템플릿해당 파일을 개인 (및 무시 된) 구성 파일로 변환 할 수 있습니다 .config.template


이에 대한 답변이 아닙니다.

특정에서 충돌하는 병합을 위해 항상 내 로컬 버전을 선택하도록 git에게 알리려면 어떻게해야합니까? (모든 파일 또는 파일 그룹)

여러 종류의 병합은 "복사 병합"으로, 충돌이있을 때마다 항상 파일의 '우리의'또는 '그들의'버전을 복사합니다.

(로 브라이언 반덴버그의 노트 게임에 , ' ours'과 '은 theirs'여기에 병합에 사용 됩니다.
그들이 반전 A에 대한 REBASE 다음을 참조하십시오 " "는 REBASE를 사용하는 " , 추적을 유지하는 '원격' '지역 '및 ')Why is the meaning of “ours” and “theirs” reversed with git-svngit rebase

"파일"(일반적으로 "구성"파일을 말하는 것이 나쁜 예이기 때문에 파일)의 경우 병합을 통해 호출되는 사용자 확장을 사용하면됩니다.
당신이 정의하는 것 때문에 힘내는 해당 펼쳐를 호출합니다 gitattributes의 정의, 사용자 정의 병합을 .

이 경우 "사용자 병합 드라이버"는 기본적으로 현재 버전을 변경하지 않고 유지하지 않고 항상 로컬 버전을 선택할 수 있습니다.


Windows에서 msysgit 1.6.3을 사용하여 단순한 DOS 세션에서 간단한 시나리오로 테스트 해 보겠습니다.

cd f:\prog\git\test
mkdir copyMerge\dirWithConflicts
mkdir copyMerge\dirWithCopyMerge
cd copyMerge
git init
Initialized empty Git repository in F:/prog/git/test/copyMerge/.git/

이제 충돌이 발생하지만 서로 다른 방식으로 병합되는 두 개의 파일을 만들어 보겠습니다.

echo a > dirWithConflicts\a.txt
echo b > dirWithCopyMerge\b.txt
git add -A
git commit -m "first commit with 2 directories and 2 files"
[master (root-commit) 0adaf8e] first commit with 2 directories and 2 files

두 개의 다른 git 브랜치에있는 두 파일의 내용에 "충돌"을 도입 할 것입니다.

git checkout -b myBranch
Switched to a new branch 'myBranch'
echo myLineForA >> dirWithConflicts\a.txt
echo myLineForB >> dirWithCopyMerge\b.txt
git add -A
git commit -m "add modification in myBranch"
[myBranch 97eac61] add modification in myBranch

git checkout master
Switched to branch 'master'
git checkout -b hisBranch
Switched to a new branch 'hisBranch'
echo hisLineForA >> dirWithConflicts\a.txt
echo hisLineForB >> dirWithCopyMerge\b.txt
git add -A
git commit -m "add modification in hisBranch"
[hisBranch 658c31c] add modification in hisBranch

이제 "hisBranch"를 "myBranch"에 병합 해 보겠습니다.

  • 충돌하는 병합에 대한 수동 해결

  • dirWithCopyMerge\b.txt항상 버전 을 유지하려는 경우제외하고b.txt .

병합은 ' MyBranch' 에서 발생하는 다시 전환 gitattributes하고 병합 동작을 사용자 지정하는 ' '지시문을 추가합니다 .

git checkout myBranch
Switched to branch 'myBranch'
echo b.txt merge=keepMine > dirWithCopyMerge\.gitattributes
git config merge.keepMine.name "always keep mine during merge"
git config merge.keepMine.driver "keepMine.sh %O %A %B"
git add -A
git commit -m "prepare myBranch with .gitattributes merge strategy"
[myBranch ec202aa] prepare myBranch with .gitattributes merge strategy

우리는이 .gitattributes에 정의 파일 dirWithCopyMerge디렉토리 (만 병합 의지가 포함 지점의 정의 :) myBranch, 우리는 .git\config지금 병합 드라이버가 포함을 포함합니다.

[merge "keepMine"]
        name = always keep mine during merge
        driver = keepMine.sh %O %A %B

아직 keepMine.sh를 정의하지 않고 병합을 시작하면 다음과 같은 결과를 얻을 수 있습니다.

git merge hisBranch
sh: keepMine.sh: command not found
fatal: Failed to execute internal merge
git st
# On branch myBranch
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   dirWithConflicts/a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

type dirWithConflicts\a.txt
a
<<<<<<< HEAD:dirWithConflicts/a.txt
myLineForA
=======
hisLineForA
>>>>>>> hisBranch:dirWithConflicts/a.txt

괜찮습니다.

  • a.txt 병합 할 준비가되어 있으며 충돌이 있습니다.
  • b.txt병합 드라이버가 처리해야하기 때문에 여전히 변경되지 않습니다 ( .gitattributes디렉토리 파일에 있는 지시문으로 인해 ).

정의 keepMine.sh당신의 어디를 %PATH%(또는 $PATH우리의 유닉스 친구 나는 물론 모두를 수행합니다. 나는 버추얼 세션에서 우분투 세션이)

으로 주석 에 의해 lrkwz , 그리고 "에 설명 된 병합 전략 의"섹션 사용자 정의 힘내 - 힘내 속성 , 당신은 쉘 명령으로 쉘 스크립트를 대체 할 수 있습니다 true.

git config merge.keepMine.driver true

그러나 일반적인 경우에는 스크립트 파일을 정의 할 수 있습니다.

keepMine.sh

# I want to keep MY version when there is a conflict
# Nothing to do: %A (the second parameter) already contains my version
# Just indicate the merge has been successfully "resolved" with the exit status
exit 0

(그것은 하나의 간단한 병합 드라이버였습니다.) (이 경우 더 간단하게 사용하십시오. true)
(다른 버전을 유지하려면 exit 0앞에 추가 하십시오
cp -f $3 $2..
그게 다입니다. 병합 드라이버는 다른 버전에서 오는 버전을 유지하지 않습니다. 분기, 로컬 변경 무시)

이제 처음부터 병합을 다시 시도해 보겠습니다.

git reset --hard
HEAD is now at ec202aa prepare myBranch with .gitattributes merge strategy

git merge hisBranch
Auto-merging dirWithConflicts/a.txt
CONFLICT (content): Merge conflict in dirWithConflicts/a.txt
Auto-merging dirWithCopyMerge/b.txt
Automatic merge failed; fix conflicts and then commit the result.

병합이 실패합니다 ... a.txt에 대해서만 .
a.txt를 편집하고 'hisBranch'의 줄을 그대로 둔 다음 :

git add -A
git commit -m "resolve a.txt by accepting hisBranch version"
[myBranch 77bc81f] resolve a.txt by accepting hisBranch version

이 병합 중에 b.txt가 보존되었는지 확인하겠습니다.

type dirWithCopyMerge\b.txt
b
myLineForB

마지막 커밋은 전체 병합을 나타냅니다 .

git show -v 77bc81f5e
commit 77bc81f5ed585f90fc1ca5e2e1ddef24a6913a1d
Merge: ec202aa 658c31c
git merge hisBranch
Already up-to-date.

(Merge로 시작하는 줄이이를 증명합니다)


Git은 다음과 같이 병합 드라이버를 정의, 결합 및 / 또는 덮어 쓸 수 있습니다.

  • 검사 <dir>/.gitattributes(문제의 경로와 동일한 디렉토리에 있음) : .gitattributes디렉토리 에서 다른 디렉토리보다 우선합니다.
  • 그런 다음 .gitattributes(부모 디렉토리에 있음) 검사하고 아직 설정하지 않은 경우 지시문 만 설정합니다.
  • 마지막으로 $GIT_DIR/info/attributes. 이 파일은 트리 내 설정을 재정의하는 데 사용됩니다. <dir>/.gitattributes지시문 을 덮어 씁니다 .

"결합"이란 다중 병합 드라이버를 "집계"한다는 의미입니다.
Nick Green 은 주석 에서 실제로 병합 드라이버를 결합 하려고합니다 . " python git driver를 통한 Merge pom 's "를 참조하십시오 .
그러나 그의 다른 질문 에서 언급했듯이 충돌이 발생한 경우에만 작동합니다 (두 분기에서 동시에 수정).


덮어 쓰지 않으려는 구성 파일이 여러 개 있습니다. 그러나 .gitignore 및 .gitattributes는 우리 상황에서 작동하지 않았습니다. 우리의 해결책은 구성 파일을 구성 브랜치에 저장하는 것이 었습니다. 그런 다음 git 병합 중에 파일이 변경되도록 허용하되 병합 직후 "git checkout branch-."를 사용하십시오. 병합 후 구성 브랜치에서 구성 파일을 복사합니다. 여기에 자세한 스택 오버플로 답변

참고 URL : https://stackoverflow.com/questions/928646/how-do-i-tell-git-to-always-select-my-local-version-for-conflicted-merges-on-as

반응형