How can I automatically push after committing in Git?
Asked Answered
N

8

104

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

Nichy answered 28/10, 2011 at 6:25 Comment(3)
What protocol are you pushing over? If it's prompting for password, I assume that this is either SSH or HTTP.Edmon
Also, it's always helpful with git questions to at least mention what operating system you are using.Edmon
I question the wisdom of such a set up. This removes any ability to reorganize your changes into a different set of commits (rebasing, especially). I make too many mistakes for a set up like this to end up being useful for me.Anatolic
E
166

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.

Edmon answered 28/10, 2011 at 6:32 Comment(5)
Couldn't make ssh-agent remember my passphrase, so had to make it empty. Hope my wife won't hack my account :)Nichy
Speaking 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 prefix feature/xy/.Helgoland
git push --all originSubfloor
chmod +x .git/hooks/post-commitDeka
If 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.Stolid
P
40

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").

Pacifica answered 20/1, 2015 at 10:24 Comment(5)
For 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.Rabelaisian
Thank you for the comment, @Rabelaisian , I updated the shebang line in the answer. It should port better now!Pacifica
hello 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 fileWood
Hey, indeed there was a lost backtick. I updated the answer, please try againPacifica
Why #!/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?Randellrandene
S
14

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.

Scheers answered 28/10, 2011 at 6:32 Comment(2)
What kind of modification? Perhaps extend the answer (and/or point to some reference or Stack Overflow post)?Randellrandene
Besides, the file post-commit needs to be executablePretor
S
3

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.

Sienese answered 28/10, 2011 at 6:32 Comment(4)
There is no need for ssh-agent, just use another passphrase-less git-only ssh-key: ssh-keygen -t ed25519 -f ~/.ssh/id_pushonly. echo $'\nHost pushonly\nHostname DESTINATION\nIdentityFile ~/.ssh/id_pushonly\n' >> ~/.ssh/config. On DESTINATION configure git-shell as shown in superuser.com/a/444899/72223 using pubkey from ~/.ssh/id_pushonly.pub. The needed git-URL is something like git@pushonly:path/to/repo.git. To debug: ssh git@pushonly COMMAND must run git-shell -c COMMAND on DESTINATION. For COMMAND see man git-shellAmbulatory
@Ambulatory 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 to ssh-keygen (https://mcmap.net/q/13492/-jenkins-what-is-the-correct-format-for-private-key-in-credentials, https://mcmap.net/q/13735/-sublime-merge-ssh-failing-when-key-has-password).Sienese
@Ambulatory 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.Sienese
I 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 old sshds, I usually create a special key for them and then configure this in ~/.ssh/config.Ambulatory
F
1

Here is a Bash script for Git to automatically push to a remote repository

  1. Automatically check ssh-agent
  2. Automatically send passphrase using expect script
  3. Usage is simply: cd /path/to/your/repository and then push

Add this script to a file, for example, $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)
[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 the /bin directory, so it can be called by just the push command:

sudo ln -s $HOME/.ssh/push /bin/push
chmod +x /bin/push
Frisby answered 5/8, 2019 at 21:2 Comment(0)
R
0

Here are simple instructions for pushing/pulling without providing passphrase over SSH for people using Linux and Windows (Git Bash)

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.
    

Note: You will have to provide a password during this operation. After that, your pull/push operations won't request a password.

Note 2: You have to log in to the server using user_name at least once before using this procedure (the home directory, to which SSH keys are copied, is created during the first login).

Rhythmandblues answered 24/8, 2016 at 20:42 Comment(0)
C
0

If you're using Husky, it will overwrite your post-commit hooks file by default.

We're using this command in package.json to auto-rebase-and-push any commits to master. (First run yarn add --dev git-branch-is.)

  "husky": {
    "hooks": {
     "post-commit": "git-branch-is master && git rebase origin master && git push origin master"`
    }
  }
Cutcheon answered 22/7, 2020 at 16:46 Comment(0)
C
0
  1. Create a Git file: 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 "Regular auto-commit $(timestamp)"
    git push origin master
    
  2. Open window task scheduler

  3. Create new task

  4. General → name the task

  5. Go to the trigger section and enable the task scheduler

  6. Press the Done button

Canarese answered 16/4, 2021 at 12:48 Comment(1)
What 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).Randellrandene

© 2022 - 2024 — McMap. All rights reserved.