Skip to content
Branching in Git

Summary - Branching in Git

Introduction to Branches

Branching

Key feature in Git for developing new features, fixing bugs, or experimenting without affecting the main project.
Enables multiple lines of development simultaneously.

Key Concepts

Creating Branches: Isolate work on features or fixes.
Switching Branches: Move between development lines without interference.
Branch Isolation: Changes in one branch do not affect others.

Advantages of Using Branches

Parallel Development: Work on multiple features at once.
Safe Experimentation: Test ideas without risking the main codebase.
Organized Workflow: Keep the project manageable and organized.

Default Branch

Primary Branch: Main development line with stable code.
Naming: Often called "main".
Automatic Creation: Created when initializing a new Git repository.

What is a HEAD

Concept of HEAD

Reference to the current commit in your repository.
Moves with each new commit, always pointing to the latest commit on the current branch.

Scenarios Triggering HEAD Movement

New Commits: HEAD moves to the latest commit.
Switching Branches: HEAD updates to the latest commit on the switched branch.
Checking Out Specific Commits: HEAD can be moved to a specific commit, leading to a detached HEAD state.

Git Branch Operations

Listing Branches

git branch: See all branches in a repository.

Creating and Switching Branches

Create Branch: git branch <branch-name>
Switch Branch: git checkout <branch-name>

Renaming and Deleting Branches

Rename Current Branch: git branch -m <new-name>
Rename Other Branch: git branch -m <old-name> <new-name>
Delete Branch: git branch -d <branch-name> (use -D for force delete)

What is it like to lose HEAD

Detached HEAD State

Detached HEAD: Points to a specific commit, not the latest commit on a branch.
Temporary Changes: Changes do not affect any branch unless merged.
Visibility: Commits are not visible from branches unless merged.

Risk of Losing Commits

Dangling Commits: Commits in detached HEAD state are at risk of being deleted during Git's garbage collection.
Garbage Collection: Git cleans up unnecessary files, including unattached commits.

How to Preserve Detached HEAD Commits

Create a New Branch: Attach commits to a new branch to save changes.

HEAD Notations in Git

HEAD: Latest commit.
HEAD~1: Parent commit.
HEAD~2: Grandparent commit.

Advantages of Using Detached HEAD

Testing Changes: Test changes without committing to a branch.
Temporary Modifications: Make changes without affecting main branches.
Experimentation: Explore ideas safely.

Git Tags

Purpose of Git Tags

Mark specific points in a repository’s history as important.
Commonly used to mark release points.

Types of Git Tags

Lightweight Tags: Bookmarks to a specific commit.
Create Lightweight Tag: git tag <tag-name>
Annotated Tags: Include additional information.
Create Annotated Tag: git tag -a <tag-name> -m <message>

Tag Management

List Tags: git tag
Show Tag Details: git show <tag-name>
Checkout Tag: git checkout <tag-name> (results in a detached HEAD)
Delete Tag: git tag -d <tag-name>
Understanding Git is crucial for effective project management and collaboration. For more detailed information, refer to the Notion link:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.