确认git推送命令

发布于 2024-11-26 15:55:54 字数 65 浏览 0 评论 0原文

如何在具体分支中配置 git Push 命令的一些确认?我有生产分支,有时我忘记了我在生产分支中并且不推送这些更改。

How I can configure some confirmation on git push command in concrete branch? I have production branch and sometimes I forgot that I in production branch and push not those changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

笛声青案梦长安 2024-12-03 15:55:54

一种可能的解决方案是删除与“生产”分支关联的配置。

git config --unset branch.<branch>.remote
git config --unset branch.<branch>.merge

这样,您必须提及您想要从哪个遥控器拉出(或推送到哪个遥控器)。
如果您为拉取定义了一个别名,其中:

  • 拉取
  • 然后删除该分支的远程和合并配置,那么

您确信您将无法在不输入其他参数的情况下对该分支执行简单的“git pull”。

One potential solution would be to remove the config associated with 'production' branch.

git config --unset branch.<branch>.remote
git config --unset branch.<branch>.merge

That way, you have to mention to what remote you want to pull from (or push to, for that matter).
If you define an alias for the pull which:

  • pull
  • and then remove the remote and merge config for that branch,

you are sure you will not be able, for that branch, to do a simple "git pull" without entering additional parameters.

明明#如月 2024-12-03 15:55:54

您所需要的只是用于提交到受保护分支(例如“master”)的预推送挂钩。

Git 为各种操作提供了许多钩子(如文档)。钩子是带有特殊输入的 shell 脚本,由 git 在特定时间调用,因此您可以根据需要调整它们,但是要在推送到 master 之前要求确认,您可以这样做:

protected_ref="refs/heads/master"
while read local_ref local_sha remote_ref remote_sha
do
        if [ "$remote_ref" = "$protected_ref" ]; then
        echo "Pushing to master, are you sure? (y/n)"
        read confirmation < /dev/tty
        if [ "$confirmation" != "y" ]; then
                echo >&2 "$confirmation is not 'y', cancelling push"
                exit 1
        fi
fi
done

exit 0

请注意,当要求用户输入时,您必须给出它一个要读取的终端,如问题 如何要求用户输入Git 挂钩?

PS:需要 GIT 版本 1.8.2 或更高版本才能使用钩子。

All you need is the pre-push hook for the commits into protected branches like "master".

Git offers a number of hooks for various actions (as written in the documention). The hooks are shell scripts with special inputs called at specific times by git so you can adapt them as you want, but to ask a confirmation before pushing to master you can do this:

protected_ref="refs/heads/master"
while read local_ref local_sha remote_ref remote_sha
do
        if [ "$remote_ref" = "$protected_ref" ]; then
        echo "Pushing to master, are you sure? (y/n)"
        read confirmation < /dev/tty
        if [ "$confirmation" != "y" ]; then
                echo >&2 "$confirmation is not 'y', cancelling push"
                exit 1
        fi
fi
done

exit 0

Note that when asking for user input, you have to give it a terminal to read from, as explained in the question How to ask for user input in a Git hook?.

PS: GIT Version 1.8.2 or above needed for hooks to be available.

你与清晨阳光 2024-12-03 15:55:54

您可以git fetch,然后git status会告诉您是否已推送新提交。

You can git fetch, then git status will tell you if new commits have been pushed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文