如何设置Git自动将本地库的每个提交(包括自动提供我的passphrase)推送到远程仓库?
如何设置Git自动将本地库的每个提交(包括自动提供我的passphrase)推送到远程仓库?
首先,确保您可以在不提供密码的情况下手动推送。 如果您正在通过HTTP或HTTPS进行推送,则需要创建一个包含登录详细信息的.netrc
文件或者在远程URL中添加用户名和密码。如果使用SSH,您可以创建私钥没有密码的密钥对,或者使用ssh-agent
缓存私钥。
接下来,您应该在 .git/hooks/post-commit
创建一个可执行文件(chmod +x
),其中包含以下内容:
#!/bin/sh
git push origin master
如果你想推送到除了origin
以外的远程仓库,或者推送除了master
分支以外的分支,那么你需要自定义该行。请确保该文件是可执行的。
.git/config
中带有前缀feature/xy/
的相应远程分支的分支。 - Asclepius.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
#!/bin/sh
才能让它正常工作。否则它一直显示:error: cannot run .git/hooks/post-commit: No such file or directory
。谢谢,我最喜欢你的解决方案。 - oyalhi#!/usr/bin/env bash
?它的含义是什么(它应该做什么)?这是为了使用 Bash 而不是 Dash (sh
) 吗?在哪个系统上?Ubuntu 16.04 (Xenial Xerus)?为什么它与默认的 #!/usr/sh
不同? - Peter Mortensenssh-agent
,只需使用另一个无密码的 git
专用 ssh
密钥:ssh-keygen -t ed25519 -f ~/.ssh/id_pushonly
。echo $'\nHost pushonly\nHostname DESTINATION\nIdentityFile ~/.ssh/id_pushonly\n' >> ~/.ssh/config
。在 DESTINATION
上使用来自 ~/.ssh/id_pushonly.pub
的公钥配置 git-shell
,如 https://superuser.com/a/444899/72223 所示。所需的 git
URL 类似于 git@pushonly:path/to/repo.git
。调试:ssh git@pushonly COMMAND
必须在 DESTINATION
上运行 git-shell -c COMMAND
。有关 COMMAND
请参阅 man git-shell
。 - Tino-t ed25519
?我通常使用 -t rsa
,尽管最近我不得不在 ssh-keygen
中添加 -m PEM
(https://dev59.com/dVQJ5IYBdhLWcg3wiWec#53645530,https://dev59.com/9bDla4cB1Zd3GeqP7mQC#53729009)。 - VonCed25519
,因为它可以在~/.ssh/authorized_keys
中提供简短而方便的行。此外,非常有趣的是DJB关于ed255519的写作:安全防范旁路攻击(Spectre),CPU占用率低等等。顺便说一下,当处理旧的sshd
时,我通常会为它们创建一个特殊的密钥,然后在~/.ssh/config
中进行配置。 - Tino这是一个用于Git自动push
到远程仓库的Bash脚本:
将此脚本添加到文件中,例如$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 the repository
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
/bin
目录,这样只需使用 push
命令即可调用它:sudo ln -s $HOME/.ssh/push /bin/push
chmod +x /bin/push
以下是针对使用Linux和Windows(Git Bash)的人们,无需提供SSH密钥的简单推送/拉取指令。
在您的客户端上:
检查是否已生成SSH密钥:
$ ls ~/.ssh/id_rsa.pub; ls ~/.ssh/id_dsa.pub
/c/Users/Cermo/.ssh/id_rsa.pub <-- 我有RSA密钥
ls: cannot access '/c/Users/Cermo/.ssh/id_dsa.pub': No such file or directory
如果没有任何密钥(两个“ls:cannot access ...”行),请生成一个新密钥。如果您已经拥有其中任何一个密钥,则跳过此步骤。
$ 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): <-- 按回车键
Enter same passphrase again: <-- 按回车键
将您的密钥复制到您想要使用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.
注意:在此操作期间,您需要提供一个密码。之后,您的拉/推操作将不会要求密码。
注意2:在使用此过程之前,您必须至少使用user_name登录到服务器一次(SSH密钥所复制的主目录在第一次登录时创建)。
创建一个Git文件:commit.sh
#!/bin/sh
cd c:/Users/Lenovo/Desktop/nalms/src
git add --all
timestamp() {
date +"at %H:%M:%S on %d/%m/%Y"
}
git commit -am "定期自动提交 $(timestamp)"
git push origin master
打开窗口任务计划程序
创建新任务
常规 → 命名任务
进入触发器部分并启用任务计划程序
按下 完成 按钮
如果您正在使用Husky,则默认情况下将覆盖post-commit
挂钩文件。
我们在package.json中使用此命令将任何提交自动合并到主分支并推送。 (首先运行yarn add --dev git-branch-is
。)
"husky": {
"hooks": {
"post-commit": "git-branch-is master && git rebase origin master && git push origin master"`
}
}