Reputation: 707
I'm still new to Git so I'm not sure how to do this, although I've seen it done with some software. How can you get a list of commits that were made on a given branch?
The more specific reason I would like to know this is so that I can find all files that were added to the repo via a specified branch as the input. Thanks.
Upvotes: 2
Views: 1539
Reputation: 12629
A simplistic approach for a list of commits on a given branch, excluding merges into it:
git log --first-parent <branch>
The above command will include all commits that led to the commit at the creation of the branch.
Because, you must remember, git does not really track where branches have started. A branch is just a moving label, pointing to a commit, and its log normally shows a list of ALL commits that have contributed to the history of this commit. Including all merges.
In the original repo where the branch was created, you can extract the history from branch-start from the reflog:
git reflog show <branch>
But good luck on a server, where the branch was first pushed after multiple commits.
Actually, I wonder, does anyone have a reliable solution for finding the starting point of a branch? Graph theory experts wanted.
Upvotes: 1
Reputation: 7507
You don't need to checkout the branch in order to see its log. In fact, you don't necessarily need a branch at all, all you need is a ref. Either way, let's say your branch (or commit hash/ref) is called mybranch
. Simply run this command to get a list of all the files which were once a part of your git history:
git log --name-only --format="% " mybranch | grep -v '^$' | sort | uniq
Where --name-only
will show you the file names, --format="% "
will remove all commit messages and hashes, mybranch
marks the starting point to go backwards from (until the first commit). Then you remove all empty lines with grep
, and you have to sort
the names in order to remove duplicates with uniq
. The generated list will include files which were deleted or moved within the repository.
If you need to see only the current files (will not include deleted ones), use the following command:
git ls-tree -r --full-tree --name-only mybranch
To see a list of commits (without the files - your first question), simply run
git log REF
Or, if you want single line format, use the --format=oneline
flag.
Also, try out the --stat
flag. It's not as greppable as the above examples, but may prove itself useful for you one day.
Upvotes: 1
Reputation: 43790
Checking out git log --help
, there is the --branches=<pattern>
option which you can use.
So you can do git log --branches=<branch-name> --name-only
This will list all the commits along with the files that were changed in each one.
You can use git merge-base <branch1> <branch2>
to find the SHA that the branches diverged from.
Then using git log --branches=<branch-name> --name-only <SHA>..HEAD
you can see all the changes only on the branch.
Upvotes: 1