我该如何避免来自本地分支的意外dcommit [英] How can I avoid an accidental dcommit from a local branch

查看:132
本文介绍了我该如何避免来自本地分支的意外dcommit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,我在git中创建了本地分支,并且当我尝试从它们提交dcommit时,我想要收到一条警告消息。

如何防止自己如果你使用的是Linux(或者Git),那么你可以使用一个预先提交钩子的方法来解决这个问题。

bash或Cygwin或类似的),就是在shell帮助函数中包装 git 。将以下内容添加到〜/ .bashrc (用于bash,Git bash)或〜/ .zshrc (用于zsh )文件,或者任何相当于你的shell的:

$ $ $ $ p $ $ $ $ $ $ $($ git)
函数git {
if [[($ 1 == svn)&& ($ 2 == dcommit)]]
然后
curr_branch = $($ real_git分支| sed -n's / \ * // p')
if [[($ curr_branch! =主)&& ($ curr_branch!='(no branch)')]]
then
echo从$ curr_branch提交;你确定吗?[y / N]
读取resp
如果[[($ resp!= y)&& ($ resp!= Y)]]
然后
返回2
fi
fi
fi
$ real_git$ @
}

(我在Red Hat上用bash和zsh测试了这个,在Cygwin上测试了这个) p>

每当你调用 git ,你现在都会调用这个函数而不是普通的二进制。该函数将正常运行git,除非您在连接到不是master的分支时调用 git svn dcommit 。在这种情况下,它会在提交之前提示您确认。你可以通过明确地指定 git 的路径覆盖这个函数(这就是 $ real_git 正在做的事情)请记住,在更新〜/ .bashrc 或等价物之后,您需要重新加载它,或者通过启动一个新的shell会话(注销并重新登录)或运行 source〜/ .bashrc



编辑:作为增强功能,您可以删除第一行,从 real_git = 开始,并替换 $ real_git 命令git ,它们以优选的方式实现了相同的功能。我还没有更新脚本本身,因为我无法在zsh上测试更改。


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?

解决方案

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:

real_git=$(which git)
function git {
    if [[ ($1 == svn) && ($2 == dcommit) ]]
    then
        curr_branch=$($real_git branch | sed -n 's/\* //p')
        if [[ ($curr_branch != master) && ($curr_branch != '(no branch)') ]]
        then
            echo "Committing from $curr_branch; are you sure? [y/N]"
            read resp
            if [[ ($resp != y) && ($resp != Y) ]]
            then
                return 2
            fi
        fi
    fi
    $real_git "$@"
}

(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 calling git 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 to git 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 running source ~/.bashrc.

Edit: As an enhancement, you can remove the first line, starting real_git=, and replace the other instances of $real_git with command 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.

这篇关于我该如何避免来自本地分支的意外dcommit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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