Sharif
Sharif

Reputation: 1

Traverse through HEAD to access all git commits

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.

Upvotes: 0

Views: 984

Answers (2)

Sharif
Sharif

Reputation: 1

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

Upvotes: 0

eftshift0
eftshift0

Reputation: 30156

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

Upvotes: 1

Related Questions