Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pull hook script which executes locally

Tags:

git

I want to prompt for the confirmation [yes/no] for every the commands git push and pull which contacts the git server. Is there a way to include any commands/scripts to make this happen? so User can able to given the input, after that only git proceeds for that operation.

like image 660
user1482764 Avatar asked May 09 '26 02:05

user1482764


2 Answers

First of all, I don't think that you will make much friends with that change. If someone types in git pull, he already proved that he's willing to contact the remote server. In my opinion, there's no need to annoy the user with confirm dialogs in that case.

Anyway, if you want to confirm every pull and push, hooks are not the right tool since the pull/push hooks only exist on the server side. There are no pre-pull, post-pull, pre-push or post-push hooks on the client side. So, we have to discard the hook idea.

You could alias the pull and push commands instead to surround them with the query whether there should really be pulled/pushed or not.

The query is simple:

echo -n "really push? [y/n] "
read -s -n 1 really
if [[ $really == "y" ]]; then 
  # PULL/PUSH HERE
else 
  echo "aborting"
fi

Unfortunately, you can't use the same name for an alias. So, the easy thought of

[alias]
    push = "!echo -n \"really push? [y/n]\";read -s -n 1 really;if [[ $really == \"y\" ]];then git push; else echo \"aborting\"; fi"

doesn't work.

But you have two other options to achieve what you want:

  • use git bord means and call the alias safepush/safepull or something like that
  • set up an alias in your .bashrc for git pull and git push (see this question on SU for setting up multiword aliases which are actually functions)
like image 195
eckes Avatar answered May 12 '26 11:05

eckes


I believe you're looking for Git hooks.

You will have to put the hooks there yourself though. I don't think you can make the user pull the (client-side) hooks along with the rest of the code.

like image 38
Frost Avatar answered May 12 '26 09:05

Frost