Bonus Concepts

This section covers advanced and useful Git concepts related to branching, history, and project management.
Even if you’re new to Git, these tools and ideas will help you understand and recover your project in tricky situations!


Tags vs Branches

What is a Tag?

  • A tag is a fixed pointer to a specific commit.
  • Used to mark important points in history, like releases (v1.0.0, v2.1.3).
  • Tags do not move—they always point to the same commit.

Create a tag:

git tag v1.0.0

List tags:

git tag

Push tags to remote:

git push origin v1.0.0

How is a Tag Different from a Branch?

TagBranch
Fixed pointer to a commitMovable pointer to latest commit
Used for releases/versionsUsed for ongoing development
Does not changeMoves as new commits are added

Detached HEAD & Temporary Branches

What is Detached HEAD?

  • HEAD is Git’s pointer to your current branch.
  • Detached HEAD means HEAD points directly to a commit, not a branch.
  • Happens when you checkout a specific commit:
    git checkout a1b2c3d
    
  • You’re “not on a branch”—any new commits are not attached to a branch.

Why Use Detached HEAD?

  • Experiment with changes without affecting any branch.
  • Review or test old versions of your code.

How to Save Work from Detached HEAD

If you want to keep your work:

git switch -c temp-branch
  • This creates a new branch from your current state, saving your changes.

Orphan Branches

What is an Orphan Branch?

  • An orphan branch starts with no history—it’s a completely fresh branch.
  • Useful for creating documentation, gh-pages, or starting a new project in the same repo.

Create an orphan branch:

git checkout --orphan docs
  • Your working directory stays the same, but the branch has no commits.
  • Add and commit files as usual.

Reflog: Recover Lost Commits and Branches

What is Reflog?

  • Reflog records every change to HEAD (branch checkouts, commits, resets, etc.).
  • Lets you recover lost commits, branches, or undo mistakes—even after deletion.

View reflog:

git reflog
  • Shows a list of recent HEAD positions.

Recover a deleted branch:

  1. Find the commit hash in git reflog.
  2. Create a branch from it:
    git branch recovered-branch <commit-hash>
    

Bisecting: Find the Buggy Commit

What is Bisecting?

  • git bisect helps you find which commit introduced a bug by binary search.
  • You mark a “good” commit and a “bad” commit, and Git checks commits in between.

Start bisect:

git bisect start
git bisect bad                # Mark current commit as bad
git bisect good a1b2c3d       # Mark known good commit
  • Git checks out a commit in the middle. Test it, then mark as good or bad.
  • Repeat until you find the exact commit that introduced the bug.

Finish bisect:

git bisect reset

Visualization Tools

Command-Line Visualization

  • Show commit history as a graph:
    git log --oneline --graph --all
    
  • Show branches and merges visually.

GUI Tools

  • Gitk:
    Run gitk for a graphical history viewer.
  • SourceTree, GitKraken:
    Free tools for visualizing branches, merges, and history.
  • VS Code GitLens:
    Powerful extension for viewing history and branch structure inside VS Code.

Tip:
These advanced tools and concepts help you manage, recover, and understand your project’s history.
Practice using them—they can save your