2

Suppose that, while on some branch branch-X I add and commit a certain file, foo.txt, and that this file is not being tracked by any other branch in the repo besides branch-X.

I want to tell git that, whenever branch-X is merged into any other branch in the repository, the file foo.txt should never be added.

I have tried doing this by, e.g., setting the following line in the appropriate .gitattributes file

foo.txt merge=ours

...but the file keeps getting added whenever I run

git checkout otherbranch
git merge branch-X

I guess that the issue here is that there's no real "conflict" between the file foo.txt in branch-X, and "no file" in otherbranch, so the merge strategy does not need to come into play. (BTW, this is why this question is not a duplicate, AFAICT: all related questions I've found refer to the case of conflicting files that exist in both branches of the merge, and the answers given to those questions don't seem to work for the case I'm presenting here.)

Also, at least in cases where the merge is a fast-forward, no new commit is done, so it's no use to run git-merge with the --no-commit option in the hopes of explicitly removing foo.txt from the index.

I imagine that the mechanics underlying what I'm trying to do here would require translating the command

git merge branch-X

into the sequence

git branch branch-X-no-foo branch-X
git checkout branch-X-no-foo
git rm foo.txt
git commit -m 'pay no attention to the man behind the curtain...'
    # or maybe
    # git commit -C HEAD --amend
git checkout otherbranch
git merge branch-X-no-foo

I suppose I that I could write a script to wrap this sequence into an easy-to-type command, but I thought I'd ask if there's a better approach.

kjo
  • 33,683
  • 52
  • 148
  • 265
  • Isn't this `git merge --no-commit branch` followed by `git rm --cached foo.txt` followed by `commit`? – matt Apr 14 '14 at 01:25
  • @matt: as I pointed out in my post, that may work unless the merge is a fast-forward; in the FF case there's no commit. (I suppose that in the FF case, one could do `git rm foo.txt` followed by `git commit -C HEAD --amend`... I need to look into this.) At any rate, it seems that I'll have to write some scripts to automate this... – kjo Apr 14 '14 at 01:41
  • 2
    Okay, `git merge --no-commit --no-ff` then. – matt Apr 14 '14 at 02:11

1 Answers1

1

One actual workaround to avoid having to deal with merge driver is to:

  • version a foo.txt.tpl template file
  • version a foo.txt.values with values for the right branch(es)
  • version a script able to detect the current branch and generate (or not generate, depending on the current branch) the right foo.txt file on checkout (smudge script)

See this example of content filter driver:

enter image description here

(Picture from "Customizing Git Attributes" from the Git Book)

That way, you don't have to deal with merge at all.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250