简体   繁体   English

如果未提交更改,请阻止'git checkout'

[英]Prevent 'git checkout' if uncommitted changes

I frequently have a bad time because I checkout a branch with uncommitted changes that I should have committed before changing branches but forgot. 我经常度过一段不愉快的时光,因为我在更换分支之前检查了一个我应该提交的未提交更改但仍然忘记的分支。 Is there a way using some git config I can have the git checkout fail if there are any pending changes or new files? 有没有办法使用一些git配置我可以让git checkout失败,如果有任何挂起的更改或新文件? I've recently gotten into the habit of running git status before git checkout , but that's inconvenient and still prone to forgetfulness. 我最近养成了在git checkout之前运行git status的习惯,但这git checkout ,但仍然容易遗忘。 I can probably use a Bash alias to chain those two commands together in some way, but I'm hoping there's either something I can throw in my .gitconfig or a flag that I'm not seeing in git config 's man page. 我可以使用Bash别名以某种方式将这两个命令链接在一起,但我希望我可以在我的.gitconfig一些东西或者在git config的手册页中没有看到的标志。

git checkout will not run if the checkout would cause any changes to get lost during its process. 如果结帐会导致任何更改在其过程中丢失,则git checkout将不会运行。 So even if you use git checkout with a dirty working directory, all your changes are carried over to the other branch. 因此,即使您将git checkout与脏工作目录一起使用,您的所有更改也会转移到另一个分支。 And you can also just switch back to the previous branch to get in the old state. 而且您也可以切换回上一个分支以进入旧状态。

If the branches are incompatible (for example a file you modified was deleted), git checkout should automatically fail, preventing you from losing any changes. 如果分支不兼容(例如,您修改的文件已被删除), git checkout应自动失败,防止您丢失任何更改。

YOu can use 您可以使用

git stash git stash

to save your uncomitted changes on a stack. 在堆栈上保存未经注册的更改。

git stash list (to see which stashed you have stored) git stash list(看看你存储了哪些存储)

git stash apply (to apply your changes stored in stash) You can even create branch from the stash. git stash apply(应用存储在存储中的更改)您甚至可以从存储创建分支。 Refer gitscm book for more information ( http://git-scm.com/book ) 有关更多信息,请参阅gitscm书籍( http://git-scm.com/book

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM