进行“git pull”拉不同分支时要求确认 [英] Make "git pull" ask for confirmation when pulling different branch

查看:1251
本文介绍了进行“git pull”拉不同分支时要求确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当同时与很多项目和分支机构合作时,我偶尔会犯一些愚蠢的错误,比如拉错分支。例如,在分支上 master 我做了 git pull origin危险代码,并且没有注意到这段时间。这个小小的错误导致了很多混乱。



有没有办法让git在我试图拉出除当前被检出的分支以外的分支时要求确认?基本上我希望它要求确认,如果分支名称不匹配(检出和被拉)。

解决方案

现在,我将重点介绍如何在执行 之前提示用户进行确认。



不幸的是,因为有没有预拉钩子这样的事情,我不认为你可以得到实际命令直接为你做。正如我所看到的,您有两种选择:

1 - 使用 fetch 然后合并(而不是 pull



代替运行 git拉,运行 git fetch ,然后 git merge git变基;将拉分解为两个步骤,它会强制您仔细检查将要合并/重新分配的内容。



2 - 定义一个要求在拉动前确认的别名

定义并使用 wrapper(作为一个Git别名),如果你试图从一个名字不同于当前本地分支的远程分支中提示你确认。



写在〜/ bin中一个名为 git-cpull.sh (用于确认,然后拉)的脚本文件的以下行/


$ b

 #!/ bin / sh 

#git-cpull.sh
$ b $ if if [$ 2!=$(git symbolic-ref --short HEAD)]
then
while true ;做
读-p你确定这次拉? yn
case $ y $ in
[Yy] *)
git pull $ @;
break
;;
[Nn] *)
退出
;;
*)
printf%s \\请回答是或否。
esac
done
else
git pull $ @
fi

然后定义别名:

git config --global alias。 cpull'!sh git-cpull.sh'

之后,例如,如果运行

  git cpull origin master 

,但当前分支不是 master ,系统会要求您确认实际上是执行的。



示例



  $ git branch 
* master
$ git cpull origin foobar
您确定这次拉?n
$ git cpull origin master
从https://github.com/git/git
* branch master - > FETCH_HEAD
已经是最新的。


When working with many projects and branches at the same time, I occasionally do stupid mistake like pulling into the wrong branch. For example being on branch master I did git pull origin dangerous_code and didn't notice that for quite some time. This small mistake caused a lot of mess.

Is there any way to make git ask for confirmation when I attempt to pull a branch other than branch that is currently checked out? Basically I want it to ask for confirmation if the branch name doesn't match (checked out and the one being pulled).

解决方案

For now, I'll focus on how to prompt the user for confirmation before any pull is carried out.

Unfortunately, because there is no such thing as a pre-pull hook, I don't think you can get the actual pull command to directly do that for you. As I see it, you have two options:

1 - Use fetch then merge (instead of pull)

Instead of running git pull, run git fetch, then git merge or git rebase; breaking down pull into the two steps it naturally consists of will force you to double-check what you're about to merge/rebase into what.

2 - Define an alias that asks for confirmation before a pull

Define and use a pull wrapper (as a Git alias) that prompts you for confirmation if you attempt to pull from a remote branch whose name is different from the current local branch.

Write the following lines to a script file called git-cpull.sh (for confirm, then pull) in ~/bin/:

#!/bin/sh

# git-cpull.sh

if [ "$2" != "$(git symbolic-ref --short HEAD)" ]
then
    while true; do
        read -p "Are you sure about this pull?" yn
        case "$yn" in
            [Yy]*)
                git pull $@;
                break
                ;;
            [Nn]*)
                exit
                ;;
            *)
                printf %s\\n "Please answer yes or no."
        esac
    done
else
    git pull $@
fi

Then define the alias:

git config --global alias.cpull '!sh git-cpull.sh'

After that, if, for example, you run

git cpull origin master

but the current branch is not master, you'll be asked for confirmation before any pull is actually carried out.

Example

$ git branch
* master
$ git cpull origin foobar
Are you sure about this pull?n
$ git cpull origin master
From https://github.com/git/git
 * branch            master     -> FETCH_HEAD
Already up-to-date.

这篇关于进行“git pull”拉不同分支时要求确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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