Differnet Ways of Accessing a Commit in Git

| Comments

Ok, so it’s been quite long since I wrote anything for this blog. I made a resolution earlier about ‘a post a day’ which is apparently shattered. So here’s a quick and dirty posts straight from my notes to help bringing me back on track. Any help it does to anyone else is a bonus :)

So as we discussed in a previous post, there are many names for a commit (noun) in git. We may call it a branch, a tag, or just a commit, they are just different ways of accessing a commit.

So, here’s an incomplete list of different ways in which we can access a commit in git.



branch name

A branch in git is nothing but a named commit. So whenever we are referring to a branch, we are basically just accessing a commit.

tag name

Same is true for tags as branches in git. The only difference between a tag and a branch is that tags can have their own descriptions.

HEAD

HEAD represents currently checked-out commit. It’s basically just a pointer to a commit.

c23a23c43

A commit can of course be accessed with its full or abbreviated hash id. Abbreviated hash id is the hash id of the commit with minimum number of characters that are required to uniquely identify the commit. These are generally 7-8.
from here on I am using word ‘name’ for any of the above mentioned ways of accessing a commit.

name^  and also name^^

Represents the parent of a commit. If multiple parents present, represent the first parent. More ‘^’ means higher up the chain.

name^2

It represents second parent of the commit if multiple parents are present

name~5

Same as name^^^^^

name:path

References a certain file within a commit’s content tree

name^{tree}

Reference just the tree held by the commit, rather than the commit itself

name1..name2

A commit range. Refers to all commits reachable from name2 to name1. If either name1 or name2 is omitted, HEAD is used in its place.

name1…name2

For git log, it refers to all commits referenced by name1 or name2, but not by both.
For git diff, the range expressed is between name2 and the common ancestor of name1 and name2.

–since=”2 weeks ago”

Refers to all commits since a certain date

–until=”1 week ago”

Refers to all commits up to a certain date

–grep=pattern

Refers to all commits whose commit message matches the regular expression

–committer=pattern

Refers to all commits whose committer matches the pattern

–no-merges

Refers to all commits in the range that have only one parent

Comments