How to automatically click after commit in git?

How to install git to automatically go to a remote repo (including automatically providing my passphrase) after each commit of a local repo?

+69
git git-push post-commit-hook githooks
Oct 28 '11 at 6:25
source share
7 answers

First, make sure you can click manually without providing a password. If you are promoting through HTTP or HTTPS, this will be the case of creating a .netrc with login information or adding your username and password to the remote URL . If you use SSH, you can create a key pair in which the private key does not have a password, or use ssh-agent to cache your private key .

Then you must create an executable file ( chmod +x ) in .git/hooks/post-commit which contains the following:

 #!/bin/sh git push origin master 

... setting up this line if you want to transfer to a remote device other than origin , or to transfer a branch other than master . Make sure you make this file executable.

+128
Oct 28 '11 at 6:32
source share

If you start using more than the main branch, you can automatically push the current branch. My hook ( .git/hooks/post-commit ) looks like this:

 #!/usr/bin/env bash branch_name=$(git symbolic-ref --short HEAD') retcode=$? non_push_suffix="_local" # Only push if branch_name was found (my be empty if in detached head state) if [ $retcode -eq 0 ] ; then #Only push if branch_name does not end with the non-push suffix if [[ $branch_name != *$non_push_suffix ]] ; then echo echo "**** Pushing current branch $branch_name to origin [i4h post-commit hook]" echo git push origin $branch_name; fi fi 

It pushes the current branch if it can determine the branch name using git symbolic-ref.

β€œ How do I get the current branch name in Git? ” Deals with this and other ways of getting the current branch name.

An automatic push for each branch can be troubling when working in task branches where you expect some sort of sausage preparation to happen (you cannot easily rebase after the push). Thus, the trap will not push forward branches ending with a specific suffix (in the example "_local").

+28
Jan 20 '15 at 10:24
source share

Create a file called "post-commit" in the .git / hooks directory with the contents of "git push", although if you want to automatically provide a password, you will need to modify it.

+8
Oct 28 '11 at 6:32
source share

This git-autopush script allows you to configure the hook after commit, similar to what was recommended in " How to configure automatic pressing? ".
But for a passphrase you need to run ssh-agent .

+3
Oct 28 '11 at 6:32
source share

Here is a simple instruction for push / pulling without providing passphrase over ssh for people using Linux and Windows (git bash)

On your client:

  • Check if ssh keys are generated:

     $ ls ~/.ssh/id_rsa.pub; ls ~/.ssh/id_dsa.pub /c/Users/Cermo/.ssh/id_rsa.pub <-- I have RSA key ls: cannot access '/c/Users/Cermo/.ssh/id_dsa.pub': No such file or directory 
  • If you do not have a key (two lines of "ls: cannot access ..."), create a new one. If you have any keys, skip this step.

     $ ssh-keygen.exe Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/Cermo/.ssh/id_rsa): Enter passphrase (empty for no passphrase): <-- press Enter Enter same passphrase again: <-- press Enter 
  • Copy your key to the remote server from which you want to pull or click using git:

     $ ssh-copy-id user_name@server_name /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys user_name@server_name password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'user_name@server_name'" and check to make sure that only the key(s) you wanted were added. 

Note. During this operation, you will need to provide a password. After that, your pull / push operations will not ask for a password.

Note 2. Before using this procedure, you need to log into the server using the username at least once before using this procedure (the home directory to which ssh keys are copied is created during the first login)

0
Aug 24 '16 at 20:42 on
source share

Here is the bash script for git for auto push remote repo

  1. Automatically check ssh-agent
  2. Automatically send passphrase using expected script
  3. Usage is simple: $ cd /path/to/your/repository then $ push

Put this script in a file, for example $HOME/.ssh/push

 #!/bin/bash # Check connection ssh-add -l &>/dev/null [[ "$?" == 2 ]] && eval 'ssh-agent' > /dev/null # Check if git config is configured if [ ! $(git config user.name) ] then git config --global user.name <user_name> git config --global user.email <user_email> fi # Check if expect is installed if [[ ! $(dpkg -l | grep expect) ]] then apt-get update > /dev/null apt-get install --assume-yes --no-install-recommends apt-utils expect > /dev/null fi # Check identity ssh-add -l &>/dev/null [[ "$?" == 1 ]] && expect $HOME/.ssh/agent > /dev/null # Clean and push repo REMOTE=$(git remote get-url origin) URL=git@github.com:${REMOTE##*github.com/} [[ $REMOTE == "http"* ]] && git remote set-url origin $URL git add . && git commit -m "test automatically push to a remote repo" git status && git push origin $(git rev-parse --abbrev-ref HEAD) --force 

Link it to the /bin so that it can be called simply with the $ push command

 $ sudo ln -s $HOME/.ssh/push /bin/push $ chmod +x /bin/push 
0
Aug 05 '19 at 21:02
source share

In addition, I developed the " live-git-push " package, which will be used in courses and the like, to synchronize real-time online repos with local file changes, commit and send each file when saving.

-one
Dec 17 '18 at 10:04
source share



All Articles