0

I encountered a tricky problem. For instance I was working on the "feature" branch, after several commits and merges, the code delivered to the "master", and the diagram was like:

   ---A---B---C---D---I  master
       \   \     /   /
        E---F---G---H  feature

What I want to know is which files was modified on the "feature" branch.

I found this command: git log --name-only --pretty=format:'' origin_branch..new_branch It seems it can give me the result, but the point is I must specify a proper range.

But after I study the manual, I don't know how to give a right range.

What I want to get is:

        E--- ---G---H  feature

According to gitrevisions (git help gitrevisions):

master..feature => none

master...feature =>

   ---.---.---C---D---I  master
       \   \     /   /
        .---.---.---.  feature

feature^@ =>

   ---A---B---.---.---.
       \   \     /   /
        E---F---G---.  feature

You can see none of which matches my requirement.

Really thank you if you can help me with that.

tricycle
  • 190
  • 9
  • Just wanted to clarify, are F, D, I merge commits here? – Anshul Goyal Mar 16 '15 at 07:23
  • possible duplicate of [Showing which files have changed between git branches](http://stackoverflow.com/questions/822811/showing-which-files-have-changed-between-git-branches) – Joe Mar 16 '15 at 08:16
  • The question looks similar but they are not. My problem is: The master..branch cannot give me the right range, you will get nothing if you use "git diff --name-status master..feature". – tricycle Mar 16 '15 at 08:41
  • Take a look at my answer. I've updated it with a command chain which should work in all cases. – Sascha Wolf Mar 16 '15 at 18:39

1 Answers1

1

As I see it, the easiest way would be to find the commit on which feature deviated from master (in your case A). After this you can tell git to show you all commits on feature excluding merges and any commits after the "original" commit.

To find the commit feature originated from we can use this command chain; there might be an easier way, but I'm not aware of one:

git rev-list master --first-parent | grep "$(git rev-list feature --first-parent)" | head -1

This effectlivly lists all commits which are "only" on master by only following the first parent (ignoring the merged in commits) and compares them to the commits on feature. We use the first commit which occurs on both, that's the commit feature originated from.

Then we use this commit to list all commits on feature, ignoring merge commits and any commits which follow the "originating" commit.

git log feature --name-only --no-merges --first-parent --not $(git rev-list master --first-parent | grep "$(git rev-list feature --first-parent)" | head -1)

If you want to use this command more often I would suggest creating an alias.

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
  • With a test, it works and output what I want. Really thanks. But the command is too complicated and I need more time to understand it. – tricycle Mar 17 '15 at 15:16
  • @tricycle If you want I could extend my answer and go into greater detail. I didn't have a lot of time as I wrote this. – Sascha Wolf Mar 17 '15 at 18:16
  • I think I understand how you did it. Firstly, It is the "--first-parent" eliminate the merge commit from the history. Secondly, find the very beginning of the feature branch. So, no detail is needed, it is clear to me. Except you have more things that I don't realized. Thank you very much. – tricycle Mar 18 '15 at 06:12
  • @tricycle Actually first parent just takes care that git doesn't list the merged in branches for a merge commit. Usually `git log` and `git rev-list` will follow both parents of a merge commit and `--first-parent` ensures that it just follows, well, the **first** parent (which is the branch we merged the changes into). Maybe you meant to say that. – Sascha Wolf Mar 18 '15 at 06:23