git how to switch branches without committing your changes

一个人想着一个人 提交于 2019-12-01 21:19:48

问题


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:


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



来源:https://stackoverflow.com/questions/43770228/git-how-to-switch-branches-without-committing-your-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!