How to permanently prevent specific part of a file

2019-02-24 23:48发布

I have cloned a remote SVN repository with git-svn. I have modified a pom.xml file in this cloned repo in a way that the code compiles. This setup is exclusive for me. Thus I don't want to push the changes back on the remote repo.

Is there a way to prevent this (partial) change of a file from being committed into the repo? I'm aware of the fact, that I could use a personal branch, but this would mean certain merging overhead. Are there other ways?

I've looked into this question and this one, but they are for rather temporal changes.

Update: I'm also aware of the .gitignore possibilities, but this would mean to exclude the file completely.

2条回答
爷、活的狠高调
2楼-- · 2019-02-25 00:04

EDIT: What you are asking is impossible, I didn't see the "partial" part. I know you can commit only part of files, but you cannot ignore some part of file. You will need to use the update-index trick to avoid having it in the "status" and you will need to stash that file every time you will rebase/merge from the remote, and then unstash your modification and ignore your modification with update-index. I don't know if you can create a git alias for a sequence of git commands so with one command you could do all those 3 commands to avoid the hassle

use a .gitignore file, and don't push it to the remote repo too: Ignore the .gitignore file itself

in your .gitignore, you should have

 .gitignore
 path/to/pom.xml

a .gitignore file can be at the root of the working tree, or in any subdirectory you want/need

查看更多
我命由我不由天
3楼-- · 2019-02-25 00:14

There might be a possiblity with filters. But this needs to be explored more.

Starting point: Implement the smudge / clean for this filter:

git config --global filter.versionUpdate.smudge 'sed "s/<version>14.5.0<\/version>/<version>14.5.FEATURE-15<\/version>/"'
git config --global filter.versionUpdate.clean 'sed "s/<version>FEATURE-15<\/version>/<version>14.5.0<\/version>/"'

In this way, the file is always the same when comparing, since the filter is applied.

Problem: It still needs investigation on how to find the filtered files back. This is not so evident in the working directory.

Smudge clean filter

查看更多
登录 后发表回答