Not Possible to switch branch after --skip-worktre

2019-01-22 01:59发布

WHAT I WANT TO DO

I have a file which contains sensitive datas so I don't want to push content of this file to remote server.

WHAT I DID?

To achieve this, I made a commit when the file was empty and pushed this empty file to server (GitHub). And then fill the file with sensitive datas and applied git update-index --skip-worktree path/to/file . But I didn't made any commit.

Now I'm trying to switch my branch but I'm getting this error :

    error: Your local changes to the following files would be overwritten by checkout:
    path/to/file
    Please, commit your changes or stash them before you can switch branches.
    Aborting

WHY I USE skip-worktree INSTEAD OF assume-unchanged?

I read a few SO questions about this subject, and found Borealid's answer.

--assume-unchanged assumes that a developer shouldn’t change a file. This flag is meant for improving performance for not-changing folders like SDKs.

--skip-worktree is useful when you instruct git not to touch a specific file ever because developers should change it. For example, if the main repository upstream hosts some production-ready configuration files and you don’t want to accidentally commit changes to those files, --skip-worktree is exactly what you want.

After this, I found Jeff's question and VonC's answer. Jeff's problem is almost same with mine, and I followed VonC's solution. However it's not work for me. Maybe because of git version difference. Because that question from 2012. We talked with VonC and he said to ask this as a new question because he couldn't remember answer.

I tried to use --assume-unchanged and --skip-worktree together, and soft reseting worktree. But nothing changed.

SO?

Can you help me about my problem ?

Thank you.

7条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-22 02:57

Is there a reason to have an empty variant of that file checked in at all? If not

git rm --cached path/to/file
echo 'path/to/file' >> .gitignore
git commit -m'stop tracking path/to/file'

might solve your problem. (Will have to be repeated for each branch that has the file. Or if you don't mind history-rewriting, use git filter-branch to get rid of it in previous commits, too.)

查看更多
登录 后发表回答