git how to switch branches without committing your

2019-02-23 12:50发布

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?

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-23 13:17

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

查看更多
登录 后发表回答