141

I would like to inspect any code changes after doing a git pull. Currently it's just showing me which files changes. How can I see what code changed?

iblue
  • 29,609
  • 19
  • 89
  • 128
uwe
  • 3,938
  • 11
  • 37
  • 50

6 Answers6

146
git log --name-status -2

Will show you the names of the files that changed for the last two commits.

git log -p -2

Will show you the changes themselves.

Before you pull,

git fetch
git log --name-status origin/master..

Will show you what commits you are about to retrieve, along with the names of the files.

Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
  • 1
    Based upon this reply the command "git log --graph -p" is doing a nice job. It shows tree information about the history and code changes as well. Furthermore you can scroll up and down without any extra tool or gitk. – Daniel Gschösser Sep 13 '21 at 12:17
111

Before pulling

You can review changes as @iblue says with a fetch and diff before merging:

$ git fetch
$ git diff master...origin/master

Note the triple period, which means diff against the shared parent and origin/master (commits marked x below):

SP---o---o [master]
  \
   x---x [origin/master]

Just after a pull

The very first line in the output of a pull looks like this:

$ git pull
Updating 37b431a..b2615b4
...

You can then simply do:

$ git diff 37b431a..b2615b4

Or whatever other command:

$ git log --name-status 37b431a..b2615b4

Later on

If it has been a while since you pulled, and you wish to know what changes were brought in by the last pull, you can look it up with:

$ git reflog | grep -A1 pull | head -2

which will show the hash after the pull followed by the hash before the pull:

b2615b4 HEAD@{0}: pull : Fast-forward
37b431a HEAD@{1}: checkout: moving from v6.1 to master

You can then do the same thing with these two hashes:

git diff 37b431a..b2615b4
wjandrea
  • 28,235
  • 9
  • 60
  • 81
quornian
  • 9,495
  • 6
  • 30
  • 27
  • 4
    This is the only answer that allows you to check which files have changed during the last pull when you don't remember how many commits were pulled at that time. – kremuwa Jan 17 '17 at 08:45
31

Because git pull is just a shortcut for git fetch and git merge, you can run git fetch to fetch the branches from the origin and then show the differences before merging. Like this:

git fetch                      # Load changes from remote server
git diff master origin/master  # Show differences
git merge origin/master        # Merge remote changes with local changes

If you run on a different branch than master, you should of course change the branch names in the commands above.

iblue
  • 29,609
  • 19
  • 89
  • 128
18

You can compare the pulled contents with the sources of immediately previous commit by,

git diff branch_name@{1}

eg:

git diff master@{1}

For comparing with the sources n commits behind,

git diff branch_name@{n}
Kasun
  • 181
  • 1
  • 5
  • 6
    This is a good answer, but the explanation is incorrect: *"For comparing with the sources n commits behind"*. The `@{n}` syntax actually means the `nth` previous **position** of the branch/head. For example, if there have been 10 commits since the last time you did a pull, `master@{1}` would refer to the prior **position** of `master`, which is 10 **commits** prior. This is why using `@{n}` is useful for checking changes after a pull. – wisbucky Nov 30 '18 at 00:45
  • This is the answer I've been looking for! Thanks for explanation @wisbucky – tamerlaha Aug 17 '20 at 21:33
  • Equivalent simpler form for n-th previous commit on *master* branch: `master^n` – Alex Aug 09 '21 at 12:42
  • this should be chosen as the right answer! – lewis4u Apr 21 '22 at 06:52
5

You can check what get change while push and pull by this...

git log --stat
Sandip Karanjekar
  • 850
  • 1
  • 6
  • 23
  • you can't see code changes with this. Only who made the commit, which files were changed date of commit and stuff like that – lewis4u Apr 21 '22 at 06:50
0

Best way to compare status before and after a pull (after the pull has been done):

git diff master^1

Or more generically:

git diff <branch name>^<commits back>
DankMasterDan
  • 1,900
  • 4
  • 23
  • 35