Not Possible to switch branch after --skip-worktree

9k views Asked by At

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.

9

There are 9 answers

1
user2762938 On

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.

0
Mickaël B. On

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

0
Raman On

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.

0
max630 On

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.

1
harre On

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.

3
stevospinks On

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.

Update: I've run into this problem again so have created a PowerShell script instead of the .bat file mentioned above. It will prompt the user whether they want to skip or no-skip the files. Update the two parameters and you're ready to go:

[string]$repoPath = "C:\Fully\Qualified\Path"
[string[]]$filesToSkip = @(
    "Local/Path/To/File.txt",
    "Local/Path/To/OtherFile.txt"
)

$skipText = Read-Host -Prompt 'Skip files? (y/n)'
$skip = $skipText -like "y"
cd $repoPath
foreach ($file in $filesToSkip)
{
    if($skip)
    {
        Write-Host "Skipping" $file
        git update-index --skip-worktree $file
    }
    else
    {
        Write-Host "No-skipping" $file
        git update-index --no-skip-worktree $file
    }
}
2
das-g On

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

0
Rider Harrison On

If you are just wanting to switch your branches similar to a git switch X --force you can use the below command to reset any skip-worktree files. Once the command was ran I was able to use git switch without any issues. Note that this does not delete or alter the skip-worktree configuration.

WARNING: THIS WILL OVERWRITE LOCAL CHANGES TO SKIP-WORKTREE FILES

For Windows

git ls-files -v | Select-String '^S' | ForEach-Object { git restore --ignore-skip-worktree-bits ($_.ToString().Substring(2).Trim()) }

For Linux (untested)

git ls-files -v | grep '^S' | while read -r line; do file="${line:2}"; git restore --ignore-skip-worktree-bits "$file"; done

Explanation: The git ls-files piped into the select '^S' looks for all files in your repository that have the skip-worktree bit applied to them and restores them to what your remote branch sees which fixes the error mentioned.

In my case the overwrite isn't a problem because I have a script that runs at build time for my repository which reapplies my config changes that shouldn't be going to prod so as soon as I rebuild my changes are applied.

0
Yuniac On

As a comment mentioned (I'm only writing this here as an answer hoping people who don't read comments or might miss them see this), git update-index --skip-worktree path_to_file works but only if the file is the same across all your local (I'm assuming local) branches.

What worked for me in my case was:

  1. git update-index --no-skip-worktree path_to_file to undo skipping first of all.

  2. deleted all my local branches, (You can use: git branch | grep -v "master" | xargs git branch -D), went to master and modified the file with the sensitive information.

  3. git update-index --skip-worktree path_to_file again.

  4. git checkout any__new_local_branch works just fine now.

Credits and thanks to @kise in the comments on the original question.