4

I want to check the changes that will done to the files after I run git pull. I know we use git fetch to retrieve new objects from remote repository without changing local repository. But how can I view these changes in editor ?

Paras Gupta
  • 264
  • 3
  • 11
  • 1
    I'm sorry but I don't see how this is different from https://stackoverflow.com/questions/5817579/how-can-i-preview-a-merge-in-git/5818279. You just want to know what will happen if you merge. That is easy to find out and has been well covered before. – matt Jun 03 '21 at 21:25
  • Does this answer your question? [How can I preview a merge in git?](https://stackoverflow.com/questions/5817579/how-can-i-preview-a-merge-in-git) – joanis Jun 08 '21 at 12:50

3 Answers3

7

Assuming that all your local modifications are commited, one way would be to do:

git fetch origin

# To see local changes as the "new" version
git diff origin/yourbranch HEAD

# Or to see remote changes as the "new" version
git diff HEAD origin/yourbranch

This will compare your local repository (currently HEAD) with the remote content origin/yourbranch.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
  • 1
    Probably want to swap the two arguments, so that things in HEAD but missing from `origin/yourbranch` show up as additions, not deletions. – chepner Jun 03 '21 at 18:37
  • 1
    @chepner Hmmm, based on the wording of the question which is "what will be done", perhaps the original order may be preferred? Imagine a fast forward only pull where some files will be added and some files will be deleted. – TTT Jun 03 '21 at 19:07
  • 1
    Right as well. In doubt I've edited with both possibilities. – Gaël J Jun 03 '21 at 19:11
  • I would do: `git diff HEAD...origin/yourbranch` or `git difftool -D HEAD...origin/yourbranch` or `git log HEAD...origin/yourbranch`. – Marek R Jun 03 '21 at 19:14
  • 1
    If you have not committed your files yet, `git fetch` followed by just `git diff origin/yourbranch` will work too. – joanis Jun 03 '21 at 19:49
4

to check the changes that will done to the files after I run git pull

Then the simplest solution is just run it and look! First, work out where you are:

% git show --oneline --no-patch
bf1908d ...

Now pull, and then diff to see what you just did:

% git pull
% git diff bf1908d

This shows you exactly what you are asking for: the changes done to the files after running git pull.

If you like the changes, do nothing. If you don't, and wish you had never done this pull, then git reset --hard bf1908d and you're right back where you were before the pull, no harm done.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 4
    *Absolutely* this. `git rev-parse @` is a shorter, more direct version of the `git show --oneline --no-patch`, but you don't even need to do that. `git pull; git diff @{1}` and if you don't like what you see `git reset --hard @{1}` – jthill Jun 04 '21 at 03:12
  • Clever. Yes, I always forget about `@{1}` as "where I just was". – matt Jun 04 '21 at 03:17
  • I'm not one for using git pull at all in the first place, but I still like this idea, it's simple and effective. Warning: don't do this if your sandbox has uncommitted local changes, the `git reset --hard @{1}` will destroy them. Mind you, it's best practice to commit or stash your local changes before you pull anyway, but Git does not require it, so I think it's worth a caveat. – joanis Jun 04 '21 at 12:31
1

Once you're done git fetch, you have a full local database of all the changes on the remote, which you have just fetched, so you have all to powers of Git to explore those changes.

  • git log origin/branch will show you the list of commits
  • git log -p origin/branch will show the commits with their changes, by commit.
  • git diff origin/branch or git diff HEAD origin/branch (see @Gaël J's answer)

If your local changes are committed, you can also check out origin/branch to play with it before going back to your own branch.

git checkout origin/branch  # go to the fetched state from origin
# explore stuff
git checkout branch  # go back to your own branch
joanis
  • 10,635
  • 14
  • 30
  • 40