Ali Farhoudi
Ali Farhoudi

Reputation: 6020

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)

Upvotes: 22

Views: 12824

Answers (4)

creativeChips
creativeChips

Reputation: 1197

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.

Upvotes: 28

merlin2011
merlin2011

Reputation: 75545

Based on the OP's comments, they appear to be trying to avoid a merge commit between their recent commits and the commits on the remote, which is normally generated by a git pull when the local and remote histories have diverged.

The explicit way to do this is to first fetch and the rebase, and finally push.

git fetch
git rebase origin/master
git push

A second way to do this is to pass --rebase when invoking git pull.

git pull --rebase

Finally, one can make this the default behavior of git pull by setting the configuration.

git config --global pull.rebase true

Upvotes: 12

VonC
VonC

Reputation: 1323183

You actually don't need to pull before pushing: if there are commits on the remote side you don't have locally, the git push will fail with the message message:

Pushing to git@yourserver:<user>/<repo>.git
To git@yourserver:<user>/<repo>.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/BigMeanCat/CMDA'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

If you have control over the remote server, you can set there:

git config --system receive.denyNonFastForwards true

More generally, you can easily define an alias combining both commands:

git config alias.pullpush '!git pull && git push'

If the pull fails, the push won't be executed.


Finally, you can combine any sequence of command in a bash script, named

git-pullpush

(no extension, executable, stored in a folder referenced by the $PATH)

It would be a regular bash script (which works even on Windows, since it will be interpreted by the msys bash)

#!/bin/bash
# you can add any command you want
git pull && git push

And you would call it with git pullpush (like an alias)

Upvotes: 6

Saurav Sahu
Saurav Sahu

Reputation: 13924

When you want command2 to run if and only if command1 succeeds(basically returns 0), run using double ampersand sign &&, sandwiched between them:

command1 && command2 

And, to run command2 after command1 succeeds or even if fails, run using semicolon ;:

command1 ; command2 

I have used former one for git pull && git push and later one for git pull ; date

Upvotes: 3

Related Questions