Home > Back-end >  How to get changes that was commited on a branch, and only on that branch in Git?
How to get changes that was commited on a branch, and only on that branch in Git?

Time:05-31

Let's say, I have the following branches

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

There's a feature branch which is periodically refreshed from master by merging master to feature (merge commits B and E). And when feature is ready it is merged into master (merge commit J), so at this point feature and master is equivalent.

I'd like to view or log what has changed only on feature branch without merge commits(B and E). So is there any way that I can view the changes only on feature branch combined.

Thanks.

CodePudding user response:

git log --first-parent --no-merges A..feature

with any details you want, maybe --oneline --patch.

There's afaik no convenience command to find A here, the first-parent merge base, the most recent commit in two tips' first-parent ancestries, but it's pretty easy to do:

merge-base-first-parent() {
        ( git rev-list --first-parent $1;
          git rev-list --first-parent $2
        ) | awk 'seen[$1]  {print $1;exit}'
}

git log --first-parent --no-merges --oneline --patch \
        $(merge-base-first-parent master feature)..feature

and in really long histories you can get cute with rev-list's --parents and special handling for the degenerate cases to make it always run quick.

To view the changes as a single diff, you'll have to construct what the tip would look like without the merges. That's easy enough to do, you can

git checkout A
git rev-list --reverse --no-merges A..feature | git cherry-pick -n --stdin
git diff @
  • Related