git 在git中提交后如何自动推送?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7925850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:11:10  来源:igfitidea点击:

How to automatically push after committing in git?

gitgithooksgit-pushpost-commit-hook

提问by ulu

How do I set git to automatically push to a remote repo (including automatically providing my passphrase) after each commit to the local repo?

每次提交到本地存储库后,如何将 git 设置为自动推送到远程存储库(包括自动提供我的密码)?

回答by Mark Longair

First, make sure that you can push manually without providing your password. If you are pushing over HTTP or HTTPS, that will be a case of either creating a .netrcfile with the login detailsor adding your username and password into the URL for the remote. If you're using SSH, you can either create a keypair where the private key doesn't have a password, or use ssh-agentto cache your private key.

首先,确保您可以在不提供密码的情况下手动推送。如果您通过 HTTP 或 HTTPS 推送,则需要创建一个.netrc包含登录详细信息的文件将您的用户名和密码添加到远程. 如果您使用 SSH,您可以创建一个密钥对,其中私钥没有密码,或者用于ssh-agent缓存您的私钥

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

然后,您应该在其中创建一个可执行 ( chmod +x) 文件,.git/hooks/post-commit其中包含以下内容:

#!/bin/sh
git push origin master

... customizing that line if you want to push to a remote other than origin, or push a branch other than master. Make sure that you make that file executable.

...如果要推送到 . 以外的远程origin,或推送master. 确保您使该文件可执行。

回答by i4h

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

如果您开始使用的不仅仅是 master 分支,您可能希望自动推送当前分支。我的钩子 ( .git/hooks/post-commit) 看起来像这样:

#!/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 with git symbolic-ref.

它推送当前分支,如果它可以用 git symbols-ref 确定分支名称。

"How to get current branch name in Git?" deals with this and other ways to get the current branch name.

如何在 Git 中获取当前分支名称?”处理这种和其他获取当前分支名称的方法。

An automatic push for every branch can be disturbing when working in task branches where you expect some sausage makingto happen (you won't be able to rebase easily after pushing). So the hook will not push branches that end with a defined suffix (in the example "_local").

在任务分支中工作时,每个分支的自动推送可能会令人不安,因为在这些分支中会发生一些香肠制作(推送后您将无法轻松变基)。因此钩子不会推送以定义的后缀结尾的分支(在示例“_local”中)。

回答by Colin R

Create a file named "post-commit" in the .git/hooks directory with the contents "git push", though if you want to automatically provide a password, so modification will be needed.

在.git/hooks 目录下创建一个名为“post-commit”的文件,内容为“git push”,不过如果你想自动提供密码,那么就需要修改。

回答by VonC

This git-autopushscript allows you to setup a post-commit hook, similar to what has been recommended in "How configure automatic pushing?".
But for the passphrase, you need to run a ssh-agent.

这个git-autopush脚本允许你设置一个 post-commit 钩子,类似于“如何配置自动推送?”中的推荐。
但是对于密码,您需要运行一个ssh-agent.

回答by Rafa? Gnitecki

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

这是使用 Linux 和 Windows (git bash) 的人无需通过 ssh 提供密码即可推送/拉取的简单说明

On your client:

在您的客户端上:

  1. Check out if you have ssh keys 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
    
  2. If you don't have any key (two "ls: cannot access ..." lines) generate a new one. If you have any of the 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
    
  3. Copy your key to remote server from which you want to pull or push 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's 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.
    
  1. 检查您是否生成了 ssh 密钥:

    $ 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
    
  2. 如果您没有任何密钥(两个“ls:无法访问...”行),请生成一个新密钥。如果您有任何密钥,请跳过此步骤。

    $ 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
    
  3. 使用 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's 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: You will have to provide password during this operation. After that your pull/push operations won't request password.

注意:您必须在此操作期间提供密码。之后,您的拉/推操作将不会请求密码。

Note2: You have to log to the server using user_name at least once before using this procedure (home directory to which ssh keys are copied is created during first login)

注意2:在使用此过程之前,您必须至少使用 user_name 登录到服务器一次(第一次登录时会创建复制 ssh 密钥的主目录)

回答by Chetabahana

Here is bash script for git to automatically pushto a remote repo

这是 git 自动push到远程仓库的bash 脚本

  1. Automatically check ssh-agent
  2. Automatically send passphrase using expect script
  3. Usage is simply: $ cd /path/to/your/repositorythen $ push
  1. 自动检查ssh-agent
  2. 使用expect脚本自动发送密码
  3. 用法很简单:$ cd /path/to/your/repository然后$ push

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

将此脚本放入文件例如 $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)
[email protected]:${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 /bindirectory so it can be called by just $ pushcommand

将其链接到/bin目录,以便可以通过$ push命令调用它

$ sudo ln -s $HOME/.ssh/push /bin/push
$ chmod +x /bin/push