如果工作目录不干净(如何忽略冲突检查),如何强制Git中止签出? [英] How to force Git to abort a checkout if working directory is not clean (i.e. disregarding the check for conflicts)?
问题描述
当您执行 git checkout some_branch
,并且您的工作目录不干净时,Git将检查该签出是否会导致任何冲突,如果这样会导致以下冲突:
When you do a git checkout some_branch
, and your working directory is not clean, then Git will check if the checkout will result in any conflicts, and if so it will abort with:
$ git checkout some_branch
error: Your local changes to the following files would be overwritten by checkout:
some_file
Please, commit your changes or stash them before you can switch branches.
Aborting
但是,如果结帐不会导致任何冲突,则Git将切换到该新分支并继承所有未提交的更改(并列出它们,保持Git的礼貌).
However, if the checkout will not result in any conflicts, then Git will switch to that new branch and carry over all uncommitted changes (and listing them, polite as Git is).
$ git checkout some_branch
M some_file
M some_other_file
Switched to branch 'some_branch'
到目前为止一切都很好...
So far so good ...
现在,并不是所有的Git用户都使用cmd行.如果您使用IDE(例如Eclipse)并使用肮脏的工作目录签出 some_branch
,这不会导致任何冲突,那么您将无法得到有关正在处理的更改的很好通知.更改为 some_branch
后, previous_branch
中的仍然存在于您的工作目录中.
Now, not all Git users uses cmd line. If you use an IDE (like e.g. Eclipse) and do a checkout of some_branch
with a dirty working directory which will not result in any conflicts, then you will not be nicely notified that the changes you were working on in previous_branch
are still present in your working directory after changing to some_branch
.
即当您编译代码时,来自 previous_branch
的未提交的更改仍会出现在 some_branch
上的工作目录中.
I.e. when you compile your code your uncommitted changes from previous_branch
are still present in your working directory on some_branch
.
问题1:
如果工作目录不干净(是否存在任何冲突),是否可以强制Git中止签出?
Is it possible to force Git to abort a checkout if working directory is not clean (no matter if there are any conflicts or not)?
我希望在全局Git配置中进行设置.
I would prefer a setting in global Git config.
问题2:
如果这无法设置,那么它是否是对Git中新配置选项的有效请求?
If this is not possible to setup, is it then a valid request for a new config option in Git?
如我所见,Git在上下文切换方面非常强大(即在多个分支上处理多个问题),并且我可以看到一种设置的用途,该设置可以严格检查工作目录是否不干净(忽略检查)对于冲突).
As I see it Git is very strong in context switching (i.e. working on multiple issues on multiple branches), and I could see a use for a setting which setup a strict check for if working directory is not clean (disregarding the check for conflicts).
这意味着您在分支#1上创建的更改将不会在更改为分支#2时被继承,即您将始终严格根据上下文进行操作.
This would mean that your changes created while on branch #1 will not be carried over when changing to branch #2, i.e. you will work strictly context based always.
对此有任何意见或看法吗?
Any views or opinions on this?
推荐答案
我不知道这样的选项,但是下面是一个模拟此选项的脚本行为.也许可以将IDE(或OS)配置为运行此脚本 git-checkout
.
I'm not aware of such an option, but here's a script to simulate this
behavior. Maybe IDE(or OS) can be configured to run this script instead
of git-checkout
.
#!/bin/sh
MODIFIED=$(git status -s | grep ^\ M)
if [ "$MODIFIED" != "" ]; then
echo "Current branch is dirty!"
exit 1
else
git checkout $1
fi
您可以将此脚本命名为 git-cleancheckout
,放入$ PATH并运行像这样
You can name this script git-cleancheckout
, put to your $PATH and run
like this
git cleancheckout mybranch
这篇关于如果工作目录不干净(如何忽略冲突检查),如何强制Git中止签出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!