在 Git 中提交后如何自动推送? [英] How can I automatically push after committing in Git?
问题描述
如何设置 Git 自动推送到远程每次提交到本地存储库后,存储库(包括自动提供我的密码)?
How do I set Git to automatically push to a remote repository (including automatically providing my passphrase) after each commit to the local repository?
推荐答案
首先,确保您可以在不提供密码的情况下手动推送.如果您通过 HTTP 或 HTTPS 推送,那将是 使用登录详细信息创建 .netrc
文件 或 将您的用户名和密码添加到远程 URL 中.如果您使用 SSH,您可以创建一个私钥没有密码的密钥对,或者 使用 ssh-agent
缓存您的私钥.
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 .netrc
file with the login details or 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-agent
to cache your private key.
然后你应该在 .git/hooks/post-commit
中创建一个包含以下内容的可执行文件 (chmod +x
):
Then you should create an executable (chmod +x
) file in .git/hooks/post-commit
that contains the following:
#!/bin/sh
git push origin master
... 如果您想推送到 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.
这篇关于在 Git 中提交后如何自动推送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!