Search code examples
gitgit-pushgit-pull

How to force pull before push


Every time I want to push my commits (git commit -m "message", git push origin <branch>), I do a pull (git pull origin <branch>).
Is there any way to make git do a pull before performing my push? (on the same branch)


Solution

  • The Git way to do this is with customize Git hooks.

    In your case, you will need to go to the .git/hooks directory on your repository, and add a file named pre-push, which is a script of your choice, e.g. (bash in this example)

    #!/bin/bash
    
    echo "pulling ..."
    git pull
    

    This script will be called when you do git push and before the actual push.

    Obviously, this is just a very naive sample, but I hope you get the idea. There are samples in this directory already. Comment if anything still unclear.