I am working with git p4 rebase and git LFS. unfortunately after syncing the P4 contents to local git repo, it removes all the existing LFS tracked files from .gitattributes, and adds files to .gitattributes that were synced.
example:
Local and remote .gitattributes file before sync
File 1 filter=lfs diff=lfs merge=lfs -text
File 2 filter=lfs diff=lfs merge=lfs -text
File 3 filter=lfs diff=lfs merge=lfs -text
local .gitattributes file after sync
File 4 filter=lfs diff=lfs merge=lfs -text
File 5 filter=lfs diff=lfs merge=lfs -text
Now, if I push to remote, I will lose tracking on 1,2,3. So before I push I need to merge the remote .gitattributes file with the local .gitattributes file to obtain
File 1 filter=lfs diff=lfs merge=lfs -text
File 2 filter=lfs diff=lfs merge=lfs -text
File 3 filter=lfs diff=lfs merge=lfs -text
File 4 filter=lfs diff=lfs merge=lfs -text
File 5 filter=lfs diff=lfs merge=lfs -text
Then I can push to remote.
The following works
git show origin/master:.gitattributes > ./.gitattributes.theirs
touch blankFile
git merge-file --union .gitattributes blankFile .gitattributes.theirs
if you do not want to overrride .gitattributes you can do
git merge-file -p --union .gitattributes blankFile .gitattributes.theirs > .gitattributes.new