LoginSignup
48
47

More than 5 years have passed since last update.

git merge 時に内容が異なるファイルをマージ対象から除外する

Posted at

設定

.gitattributes に以下を追記

ignore_target_file merge=ours

.git/config に以下を追記

[merge "ours"]
    name = "Keep ours merge"
    driver = true

テスト

# develop で commit && push
$ git checkout develop
$ echo "by develop" >> ignore_target_file
by develop
$ git add . & git commit -m "by develop" & git push

# master で commit && push
$ git checkout master
$ echo "by master" >> ignore_target_file
by master
$ git add . & git commit -m "by develop" & git push

# Marge: develop -> master
$ git merge develop
Merge made by the 'recursive' strategy.

# merge 対象から除外されていることを確認する
$ cat ignore_target_file
by master

# Note: 本来であれば,develop の ignore_target_file の内容がマージされる(この場合conflictしちゃうと思います).

以上.
これで,ignore_target_file をブランチごとに別々の値を持つことができます.
.gitignore との最大の違いはバージョン管理下に置ける点でしょうか.

また,.gitignore と同様に "config/* merge=ours"の様に書くと複数ファイルを対象にすることができます.
"config/ merge=ours" の様にディレクトリのみ指定するだけでは上手く機能しないようです.

参考

48
47
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
48
47