如何避免本地分支的意外 dcommit
有时,我在 git 中创建本地分支,当我尝试从它们进行 dcommit 时,我希望收到警告消息。
如何防止自己意外地从本地分支进行提交?
Sometimes, I create local branches in git, and I'd like to get a warning message when I try to dcommit from them.
How can I prevent myself from accidentally dcommiting from a local branch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 Linux(或 Git bash 或 Cygwin 或类似的),预提交挂钩的替代方法是将
git
包装在 shell 辅助函数中。将以下内容添加到您的~/.bashrc
(对于 bash、Git bash)或~/.zshrc
(对于 zsh)文件,或者适用于您的 shell 的任何等效文件:(我已经在 Red Hat 上使用 bash 和 zsh 以及 Cygwin 上的 bash 对此进行了测试)
每当您调用 git 时,您现在将调用此函数而不是普通的二进制文件。该函数将正常运行 git,除非您在附加到非 master 分支时调用 git svn dcommit 。在这种情况下,它会提示您在提交之前进行确认。您可以通过显式指定
git
的路径来覆盖该函数(这就是$real_git
正在做的事情)。请记住,更新
~/.bashrc
或等效文件后,您需要通过启动新的 shell 会话(注销并再次登录)或运行source ~/ 来重新加载它。 .bashrc
。编辑:作为一项增强功能,您可以删除以
real_git=
开头的第一行,并将$real_git
的其他实例替换为命令 git
,它以首选方式实现相同的功能。我没有更新脚本本身,因为我无法在 zsh 上测试更改。An alternative to pre-commit hooks, if you're using Linux (or Git bash or Cygwin or similar), is to wrap
git
in a shell helper function. Add the below to your~/.bashrc
(for bash, Git bash) or~/.zshrc
(for zsh) file, or whatever the equivalent is for your shell:(I've tested this with bash and zsh on Red Hat, and bash on Cygwin)
Whenever you call
git
, you'll now be calling this function rather than the normal binary. The function will run git normally, unless you're callinggit svn dcommit
while attached to a branch that's not master. In that case, it'll prompt you to confirm before doing the commit. You can override the function by specifying the path togit
explicitly (that's what the$real_git
is doing).Remember that after updating
~/.bashrc
or equivalent, you'll need to reload it, either by starting a new shell session (logging out and logging in again) or by runningsource ~/.bashrc
.Edit: As an enhancement, you can remove the first line, starting
real_git=
, and replace the other instances of$real_git
withcommand git
, which achieves the same thing but in the preferred way. I've not updated the script itself as I've not been able to test the change on zsh.首先想到的是使用 git pre-commit hook 来解决问题。这对于纯 git 存储库来说很容易:
但正如 Hooks for git-svn 中所讨论的,这并没有完全发挥作用。 VonC 提出了一个(已接受)answer 他使用了一个中间裸存储库,其作用类似于 git 和 SVN 之间的代理。
也许这也可以帮助你。
First thing that comes in mind is using a git pre-commit hook to solve the problem. This would be easy for pure git repos:
But as discussed in Hooks for git-svn, this isn't fully working. VonC came up with an (accepted) answer where he utilizes an intermediate bare repo that acts like kind of a proxy between git ans SVN.
Maybe this could help you too.
如果其他人需要 Windows Powershell:
In case anyone else needs this for Windows Powershell: