How to automatically push after committing in git?
在每次提交本地回购后,如何设置git自动推送到远程回购(包括自动提供密码)?
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.
Then you should create a file in .git/hooks/post-commit
that contains the following:
#!/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.
If you start using more than the master branch, you might want to automatically push the current branch. My hook 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 = 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_mobiles 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.
"How to get current branch name in Git?" deals with this and other ways to get the current branch name.
An automatic push for every branch can be disturbing when working in task branches where you expect some sausage making to 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").
在.git / hooks目录中创建一个名为“post-commit”的文件,内容为“git push”,但如果你想自动提供密码,那么需要进行修改。
链接地址: http://www.djcxy.com/p/19498.html上一篇: 获取git当前分支/标签名称
下一篇: 如何在git中提交后自动推送?