40

gitリポジトリに2つのブランチがあり、それらをdevとtestと呼びましょう。単一のファイルsomecode.jsに変更があります。どちらのブランチもsomecode.jsに変更があります。2つのブランチは大幅に(ただし管理可能に)分岐しているため、単純な「マージ」では不十分です。

http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/を試しましたが、両方のファイルの内容がマージされません。基本的には、ファイルの内容を実際にマージするのではなく、ファイルを上書きするだけです。

私も試しました:

git checkout -b newbranch
git checkout test somecode.js
git commit -m "somecode changes from newbranch"
git checkout dev
git merge newbranch

git checkout -m test somecode.js

(マージ用の-mは本当に期待できましたが、うまくいかなかったようです...)

必要なものに近いと思いましたが、コミットが早送りされただけで、マージされず、テストで元のファイルに上書きされたことがわかりました。

繰り返しになりますが、gitを使用してマージするブランチのファイルを上書きせずに、特定のファイルを1つのブランチから別のブランチにマージするにはどうすればよいですか。

4

3 に答える 3

41

使いたいと思います

git checkout -p

あなたの場合

git checkout dev
git checkout -p test somecode.js

また、diffをインタラクティブに適用できます。

于 2012-12-04T21:40:17.380 に答える
5
git checkout dev
git show test:somecode.js > somecode.js.theirs
git show $(git merge-base dev test):somecode.js > somecode.js.base
git merge-file somecode.js somecode.js.base somecode.js.theirs 

このようにして、test ブランチの somecode.js から dev ブランチの somecode.js への 3 方向マージを手動で行います。

または..必要な変更を加えた一時的なブランチを作成し、そこからスカッシュ マージを行うことができます。「git way」で十分ですか?:)

git checkout -b test/filtered $(git merge-base dev test)
git diff ..test -- somecode.js | git apply
git add -- somecode.js
git commit -m "Updated somecode.js"
git checkout dev
git merge --squash test/filtered
git branch -D test/filtered
于 2012-12-04T21:07:40.853 に答える
1

git merge-file が探しているものだと思います。マニュアルページから:

git merge-file incorporates all changes that lead from the <base-file> to
<other-file> into <current-file>. The result ordinarily goes into <current-file>.
git merge-file is useful for combining separate changes to an original. Suppose
<base-file> is the original, and both <current-file> and <other-file> are
modifications of <base-file>, then git merge-file combines both changes.
于 2012-12-04T20:56:59.357 に答える