如何让 git 在签出不同的分支之前总是警告存储或提交更改?

How to make git always warn to stash or commit changes before checking out a different branch?

当我修改当前分支并签出到另一个分支时,git 警告我我没有隐藏或提交失败 仅当两个分支之间存在冲突时.

如何让 git 在结帐前始终警告我文件尚未提交?

作为 ,您将需要编写自己的小 mini-program(例如,几行 shell 脚本或别名)来执行此操作。 Git 的“允许修改文件切换”被认为是一个 特性 ,而不是一个错误,它特别允许在任何时候创建一个新的分支名称,以防你已经开始工作并决定这应该成为它自己的一个分支。

git-core 库包含一个名为 git-sh-setupsource-able shell 文件,它提供了一个名为 require_clean_work_tree 的函数,因此您的备用程序(您将使用 而不是 git checkoutgit switch) 可能以:

开头
#! /bin/sh
. git-sh-setup

# adjust my-checkout name as desired
require_clean_work_tree my-checkout "stash or commit your changes before using my-checkout"

但是请注意,成功 git checkoutgit switch 在索引 and/or 中进行修改tree 产生 git status 风格的输出显示携带的修改。您可以简单地 git checkout -git switch - 到 return 到您刚才所在的原始分支名称(或分离的 HEAD)。在一些罕见的极端情况下,您会在此过程中丢失一些信息,但在实践中您不太可能 运行 进入这些情况。有关详细信息,请参阅 Checkout another branch when there are uncommitted changes on the current branch