0

I am trying use the below command to access the commit ID's for all the commits on my branch.

git rev-parse HEAD~0 --> Gives the latest commit

git rev-parse HEAD~1 --> Gives the previous commit

git rev-parse HEAD~n

I need to access all the commit's one by one using a looping method
Ex: git rev-parse HEAD~i

I cannot make it work.

Koushik Ravulapelli
  • 1,140
  • 11
  • 30
Sharif
  • 1
  • 2
    `git rev-list --pretty=format:'%H' ` would list all those commit hashes sequentially in reverse chronological order (walking backwards from the branch tip through history), but is it usable for what you need? – Romain Valeri Feb 28 '19 at 15:36
  • This looks like homework, e.g., that you were given an assignment to figure out how to have a shell variable expand to the number, and write your own loop. Note, though, that this only walks *first* parents of each commit, so at merges, it will ignore all the commits brought in *by* the merge, a la `git log --first-parent`. – torek Feb 28 '19 at 17:10

2 Answers2

1

A simple while should work fine

git log --pretty="%h" | while read revision; do
    # do whatever you need to do with this revision
    echo revision $revision
done

If you need it in reverse, you can use --reverse as a parameter to log.

PS Trying to get the number of revisions?

revisions=$( git log --pretty="%h" | wc -l )
echo There are $revisions revisions on my branch
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Thanks, I am trying to do this to get the number of commits on my branch (HEAD). – Sharif Mar 01 '19 at 08:33
  • 2
    @Sharif So rather `git rev-list --count HEAD`? No ? – Romain Valeri Mar 01 '19 at 12:57
  • There you go! Another option I didn't know about. – eftshift0 Mar 01 '19 at 12:59
  • @RomainValeri Thanks for the update. Let me elaborate abit more. When I use the above commands I get a huge number 73000+ count which is not the correct count I am looking for. I have made just 2 commits on my branch and later merged my branch with master and this causes the above result. I am aware of the --no-merge command but not sure how to use it to get the count of 2 ? – Sharif Mar 04 '19 at 11:38
  • The following commands gives the commit id's which I was looking for git log origin/master.. --no-merges --> Gives only the commits id's specific to the branch ignoring master branch merges. – Sharif Mar 04 '19 at 11:55
  • @Sharif Your colloquial use of the expression "on my branch" has confused us all. I suspect your idea of branches in git might be slightly off. Commits are not "on branches", even though the metaphor is overly used. Anyway, glad that you now have what you wanted. – Romain Valeri Mar 04 '19 at 13:02
0

Finally managed to get the count for the number of commits on the dev branch ignoring the master merged pull commits git rev-list origin/master.. --no-merges --count The above commands returns a value of 2 which is as expected as I had made 2 commits on my branch and rest were master merge commits. I have stored this count as local variable i = 2. May I know how can I use this variable in the below command ? The below commands gives an error when used as it is. Basically I want to make the command work with value of i = 2. Thanks a ton

git rev-parse HEAD~i

Sharif
  • 1