“Debunking Common Myths and Clarifying Branching, Commits, and Timelines”@GIT

sonu kushwaha
4 min readDec 23, 2023

CAUTION:- Must have some background knowledge of GIT/GITHUB

https://www.flaticon.com/free-icon/version-control_5047324

Myth: Always Remember that when ever you commit, Keep Head Pointer on Branch Tag

Case A:

  • When you commit with the head pointer on the branch tag here(sonu), both the branch tag and the head pointer move along with the new commit. This is the normal behavior and ever one does on regular basis.

here, once you commit, the branch tag and the head pointer will move along the commits you will be making .(and this is the normal case/scenario )

Case B:

  • If you commit with the head pointer directly on a commit (not on a branch tag), only the head pointer moves. This creates a detached head state, where you are not on any specific branch.

now if you make a commit , only head will move along the commit you will be making,leaving behind the branch tags of sonu and master. you can see the following images.

Clarification:

  • In Git, branches are tags to specific commits, and the head pointer represents your current position in the commit history.
  • It is always suggest to have the head pointer on the Branch tag, rather than being on the specific Commit id.
  • In Case B, even though the head pointer is detached, the commit itself is still part of the commit history. You can create a branch from this commit later if needed.

Myth: Branch Equals Timeline

we usually think that branch is equivalent to timeline. but this is not the case branch actually is a tag to the timeline i.e. we can have timeline without branch, usually termed as unreachable/dangling timeline.

in the following diagram commit with id 7aa3e3a is the dangling/unreachable ,this was commited when our head pointer was on commit id 9b9c742 and after that i have checkout to soun branch this is what the following images depicts

command to delete the unreachable/dangling timeline

git reflog expire --expire-unreachable==now --all
git gc --prune=now

after the above command is run , following will be the diagram of the repository, with no unreachable/dangling timeline.

Clarification:

  • A branch in Git is like a movable pointer to a specific commit. It’s not the entire timeline; it’s a way to label a specific commit in the timeline.
  • Unreachable or dangling commits can exist, and they are not part of any branch. Git’s garbage collection can remove them, as shown with the commands git reflog expire and git gc.

Visualization Tool: gitviz

Note:

  • There isn’t a widely known tool called “gitviz.” Git itself provides a rich set of commands for visualization, such as git log --graph for a text-based representation or using external tools like GitKraken or Sourcetree.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Conclusion:

  • Understanding the relationship between branches, commits, and the head pointer is crucial.
  • Detached head state is not necessarily a problem, but it’s essential to create branches or tags to label specific commits for easier reference.
  • Git commands like git reflog expire and git gc are used to clean up unreachable commits.

--

--