'Is there a "git pull --dry-run" option in Git?

Is there such a thing as git pull --dry-run to see how stuff will be merged before it messes up my working tree?

Right now I am doing:

git fetch origin && git merge --no-commit --no-ff

I did not see anything in the man page for 'git-pull' related to it.

To clarify, I just need it in an Ant script for deployment to see if there are conflicts when doing git pull, then back off exit out of build, fail deployment and leave that directory tree the same it was before git pull.



Solution 1:[1]

You will need to fetch first to update your local origin/master

git fetch origin

Then you can do:

git diff --name-only origin/master

Will list the files that have changed.

git diff origin/master directory_foo/file_bar.m

Will list the line by line diff of file directory_foo/file_bar.m.

Solution 2:[2]

You can get the effect you want by creating a new throw-away branch from your current one and doing the git pull there. If you're unhappy with the results, the original branch is intact.

Solution 3:[3]

# fetch new commits from origin
$ git fetch

# check what are the differences and judge if safe to apply
$ git diff origin/master

# actually merge the fetched commits 
$ git pull

Solution 4:[4]

Since v2.27.0 there is a dry-run flag

Solution 5:[5]

Since pulling implies merging, I'd go with running git merge --abort if your script detects there were any conflicts and merging failed.

Solution 6:[6]

See my answer in this similar question:

How to preview git-pull without doing fetch?

this goes to the ~/.gitconfig file:

[alias]
        diffpull=!git fetch && git diff HEAD..@{u}

Solution 7:[7]

OliverE is spot-on: git pull has a dry-run option, so I recommend git pull --dry-run -v to achieve the OP's purpose -- simple and direct. pull did not always have a dry-run option but in previous (and current) versions of git, fetch did (and does) have a dry-run option. Thus, an alternative approach is to do a git fetch --dry-run -v before you do your pull. Always better to check on an action before executing it, than having to spend time reverting.

Solution 8:[8]

Without pulling:

[ "$(git fetch;git diff | wc -l)" != "0" ] && (
echo there are updates
echo do your stuff here
)

or without touching anything:

[ "$(git pull --dry-run | wc -l)" != "0" ] && (
echo there are updates
echo do your stuff here
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GayleDDS
Solution 2 gcbenison
Solution 3
Solution 4 OliverE
Solution 5 kostix
Solution 6
Solution 7 rob_7cc
Solution 8 Zibri