The question
Is there a way to list all the commits done on current branch only since it has been branched from its parent?
Example
Let's say I'm on my master
branch and made commits A
and B
, then branch to a new feature-branch
and made commits C
and D
: is there a way to list only C
and D
?
CodePudding user response:
Git doesn't remember parent branches, or where a branch started, but you probably do:
Let's say I'm on my
master
... then [create]feature-branch
and made commitsC
andD
: is there a way to list onlyC
andD
?
The git log
command will show commits that are in some selected range of commits, if you use the b1..b2
syntax, which is short for b2 --not b1
. Here you'd want:
git log feature-branch --not master
or:
git log master..feature-branch
or:
git log master..HEAD
or:
git log master..
The way this works internally is that Git will, temporarily, "paint certain commits red" (for STOP) and other commits "green" (for GO). The "green paint" starts with the commits you say you want: HEAD
or feature-branch
for instance. It then floods "backwards" from commit to parent-commit.
The "red paint" starts with the commits you say to reject: master
, or commit B
in this case. It then floods "backwards" from commit to parent-commit.
The red "overrides" the green, or equivalently, we do the red first and don't re-paint a commit. So given that we have:
A <-B <-- master
\
C <-D <-- feature-branch (HEAD)
this "paints B
and A
red" and "paints C
and D
green" and hence the commits you'll see listed are D
and then C
(in that order due to the git log graph-walk order).
The master..
syntax is short for master..HEAD
: any time you leave a name off one side of the two-dot syntax, it means HEAD
.
If we add more commits to master
(and then go back to feature-branch
):
A <-B <-E <-F <-G <-H <-- master
\
C <-D <-- feature-branch (HEAD)
the "red paint" now starts at H
and works its way back to B
, so this still works.
Note: this two-dot syntax works with many Git commands and usually means this same thing. However, for git diff
, it doesn't: git diff master..feature-branch
just means find the commit that master
points to and find the commit that feature-branch
points to—which in this case would be B
and D
, or later, H
and D
—and diff those two commits. That is, git diff X..Y
and git diff X Y
are entirely identical.
(There's also a three-dot syntax, and it has one meaning for most Git commands and different and special meaning for git diff
. So git diff
is a little weird overall.)
CodePudding user response:
According to what you are saying, you should be using cherry and here is the documentation for a more in-depth look about it: https://git-scm.com/docs/git-cherry
The command that should show you what you are looking for:
git cherry -v <parent_branch> <targeted_branch>
# Given your example: git cherry -v master feature-branch
Explanation:
This would display all commits contained in <targeted_branch> and especially not from the <parent_branch>. Additionally, if the final parameter is omitted, the current branch will be compared instead. (See the documentation above for more detailed).