5

I am working in a branchX and it has a dozen config files that I do not want to commit. So i marked all the config files as --skip-worktree. Now I want to change to branchY. How do I do it?

I tried

git checkout branchY

and it says

Please commit your changes or stash them before you switch branches.

So i tried to stash them, using

git stash save

but it says

No local changes to save

This is very annoying. Apparently the only solution is

  1. use git ls-files -v to get a list of all the skip-worktree files
  2. for each file, remove the skip-worktree
  3. git stash save
  4. git checkout branchY
  5. git stash pop
  6. manually resolve any conflict with --theirs
  7. for each file, add the skip-worktree flag again

Is there an easier way?

John Henckel
  • 10,274
  • 3
  • 79
  • 79
  • 1
    Did you try ignoring them (with .gitignore) instead? – eftshift0 May 03 '17 at 21:22
  • @Edmundo that would work if the config files were not already versioned, which they are. Basically the master has a set of config files for production, but locally I make changes to them because my development environment is a little different. – John Henckel May 03 '17 at 21:26
  • When a file is already versioned and I want to "ignore" it, I've always used ```git update-index --assume-unchanged``` on said file and I've never hit problems when switching (understanding that I actually do not care if the files are modified in my local so that git can do as it pleases with them if I check out anything). – eftshift0 May 03 '17 at 21:33
  • 2
    The easiest way, by far, is to *not commit configuration files* (at least not in the corresponding source—it's fine to have them committed in a separate repository meant just for tracking "my config files" or "my foo-system configuration"). Anything else leads to the kind of head- and/or heart- and/or stomach-ache you are experiencing now. That said, consider using `git worktree add` to work on another branch in a separate work-tree with a separate index; this may suffice and avoid all the aches. – torek May 03 '17 at 21:44
  • @Edmundo, i've tried `assume-unchanged` and it also prevents me from changing branches. Perhaps it worked for you because you did not actually change the files? But I need to change the config files. The checkout fails because it notices the config were changed, even though I tell it they are assume-unchanged. – John Henckel May 03 '17 at 21:57
  • @Edmundo `skip-worktree` is the better solution, see https://stackoverflow.com/a/13631525/1615903 – 1615903 May 04 '17 at 05:40
  • @JohnHenckel this happens because the config files have different contents in the branch you are switching to - what would you expect git to do in this situation? – 1615903 May 04 '17 at 05:41
  • @1615903 The purpose of skip-worktree is so that developers can make any file exempt from git. Any git command that might alter the file should simply skip over it. If I do a checkout, then git should skip those files. In other words, git checkout should succeed, but not make any changes to the skipped files. This is how skip-worktree should have been implemented. What do you think it the purpose of skip-worktree?The current implementation seems useless to me. – John Henckel May 08 '17 at 19:53
  • I was commenting on Edmundo's suggestion to use `assume-unchanged` – 1615903 May 09 '17 at 03:54

1 Answers1

0

I'm using git 1.9.1 and it is possible to switch branches with changes in a file that's already in the tree and then skipped with git update-index --skip-worktree filename.

The file is not changed after the switch (i.e. it is not changed to the version in the branch I switched to).

(I would have thought that this is the behavior since 1.7.7 since its release notes state * "git stash" learned an "--include-untracked option". but 1.7.7 was released before you asked the question, so I don't know.)

Please note that when trying to switch to a branch that doesn't have the file git still complains with

error: Your local changes to the following files would be overwritten by checkout: f1.config

tymtam
  • 31,798
  • 8
  • 86
  • 126