Understanding Branches in Git
git branch, HEAD pointer, branch concept, main branch, feature branches, branch listing
Understanding Branches in Git
A branch in Git is a lightweight movable pointer to a commit. When you create a branch, Git simply creates a new pointer — it does not copy any files. This makes branching in Git extremely fast and cheap compared to other version control systems.
The HEAD Pointer
HEAD is a special pointer that tells Git which branch you are currently on. When you commit, the current branch pointer moves forward to the new commit, and HEAD follows it. You can think of HEAD as your current position in the repository.
Listing Branches
Run git branch to see all local branches. The current branch is marked with an asterisk. To see remote branches as well, use git branch -a.
The Default Branch
When you initialize a repository, Git creates a default branch. In modern Git installations, this is named main. Older repositories may use master. You can rename it at any time:
git branch -m master mainProfessional teams protect the main branch by requiring pull requests for all changes. Direct commits to main are typically blocked in collaborative projects.
Why Branch?
Branching lets multiple developers work simultaneously without interfering with each other. Each feature, bug fix, or experiment gets its own branch. Only when work is complete and reviewed is it merged into the main branch, keeping it stable at all times.
