my-notes

git_commits_and_refs

1 min read

git commits and refs

Understanding commits and references.


πŸ“ Commit Structure

A commit contains:

  • Tree (snapshot)
  • Parent(s)
  • Author
  • Committer
  • Message

View Commit Details

git cat-file -p HEAD

Shows full commit object.


🏷️ Reference Types

Ref Location Purpose
HEAD .git/HEAD Current position
branches .git/refs/heads/ Branch tips
remotes .git/refs/remotes/ Remote branches
tags .git/refs/tags/ Tag pointers

πŸ“Š Reference Diagram

graph LR
    A[HEAD] --> B[refs/heads/main]
    B --> C[commit abc123]
    D[refs/heads/feature] --> E[commit def456]
    F[refs/tags/v1.0] --> C

πŸ” View References

Show HEAD

cat .git/HEAD

Shows what HEAD points to.


Show Branch Ref

cat .git/refs/heads/main

Shows commit hash of main branch.


Show All Refs

git show-ref

Lists all references.


Resolve Reference

git rev-parse HEAD

Shows full commit hash.


Resolve to Short Hash

git rev-parse --short HEAD

Shows short commit hash.


πŸ“ Reference Syntax

Direct Reference

git show main

Shows main branch tip.


Parent Reference

git show HEAD~1

Shows parent of HEAD.


Grandparent

git show HEAD~2

Shows 2 commits before HEAD.


First Parent (Merge)

git show HEAD^1

First parent of merge commit.


Second Parent (Merge)

git show HEAD^2

Second parent (merged branch).


By Date

git show main@{yesterday}

Shows main as of yesterday.


By Reflog Index

git show HEAD@{5}

Shows HEAD from 5 moves ago.


πŸ“‹ Commit Ranges

Commits in B not in A

git log A..B

Shows commits reachable from B but not A.


Commits in Either

git log A...B

Shows commits in A or B but not both.


πŸ”§ Update References

Update Branch Ref

git update-ref refs/heads/main abc1234

Manually updates branch to commit.


Delete Reference

git update-ref -d refs/heads/old-branch

Removes a reference.


πŸ’‘ Tips

HEAD States
  • Attached: ref: refs/heads/main
  • Detached: abc123... (raw hash)
Symbolic Ref
git symbolic-ref HEAD

Shows which branch HEAD is on.



#git #commits #refs #internals

Built with mdgarden