How can I automatically push after committing in Git?
Solution 1
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 an executable (chmod +x
) 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.
Solution 2
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:
#!/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.
"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").
Solution 3
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, a modification will be needed.
Solution 4
This git-autopush script 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
.
ulu
Developer on the .Net platform, currently building Chpokk, a .Net IDE (http://chpokk.apphb.com/).
Updated on December 06, 2021Comments
-
ulu over 2 years
How do I set Git to automatically push to a remote repository (including automatically providing my passphrase) after each commit to the local repository?
-
ulu over 12 yearsCouldn't make ssh-agent remember my passphrase, so had to make it empty. Hope my wife won't hack my account :)
-
Asclepius about 10 yearsSpeaking of customization, what if I want to push some but not all branches in this manner? For example, I want to auto-push only the branches that have a corresponding remote branch noted in
.git/config
with the prefixfeature/xy/
. -
Yada almost 9 yearsgit push --all origin
-
oyalhi almost 8 yearsFor the first line, i had to use
#!/bin/sh
to make it work. Otherwise it kept saying:error: cannot run .git/hooks/post-commit: No such file or directory
. Thank you, I like your solution best. -
i4h almost 8 yearsThank you for the comment, @oyalhi , I updated the shebang line in the answer. It should port better now!
-
webcpu over 6 yearschmod +x .git/hooks/post-commit
-
Tino over 5 yearsThere is no need for
ssh-agent
, just use another passphrase-lessgit
-onlyssh
-key:ssh-keygen -t ed25519 -f ~/.ssh/id_pushonly
.echo $'\nHost pushonly\nHostname DESTINATION\nIdentityFile ~/.ssh/id_pushonly\n' >> ~/.ssh/config
. OnDESTINATION
configuregit-shell
as shown in superuser.com/a/444899/72223 using pubkey from~/.ssh/id_pushonly.pub
. The neededgit
-URL is something likegit@pushonly:path/to/repo.git
. To debug:ssh git@pushonly COMMAND
must rungit-shell -c COMMAND
onDESTINATION
. ForCOMMAND
seeman git-shell
-
VonC over 5 years@Tino Thank you. Why are you using the digital signature scheme
-t ed25519
? I use generally-t rsa
, although recently I have to add-m PEM
tossh-keygen
(stackoverflow.com/a/53645530/6309, stackoverflow.com/a/53729009/6309). -
VonC over 5 years@Tino I understand rsa is slower, less secure (if its length is lower than 2048 bits: bagja.net/blog/upgrade-ssh-key-to-ed25519.html), but I deal at work with older openssh servers which might not interpret correctly an ed255519 signature.
-
Tino over 5 yearsI primarily use
ed25519
because it gives short and handy lines for~/.ssh/authorized_keys
. Also it is very interesting what DJB writes about ed255519: Secure against side channels (Spectre), less CPU, etc. BTW, when dealing with oldsshd
s, I usually create a special key for them and then configure this in~/.ssh/config
. -
otto about 4 yearshello i get this error: .git/hooks/post-commit: line 3: unexpected EOF while looking for matching ``' .git/hooks/post-commit: line 17: syntax error: unexpected end of file
-
i4h about 4 yearsHey, indeed there was a lost backtick. I updated the answer, please try again
-
Gagan over 3 yearsIf you don't want to store the password then also this approach works very well. Only thing after every commit you have to type the password.
-
Peter Mortensen over 2 yearsWhat kind of modification? Perhaps extend the answer (and/or point to some reference or Stack Overflow post)?
-
Peter Mortensen over 2 yearsWhy
#!/usr/bin/env bash
? What is the meaning of it (what is it supposed to do)? Is it to use Bash instead of Dash (sh
)? On what system? Ubuntu 16.04 (Xenial Xerus)? Why is it different from the default#!/usr/sh
? -
Peter Mortensen over 2 yearsWhat is "window task scheduler"? Is this on Windows? Do you mean Windows Task Scheduler? Or something else? You could also provide a screenshot to supplement the description (perhaps even free-hand circles (no joke)). Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).