8

in one of our projects, MysQL-Dumps are synced with git. One file for each table in the database. After pulling/merging the dumps are imported to the local database with a hook. The problem is now, I need to find all table dumps that were changed with the pull to import only these. How can I get the post-merge-hook to know, which files changed? It is possibly not only one commit before that gets merged, usually, there are more commits.

How can I get a list of modified files?

1
  • This does it: git fetch && git diff --name-only ..origin/master. I do not understand why the answers do not consider git fetch.
    – Timo
    Commented Apr 15, 2023 at 11:33

4 Answers 4

6

git diff --name-only SHA1 SHA2

Use git log to get git commit ids. Use just git diff SHAx if you just want diff against latest head that you have pulled.

Before you git pull do git log or git status to get the SHAx. You can do git fetch instead of pull and do log and diff before you do merge.

3
  • 2
    yeah, SHA1 is my HEAD, but how do I get the correct SHA2?
    – WolleTD
    Commented Jan 8, 2014 at 15:04
  • 2
    You can probably explain your answer and articulate it with a description of what you are proposing.
    – Sekhemty
    Commented Jan 8, 2014 at 17:48
  • This doesn't really help when you didn't get the commit sha before pulling Commented Jun 22, 2018 at 16:48
5

The easiest way I found is

git diff --name-only HEAD@{0} HEAD@{1}

since HEAD@{n} means the n-th previous value of HEAD

1
  • HEAD@{n} is the Nth HEAD from your reflog - it changes as you work (switch branch, commit, rebase, etc). It might have worked after doing something specific but It is only meaningful based on the previous commands you ran or the contents of git reflog. Commented Sep 16, 2020 at 5:51
3

I used git reflog right after I did git pull to get something like:

SHA1 HEAD@{0}: pull: Fast-forward
SHA2 HEAD@{1}: checkout: moving from somewhere to anotherplace

the SHA right after the one reading "pull: Fast-forward" is the one where you were before the pull.

After that I used the git diff --name-only SHA2 from the other answer, which will give you a list of files that were changed from that commit to your current state.

I'm not 100% sure how this works for pulls with lots of changes and merges, but for a simple fast-forward it worked very well.

1
  • actually HEAD@{0} (or the one reading "pull: Fast-Forward") is the actual commit - after the pull - and not the one before the pull!
    – user35014
    Commented May 2, 2017 at 10:28
0

Right after you pull/merge a branch, you can see then changes form the pull or merge (i.e. changes introduced by the new commits/merged in) with:

git diff ORIG_HEAD HEAD

Else most merges will have two parents, HEAD^1 and HEAD^2. If all changed tables were committed as a single commit, then most likely one of them is the master/source branch and the other it the hook's commit - all you need to do is show the changes with only that side, ex:

git diff --name-only HEAD^2

For more complexe / criss-cross merges, it can be actually quite hard to untangle the changes. Git log has advanced options to select the most desirable path which depends on the change flow, YMMV.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .