Not Possible to switch branch after --skip-worktree

不羁岁月 提交于 2019-11-28 17:48:31

I haven't been able to find a neat solution to this, so I'm using a .bat file to run --no-skip-worktree on the affected files. I then stash, switch branch, stash apply, and then use another .bat file to run --skip-worktree on the same files.

It's not nice, but it's the simplest and quickest way I've found so far.

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.)

You can enable sparse checkout and add the file there together with adding the skip-worktree flag (if you add the sparse checkout alone it probably will be deleted).

To exclude a file you should put to sparse-checkout file (see git-read-tree manual):

/*
!unwanted

Then it will not be touched at update (so you don't get content of the other branch there, instead preserving your edits). It might be not what you want.

Well, this is a cruddy solution, but seems to work reasonably well (though it is only lightly tested):

Create a file named git-checkoutsw somewhere in your PATH, with the following content:

#!/bin/bash
ignored=( $(git ls-files -v | grep "^S " | cut -c3-) )
for x in "${ignored[@]}"; do
  git update-index --no-skip-worktree -- "$x"
done
git checkout -m $@
for x in "${ignored[@]}"; do
  git update-index --skip-worktree -- "$x"
done

The script captures the list of ignored files, unignores them, checks out the new branch with -m (for merge) and whatever other parameters have been specified, and then ignores them again.

Now you can do git checkoutsw instead of git checkout.

So far, --assume-unchanged and --skip-worktree do not work as I found. My git version is : git version 2.8.4 (Apple Git-73) Stash is the only way that works so far.

Just find one line to do --no-skip-worktree on all files

git ls-files -v | grep "^S" | cut -c 3- | xargs -L1 git update-index --no-skip-worktree

You can easily make a little alias.unhideall with that command :)

I got the same problem and it boiled down to be casued by incoming changes on the file I wanted to assume-unchanged on.

So if I just do no-assume-unchanged on the file, stash it, checkout the latest version, pop my stash, and assume-unchanged again.

So now I'm able to switch branches again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!