Not Possible to switch branch after --skip-worktree

后端 未结 7 1216
醉酒成梦
醉酒成梦 2020-12-13 03:37

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 achiev

相关标签:
7条回答
  • 2020-12-13 04:19

    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 讨论(0)
  • 2020-12-13 04:20

    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 讨论(0)
  • 2020-12-13 04:25

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:31

    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.

    0 讨论(0)
  • 2020-12-13 04:36

    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.

    0 讨论(0)
  • 2020-12-13 04:37

    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 讨论(0)
提交回复
热议问题