git如何在不提交更改的情况下切换分支 [英] git how to switch branches without committing your changes

查看:513
本文介绍了git如何在不提交更改的情况下切换分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在branchX中工作,它有十二个我不想提交的配置文件。所以我将所有配置文件都标记为--skip-worktree。现在,我想更改为branchY。我该怎么做?

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?

我尝试过

git checkout branchY

它说

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

所以我试图用

git stash save

但它说

No local changes to save

这很 很烦 。显然唯一的解决方案是

This is very annoying. Apparently the only solution is


  1. 使用 git ls-files -v 每个文件的所有跳过工作树文件的列表

  2. ,请删除每个跳过工作树

  3. git stash save
  4. code>
  5. git结帐分支Y

  6. git stash pop

  7. 手动解决与的任何冲突-他们的

  8. 对于每个文件,再次添加skip-worktree标志

  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

有没有更简单的方法?

推荐答案

我正在使用 git 1.9.1 ,并且可以通过更改文件来切换分支那已经在树中,然后用 git update-index --skip-worktree filename 跳过。

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).

(我应该以为这是自1.7.7版本以来的行为,因为其发行说明指出了 * git stash --include-untracked选项。,但在您问问题之前发布了1.7.7,所以我不知道。)

(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.)

请注意,当尝试切换到没有文件的分支时,git仍然会抱怨

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


错误:您对以下文件的本地更改将被结帐覆盖:
f1.config

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

这篇关于git如何在不提交更改的情况下切换分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆