0

I have 2 branches, dev and master. In dev, there is a file that I don't want in master, but I still need to track it. However I cannot get git to ignore this file when merging from dev to master.

I use 2 machines, one where I commit to dev and other where I merge. The sequence I did is the following:

1. Machine 1: Create and push .gitattributes

Create a .gitattributes file with:

path/to/file.py merge=ignore

Commit file:

git add .gitattributes
git commit -m "adding .gitattributes"
git push

2. Machine 2: Merge

git config --global merge.ignore.driver true
git pull
git checkout master
git merge dev
# file is neither in master nor in dev, will e commited in the next step
ls path/to/file.py
# ls: cannot access 'path/to/file.py': No such file or directory

3. Machine 1: Commit the file to be ignored on merge

git add path/to/file.py
git commit -m "adding file.py"
git push

4. Machine 2: despite .gitattributes, git merge does not ignore file.py

git merge dev --no-commit
# file is not ignored and ready to be merged to master...
ls path/to/file.py
# path/to/file.py

What am I doing wrong?

EDIT: This answer was suggested as duplicate. You may see from commands above that I'm doing basically what is suggested in the answers there, but it does not work, so I keep trying to find the solution. Also, Git hooks mentioned in the same place are not portable (repo is used by both Windows and Mac users).

elkarel
  • 723
  • 2
  • 7
  • 20
  • You cannot ignore a file that is tracked in git. Even if a file matches a pattern in your `.gitignore` file, when you add it to your repository explicitly you are telling git "do not ignore this file". – larsks Mar 27 '23 at 15:17
  • 2
    Does this answer your question? [Git - Ignore files during merge](https://stackoverflow.com/questions/15232000/git-ignore-files-during-merge) – Eugene Sh. Mar 27 '23 at 15:18
  • Hi @EugeneSh. Thanks for the link - but I think the accepted answer does basically what I do.. Or not? – elkarel Mar 27 '23 at 15:43
  • @larsks, thanks, the issue is that I still need to track the file in the dev branch, so I guess .gitignore is not an option. – elkarel Mar 27 '23 at 15:45
  • It has a bunch of other answers. I guess if nothing works for you, then you are out of luck. Personally I'd simply merge with `--no-commit` and manually remove the file from staging. – Eugene Sh. Mar 27 '23 at 15:46
  • @EugeneSh - sure, I was just hoping for more automated solution that wouldn't require me explaining this to everybody and remembering it forever. Some of the other answers suggest git hooks, but I think that's not portable. – elkarel Mar 27 '23 at 15:50

0 Answers0