[clug] Telling git to not update files across braches
Chris Smart
clug at csmart.io
Wed Aug 1 11:23:58 UTC 2018
On Wed, 1 Aug 2018, at 15:56, jm via linux wrote:
> Say you have a git repository where the branches represent different
> execution environments (eg, dev, staging, prod). Some files should be
> carried across when commands such as,
>
> git merge dev
>
> are executed. Other files hold environment specific settings and should
> not be carried over except possibly manually. Pulls from remote servers
> should be merge within the branch though. I've tries using
> .gitattributes with "merge=ours" but this doesn't seem to work. Does
> anyone have any possible solutions?
>
There may be other ways to handle your environment configs, but anyway, I think your .gitattributes file also needs a filename/regex to match on.
Basically what you're doing there is specifying a merge strategy called "ours" for any files that match. You also need to define the "ours" strategy in your git config (locally or globally) and then when you merge in a branch you can specify that strategy (it will use "ours" if there's a conflict in the files, but there may not always be).
Here's an example of something which should work:
$ git init
Initialized empty Git repository in /tmp/git/.git/
(master#)$ git config merge.ours.driver true # uses bash builtin for true
(master #)$ echo "config.yaml merge=ours" > .gitattributes
(master #%)$ git add .gitattributes
(master +)$ git commit -m "add gitattributes to ignore config on merge"
[master (root-commit) 9d63728cae51a809] add gitattributes to ignore config on merge
1 file changed, 1 insertion(+)
create mode 100644 .gitattributes
(master)$ cat > config.yaml << EOF
environment:
- name: dev
server: dev-hostname
EOF
(master %)$ git add config.yaml
(master +)$ git commit -m "add config for dev"
[master 6dd23a88f24aa4cb] add config for dev
1 file changed, 3 insertions(+)
create mode 100644 config.yaml
(master)$ git checkout -b prod
Switched to a new branch 'prod'
(prod)$ cat > config.yaml << EOF
environment:
- name: prod
server: prod-hostname
EOF
(prod *)$ cat config.yaml
environment:
- name: prod
server: prod-hostname
(prod *)$ git add config.yaml
(prod +)$ git commit -m "add config for prod"
[prod ca30a00ee306939d] add config for prod
1 file changed, 2 insertions(+), 2 deletions(-)
(prod)$ cat config.yaml
environment:
- name: prod
server: prod-hostname
(prod)$ git checkout master
Switched to branch 'master'
(master)$ cat config.yaml
environment:
- name: dev
server: dev-hostname
(master)$ git merge -s ours prod
Merge made by the 'ours' strategy.
(master)$ cat config.yaml
environment:
- name: dev
server: dev-hostname
(master)$ git checkout prod
Switched to branch 'prod'
(prod)$ git merge -s ours master
Merge made by the 'ours' strategy.
(prod)$ cat config.yaml
environment:
- name: prod
server: prod-hostname
-c
More information about the linux
mailing list