如何避免本地分支的意外 dcommit

发布于 2025-01-04 11:28:31 字数 83 浏览 1 评论 0原文

有时,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅一夜美梦 2025-01-11 11:28:31

如果您使用 Linux(或 Git bash 或 Cygwin 或类似的),预提交挂钩的替代方法是将 git 包装在 shell 辅助函数中。将以下内容添加到您的 ~/.bashrc (对于 bash、Git bash)或 ~/.zshrc (对于 zsh)文件,或者适用于您的 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 "$@"
}

(我已经在 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:

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.

樱&纷飞 2025-01-11 11:28:31

首先想到的是使用 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.

烟酒忠诚 2025-01-11 11:28:31

如果其他人需要 Windows Powershell:

    function CallGit
    { 
        if (($args[0] -eq "svn") -And ($args[1] -eq "dcommit")) {
            $curr_branch = &{git branch};
            $curr_branch = [regex]::Match($curr_branch, '\* (\w*)').captures.groups[1].value
            if ($curr_branch -ne "master") {
                Write-Warning "Committing from branch $curr_branch";
                $choice = ""
                while ($choice -notmatch "[y|n]"){
                    $choice = read-host "Do you want to continue? (Y/N)"
                }
                if ($choice -ne "y"){
                    return
                }
            }
        }
        &"git.exe" @args
    }
    Set-Alias -Name git -Value CallGit -Description "Avoid an accidental git svn dcommit on a local branch"

In case anyone else needs this for Windows Powershell:

    function CallGit
    { 
        if (($args[0] -eq "svn") -And ($args[1] -eq "dcommit")) {
            $curr_branch = &{git branch};
            $curr_branch = [regex]::Match($curr_branch, '\* (\w*)').captures.groups[1].value
            if ($curr_branch -ne "master") {
                Write-Warning "Committing from branch $curr_branch";
                $choice = ""
                while ($choice -notmatch "[y|n]"){
                    $choice = read-host "Do you want to continue? (Y/N)"
                }
                if ($choice -ne "y"){
                    return
                }
            }
        }
        &"git.exe" @args
    }
    Set-Alias -Name git -Value CallGit -Description "Avoid an accidental git svn dcommit on a local branch"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文