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.
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
forgit pull
andgit push
(see this question on SU for setting up multiword aliases which are actually functions)
-
These solutions seems to be not working. The script i have is working fine in pre-commit hook script. Can't we make this similar kind of setup like pre-pull and pre-push. – user1482764 Jun 27 '12 at 14:04
-
$ cat .git/hooks/pre-commit # Allows us to read user input below, assigns stdin to keyboard exec < /dev/tty while true; do read -p "[post-commit hook] Check for outdated gems? (Y/n) " yn if [ "$yn" = "" ]; then yn='Y' fi case $yn in [Yy] ) echo "You entered yes"; break;; [Nn] ) exit;; * ) echo "Please answer y or n for yes or no.";; esac done KH837@KH837 /d/gitwork/20120626/myplugin (Dev-3.0)$ git commit -m "test" test enter ur choice [post-commit hook] Commit done! [post-commit hook] Check for outdated gems? (Y/n) y You entered yes nothing to commit – user1482764 Jun 27 '12 at 14:09
-
@user1482764: no, we can't make a similar kind of setup since there simply are no `pre-pull` and `pre-push` hooks. Period. – eckes Jun 27 '12 at 15:47
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.

- 11,121
- 3
- 37
- 44
-
I already went through that link. Here what my requirement is i have the script which prompt the user to give his input as yes/no. Now i need to place this script in git hook scripts which gets executes when i try to use git pull (from one hook script) and git push (another git hook script) so that they will ask for my inputs. and i found that the hooks are different for client and server. If you add different echo statements in update script in your working locations and in server side update script it will only display the server side but not your local echo. – user1482764 Jun 26 '12 at 13:20