92

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Danila Ladner
  • 863
  • 1
  • 7
  • 16
  • 1
    Why would there be conflicts? Is your deployment script making local commits and if so why? – CB Bailey Jun 21 '13 at 08:54
  • In looking at these responses I think we have overlooked a major difference that distributed source control -- A pull/fetch will grab a whole repository, not just one branch. You must specify individual branches as shown in a couple of answers below. – will Aug 23 '19 at 02:47
  • 1
    @CBBailey ... When many people are working on many branches (e.g different versions and features) there will often be overlap. Checking before hand makes it easier to plan your merge and later pull request. An equivalent command with SVN is: `svn -q -u`. – will Aug 23 '19 at 02:56

9 Answers9

77

I have always relied on the inherent abilities of Git to get me back if a merge fails.

To estimate how the merge might occur, you can start like you did with:

$ git fetch origin branch  # Fetch changes, but don't merge
$ git diff HEAD..origin/branch # Diff your current head to the fetched commit

... personal judgement of potential merge conflicts ...

$ git merge origin/branch # merge with the fetched commit

If things did not go as planned, look at your reflog and reset back to your desired state:

$ git reflog
...
abc987  HEAD@{0}: merge activity
b58aae8 HEAD@{1}: fetch origin/branch
8f3a362 HEAD@{2}: activity before the fetch
...
$ git reset --hard HEAD{2}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rynmrtn
  • 3,371
  • 5
  • 28
  • 44
  • You can also try this since pulling is somehow similar to merging [http://stackoverflow.com/questions/501407/is-there-a-git-merge-dry-run-option](http://stackoverflow.com/questions/501407/is-there-a-git-merge-dry-run-option) – scireon Jul 20 '16 at 08:13
  • `fatal: ambiguous argument 'HEAD..origin/branch': unknown revision or path not in the working tree.` – Green May 26 '17 at 16:24
  • @Green try git reset --hard HEAD@{2} – Alex Shenfield Nov 06 '18 at 10:47
  • @Green maybe like that: `git diff HEAD..origin/master` – Kuronashi Mar 05 '21 at 09:29
41

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.

GayleDDS
  • 4,443
  • 22
  • 23
21

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.

gcbenison
  • 11,723
  • 4
  • 44
  • 82
19
# 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
rantoniuk
  • 1,083
  • 12
  • 18
  • 8
    Why the downvote? I did a `git diff this-answer/top-answer` and no significant error is found. Please consider leaving a comment when you commit a downvote: `git commit -m "why would you downvote, blah blah blah"`. – WesternGun Jul 20 '17 at 07:50
15

Since v2.27.0 there is a dry-run flag

OliverE
  • 437
  • 5
  • 12
4

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

kostix
  • 51,517
  • 14
  • 93
  • 176
  • If you were doing this against a remote branch (going a `git fetch origin branch` first) it would be `git merge --abort origin/branch` right? – Nick Devereaux Feb 27 '14 at 23:41
1

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}
Andy P
  • 306
  • 4
  • 10
1

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.

rob_7cc
  • 797
  • 6
  • 16
0

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
)
Zibri
  • 9,096
  • 3
  • 52
  • 44