Make "git pull" request confirmation when pulling another branch

While working with many projects and branches at the same time, I sometimes make a stupid mistake, like pulling into the wrong branch. For example, while on the master branch, I did git pull origin dangerous_code and did not notice this for quite some time. This little mistake caused a big mess.

Is there a way to make git request confirmation when I try to pull a branch other than the one that is currently logged out? Basically, I want it to ask for confirmation if the branch name does not match (crossed out and pulled out).

+6
source share
1 answer

Currently, I will focus on how to request user confirmation before any click is performed.

Unfortunately, because there is no such thing as a pre-pull hook , I don’t think you can get the actual pull to do this right for you. As I can see, 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 the pull into two steps, of which it naturally consists, will force you to double-check that you are going to combine / remake into what.

2 - Define an alias that asks for confirmation before clicking

Define and use a pull wrapper (as an alias of Git) that asks for confirmation if you try to retrieve 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 (to confirm, then press) 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 an 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 will be asked to confirm before any pull is executed.

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. 
+3
source

Source: https://habr.com/ru/post/974536/


All Articles