1

I want to get commits of specific branch. following is my code

exec("git log $branch", $logs);

$branch may be master or any other branch. But this give all commits.

Hardeep Singh
  • 713
  • 1
  • 8
  • 18

1 Answers1

1

But this give all commits.

Sure: it gives all commits reachable from master HEAD, which are all commit even if they are part of another branch. Up to the very first one (usually done on the master branch)

Even if you were to use another branch, you would still get all commit reachable from that other branch HEAD, even if they are part of master (assuming that other branch was done from one of master commits)

x--x--x--X           (master)
          \
           --o--o--O (anotherBranch)

git log anotherBranch = O-o-o-X-x-x-x

That is why you need two parameters: one which will start the commit retrieval, one which will stop it:

git checkout mybranch
git log --all --not $(git rev-list --no-walk \
    --exclude=refs/heads/mybranch \
    --exclude=HEAD \
    --all)

Put that in your Php command, and you will get only the commits you are after.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • @HardeepSingh Did you replace "mybranch" by your branch name? FOr the explanation, read https://stackoverflow.com/a/25801208/6309 – VonC May 28 '17 at 06:06
  • i tried at my best to understand answer of suggest question. but stil i could not get success. i just want only commit list of one branch – Hardeep Singh May 28 '17 at 12:59
  • i astonished that Git does not provide a proper way get git log Even if Git is most common tool use by developers to sync their million projects – Hardeep Singh May 28 '17 at 13:08
  • First, do you understand what a branch is in Git. Are the diagrams in my answer clear enough? – VonC May 28 '17 at 13:10
  • I know branching in GIT. But their answers not full fill my needs. i am actually creating a web based tool to sync my commit files to server, where git is not available. So i immediately need the solution. – Hardeep Singh May 28 '17 at 13:13
  • So based on my diagrams, you would need the fog commits, not the 'x' ones, correct? – VonC May 28 '17 at 13:15
  • Sorry, i don't know fog commit – Hardeep Singh May 28 '17 at 13:19
  • Ok OK i understood. Thanks VonC. you solve my problem. i no need to get branch specific commits. i need to just checkout the branch before getting logs. – Hardeep Singh May 28 '17 at 13:27