Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Possible to switch branch after --skip-worktree

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.

like image 204
Eray Avatar asked Apr 09 '15 21:04

Eray


People also ask

How do I force switch to another branch?

You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

What happens if I switch branches without committing?

when you switch to a branch without committing changes in the old branch, git tries to merge the changes to the files in the new branch. If merging is done without any conflict, swithing branches will be successful and you can see the changes in the new branch.

How do I change my Worktree?

refs/heads/master can be replaced with the specific commit. Create as many worktrees for a detached HEAD as you want to. You can run git worktree add /path/foo1 refs/heads/master , git worktree add /path/foo2 refs/heads/master , ..., git worktree add /path/fooN refs/heads/master . Reset develop to master .

What is Skip Worktree?

--skip-worktree explained: This allows you to make changes to a file that you don't want to be pushed to upstream. Use this option as already shown above.


1 Answers

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     } } 
like image 140
stevospinks Avatar answered Sep 25 '22 14:09

stevospinks