Show which files changed in incoming git commits

2019-06-06 07:28发布

I'd like to see which files were touched after having fetched new commits. I want it in the same way that you'd see the files after a merge: list of files, with "x times" + (for lines inserted) and "y times" - (for lines deleted).

I've tried:

git log --pretty=format:'%h %d %s %an %ar' --date=short --stat HEAD..origin

but:

  1. it shows me commits one by one -- while I want to see the whole set of touched files as if there was only one big commit

  2. it does not stop at old commits I already had on my machine.

So, the goal here is really to see at a glance: if I merge all the new commits, which files are changed by the incoming commits?

标签: git logging
2条回答
Rolldiameter
2楼-- · 2019-06-06 08:23

The correct way to get the type of report you are looking for is to use git diff. For example:

git diff --stat master origin/master

If your local branches and their upstream counterparts always follow the same pattern (e.g. foo and origin/foo), then writing a small shell function into an alias should work - something like this in your .gitconfig:

[alias]
upstreamdiff = "!f(){ git diff --stat ${1} origin/${1} }; f"

If you have remotes that aren't named origin, or if you're not in the habit of using matching branch names, it becomes more complex, and you might need a full-on shell script to take a branch, find it's upstream partner, and then run the appropriate git diff command.

EDIT: to incorporate automatic branch determination:

alias
upstreamdiff = "!f() { b=$(git symbolic-ref --short HEAD); git diff --stat ${b} origin/${b} }; f"
查看更多
趁早两清
3楼-- · 2019-06-06 08:26

You ask

see which files were touched after having fetched

If you fetch multiple times without doing a merge or a rebase, then diffing from merge-base of your branch to your upstream will give you the sum of all of those fetches, and not just the most recent recent fetch. I find it useful to have both - I alias one of them to unmerged and the other to whatfetched.

Also, git provides builtin functionality to identify your upstream that will work even if the branch you are tracking doesn't have the same name, this is via the special ref @{u}.

So for what you would want, you would need to check

git rev-parse @{u}

That will tell you whether or not your branch has an upstream.

To get the name of your upstream you could use

 git rev-parse --abbrev-ref --symbolic-full-name @{u}

This will print our origin/$BRANCH, and will work even if you branch is tracking a branch with a different name. Assign that to $UPSTREAM

So see just the most recent fetch (whatfetched), you would use the reflog to show only the changes between where upstream is currently pointing and where it was pointing previously.

git diff --stat $(UPSTREAM) ^$(UPSTREAM)@{1}

If you wanted to see what precisely you were looking at here you could use something like

git reflog ${UPSTREAM}@{now} -2

To see the sum of all changes you haven't incorporated (unmerged), you would want to diff from the merge-base of your branch and the upstream to the upstream with

git diff --stat HEAD...$(UPSTREAM)
查看更多
登录 后发表回答